xcode MKUserLocation 蓝色 userLocation MKAnnotation 会导致应用程序在无意中触碰时崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9093690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MKUserLocation blue userLocation MKAnnotation causes app to crash if inadvertently touched
提问by timv
I have a MKMap
with a series of MKAnnotations
, all of which are red which is fine. I have selected "show user location" in IB and to change the MKAnnotation
from red to blue, I have the code in my viewForAnnotation
method:
我有MKMap
一系列的MKAnnotations
,都是红色的,很好。我在 IB 中选择了“显示用户位置”并将MKAnnotation
红色更改为蓝色,我的viewForAnnotation
方法中有代码:
if (annotation == theMap.userLocation)
return nil;
All is good and the app works fine, but if the user inadvertently taps the blue userlocation dot I get the following crash:
一切都很好,应用程序运行良好,但如果用户无意中点击了蓝色的 userlocation 点,我会遇到以下崩溃:
2012-02-01 20:43:47.527 AusReefNSW[27178:11603] -[MKUserLocationView setPinColor:]: unrecognized selector sent to instance 0x79b0720
2012-02-01 20:43:47.528 AusReefNSW[27178:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MKUserLocationView setPinColor:]: unrecognized selector sent to instance 0x79b0720'
*** First throw call stack:
If I remove the above code, all works well but the pin is red. I perfer to have the blue icon but as yet have not discovered why the crash. Any ideas would be appreciated. Thanks.
如果我删除上面的代码,一切正常,但引脚是红色的。我更喜欢有蓝色图标,但尚未发现崩溃的原因。任何想法,将不胜感激。谢谢。
SOLVED! Thanks Marvin and heres the code incase anyone finds it useful. In a nutshell, I had to first check to see if the MKAnnotation was of MyAnnotation Class or of MKUserLocation Class.
解决了!感谢 Marvin 和继承人代码柜面任何人发现它有用。简而言之,我必须首先检查 MKAnnotation 是属于 MyAnnotation 类还是属于 MKUserLocation 类。
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKPinAnnotationView *)view
{
theAnnotationSelected = [[mapView selectedAnnotations] objectAtIndex:0];
if ([theAnnotationSelected isKindOfClass:[MyAnnotation class]] )
{
view.pinColor = MKPinAnnotationColorGreen;
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKPinAnnotationView *)view
{
if ([theAnnotationSelected isKindOfClass:[MyAnnotation class]] )
{
view.pinColor = MKPinAnnotationColorRed;
}
回答by Mehul Mistri
For Current User Location go to MKMapView
property and make selection on Shows User Location in XIB.
对于当前用户位置,转到MKMapView
属性并在 XIB 中显示用户位置上进行选择。
then Implement MKMapViewDelegate in your controller..and write this method in your controller
然后在您的控制器中实现 MKMapViewDelegate ..并在您的控制器中编写此方法
-(MKAnnotationView *)mapView:(MKMapView *)pmapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil; //return nil to use default blue dot view
if([annotation isKindOfClass:[MKAnnotationClass class]])
{
//Your code
}
}
and this
和这个
- (void)mapView:(MKMapView *)mapView1 didSelectAnnotationView:(MKAnnotationView *)customAnnotationView
{
if(![annotation isKindOfClass:[MKUserLocation class]])
{
//Your code
}
}
Using this you can see blue dot on User Location..
使用它,您可以在用户位置上看到蓝点..
回答by Lee Armstrong
You will want to check the class of the annotation in
您将要检查注释的类
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
Then do the following..
然后做以下..
id *annotation = view.annotation;
if (![annotation isKindOfClass:[MKUserLocation class]]) {
//Normal Code here
}
回答by James Greenhalgh
This solution works in iOS7 to suppress the callout view, but it does not stop the user from selecting the blue dot. How to suppress the "Current Location" callout in map view
此解决方案适用于 iOS7 以抑制标注视图,但不会阻止用户选择蓝点。 如何在地图视图中抑制“当前位置”标注
I have yet to find a way to disable selection of the current location dot.
我还没有找到禁用当前位置点选择的方法。
It isn't so much a workaround as a "way to deal with it", but I zoom the map in to focus on certain types of annotations when tapped. If the user happens to be located right on top of one, the zooming helps put some distance between the blue dot and my annotation, making it easier to tap.
这与其说是一种“处理方式”,不如说是一种解决方法,但我会放大地图以在点击时专注于某些类型的注释。如果用户恰好位于一个顶部,缩放有助于在蓝点和我的注释之间放置一些距离,使其更容易点击。
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
if([view.annotation isKindOfClass:[SpecialClass class]]){
SpecialClass *cluster = (SpecialClass *)view.annotation;
if(testCriteria){
[self.mapView setRegion:MKCoordinateRegionMakeWithDistance(cluster.coordinate, cluster.radius, cluster.radius) animated:YES];
}
}
回答by elimcjah
I had the same issue such that when a user would tap the MKUserlocation "blue dot" the application would crash. I am using DDAnnotation by Ching-Lan 'digdog' HUANG. The fix I found was
我遇到了同样的问题,当用户点击 MKUserlocation“蓝点”时,应用程序会崩溃。我正在使用 Ching-Lan 'digdog' HUANG 的 DDAnnotation。我找到的解决方法是
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
id annotation = view.annotation;
if (![annotation isKindOfClass:[MKUserLocation class]]) {
//Your Annotation Code here
}
}