如何阻止 viewForAnnotation 方法覆盖 iOS 中的默认用户位置蓝色信标

Posted

技术标签:

【中文标题】如何阻止 viewForAnnotation 方法覆盖 iOS 中的默认用户位置蓝色信标【英文标题】:How to stop the viewForAnnotation method from overriding the default user location blue beacon in iOS 【发布时间】:2012-07-30 19:42:12 【问题描述】:

现在我正在使用注释填充地图视图,并显示用户的当前位置。使用 viewForAnnotation 方法,它使用默认的红色引脚覆盖所有注释,但我想返回其他注释的视图,但将用户位置保留为默认的蓝色信标。有没有办法简单地做到这一点,或者我必须创建一个新的注释视图并根据注释返回正确的视图?

现在我有类似的东西:

- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation

    if (annotation.coordinate == locationManager.location.coordinate) 

return nil;

    else 

    MKAnnotationView *annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"Beacon"];

    // Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    button.frame = CGRectMake(0, 0, 23, 23);
    annotationView.rightCalloutAccessoryView = button;


    annotationView.canShowCallout = YES;

    return annotationView;


但我不能将两者等同起来,因为我无法从注释参数中获取坐标属性。

有人知道解决办法吗?

【问题讨论】:

【参考方案1】:

在此处查看文档:

http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapViewDelegate_Protocol/MKMapViewDelegate/MKMapViewDelegate.html

正如它所说:

如果注解参数中的对象是 MKUserLocation 类,您可以提供自定义视图来表示 用户的位置。使用默认值显示用户的位置 系统视图,返回 nil。

所以你可以添加一个条件来检查这个:

if([annotation isKindOfClass: [MKUserLocation class]]) 
  return nil;

【讨论】:

我很愚蠢,非常感谢,我正在尝试所有这些愚蠢的工作,但是是的...... 非常感谢!我必须学会停止成为苹果文档的新手 在不到 1 分钟的时间内就发挥了作用。请来参加聚会。自过去 3-4 小时以来,我一直在寻找这个。谢谢,伙计。【参考方案2】:

Swift 5.0

在函数顶部打勾以忽略userLocation:

   func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? 
        guard annotation as? MKUserLocation != mapView.userLocation else  return 

【讨论】:

以上是关于如何阻止 viewForAnnotation 方法覆盖 iOS 中的默认用户位置蓝色信标的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3 中未调用 ViewForAnnotation

iPhone MapKit 问题:viewForAnnotation 设置 pinColor 不一致?

强制 MKMapView viewForAnnotation 更新

mkmapview MKUserLocation AnnotationView

如何更改 MapView 注释中的标注气泡颜色?

jquery的mouseover方法如何阻止事件冒泡