objective-c - 检查 mapView 是否已经包含注释

标签 objective-c xcode annotations mkmapview mkannotationview

当我点击另一个注释 (ann1) 时,我有一种添加辅助附近注释 (ann2) 的方法。但是,当我取消选择并重新选择完全相同的注释 (ann1) 时,ann2 会自行重新创建它并再次添加。有没有办法检查 map 上是否已存在注释,如果是,则什么都不做,否则添加新注释。我已经检查过了:Restrict Duplicate Annotation on MapView但它对我没有帮助.. 任何建议表示赞赏。这是我目前所拥有的:

    fixedLocationsPin *pin = [[fixedLocationsPin alloc] init];
        pin.title = [NSString stringWithFormat:@"%@",nearestPlace];
        pin.subtitle = pinSubtitle;
        pin.coordinate = CLLocationCoordinate2DMake(newObject.lat, newObject.lon);

        for (fixedLocationsPin *pins in mapView.annotations) {
            if (MKMapRectContainsPoint(mapView.visibleMapRect, MKMapPointForCoordinate (pins.coordinate))) {
                NSLog(@"already in map");
            }else{
                [mapView addAnnotation:pin];
            }

在这种情况下,我已经在 map 上获得了日志,但我也获得了添加到 map 的注释的放置动画。有什么想法吗?

提前谢谢你..

最佳答案

您的 for 循环不检查注释是否在屏幕上,而是检查图钉的坐标当前是否在可见区域内。即使它正在检查 pin 对象是否已经在 mapView.annotations 中,它也永远不会是真的,因为您刚刚创建了 pin 前面几行,它不可能是 mapView.annotations 中的同一个对象。它可能具有相同的坐标和标题,这就是您需要检查的内容:

bool found = false;
for (fixedLocationsPin *existingPin in mapView.annotations)
{
  if (([existingPin.title isEqualToString:pin.title] && 
       (existingPin.coordinate.latitude == pin.coordinate.latitude)
       (existingPin.coordinate.longitude == pin.coordinate.longitude))
  { 
    NSLog(@"already in map");
    found = true;
    break;
  }
}    
if (!found)
{
    [mapView addAnnotation:pin];
} 

关于objective-c - 检查 mapView 是否已经包含注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15134935/

相关文章:

ios - 保持 NSUserActivity 向后兼容 Xcode 9

swift - 如何从具有多个图钉的 map View 中删除一个图钉

objective-c - 适用于 iPhone/iPad 的线性回归库或代码片段?

ios - UIButton 在整个表面上没有响应(UICollectionViewCell)

iphone - 动画 imageview 第一次需要时间

ios - 无法对 flurry 崩溃日志进行去符号化

Java验证: type level annotation for multiple types

r - 从注释数据库中选择注释的函数不起作用

ios - 从 iOS 应用程序使用用户名打开 Vine 配置文件

iOS - 不完美地镜像 JSON 是否被认为是不好的做法?