MKUserLocation 蓝色 userLocation MKAnnotation 如果不经意触摸会导致应用程序崩溃

发布于 2025-01-01 11:29:59 字数 1393 浏览 4 评论 0原文

我有一个 MKMap 和一系列 MKAnnotations,所有这些都是红色的,这很好。我在 IB 中选择了“显示用户位置”,并将 MKAnnotation 从红色更改为蓝色,我的 viewForAnnotation 方法中有代码:

if (annotation == theMap.userLocation)
    return nil;

一切都很好,应用程序可以正常工作很好,但如果用户无意中点击了蓝色的用户位置点,我会遇到以下崩溃:

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:

如果我删除上面的代码,一切正常,但引脚是红色的。我更喜欢蓝色图标,但尚未发现崩溃的原因。任何想法将不胜感激。谢谢。

解决了!谢谢马文,这是代码,以防有人发现它有用。简而言之,我必须首先检查 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;
}

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:

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:

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.

- (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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

独享拥抱 2025-01-08 11:29:59

对于“当前用户位置”,请转到 MKMapView 属性并选择“在 XIB 中显示用户位置”。

然后在你的控制器中实现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
    }
}

- (void)mapView:(MKMapView *)mapView1 didSelectAnnotationView:(MKAnnotationView *)customAnnotationView
{

    if(![annotation isKindOfClass:[MKUserLocation class]])
    {
          //Your code
    }

}

使用这个你可以在用户位置上看到蓝点..

For Current User Location go to MKMapView property and make selection on Shows User Location in XIB.

then Implement MKMapViewDelegate in your controller..and write this method in your controller

-(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..

悲歌长辞 2025-01-08 11:29:59

您将需要检查注释的类,

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{

然后执行以下操作。

id *annotation = view.annotation;
    if (![annotation isKindOfClass:[MKUserLocation class]]) {
//Normal Code here
}

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
}
好倦 2025-01-08 11:29:59

该解决方案在 iOS7 中可以抑制标注视图,但它不会阻止用户选择蓝点。 如何在地图视图中隐藏“当前位置”标注

我还没有找到一种方法来禁用当前位置点的选择。

与其说这是一种解决方法,不如说是一种“处理方法”,但我会放大地图,以便在点击时重点关注某些类型的注释。如果用户恰好位于某个注释的正上方,则缩放有助于在蓝点和我的注释之间留出一定的距离,从而更容易点击。

- (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];
    }
}

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

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];
    }
}
内心激荡 2025-01-08 11:29:59

我遇到了同样的问题,当用户点击 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
}
}

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

-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view{
id annotation = view.annotation;
    if (![annotation isKindOfClass:[MKUserLocation class]]) {
//Your Annotation Code here
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文