检查mapView是否已包含注释

use*_*591 7 xcode annotations objective-c mkmapview mkannotationview

当我点击另一个注释(ann1)时,我有一种添加辅助附近注释(ann2)的方法.但是当我取消选择并重新选择完全相同的注释(ann1)时,ann2会重新创建它并再次添加.有没有办法检查地图上是否已存在注释,如果是,则不执行任何操作,否则添加新注释.我已经检查了这个:限制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];
            }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我已经在地图上获得了日志,但我也获得了添加到地图的注释的拖放动画.有任何想法吗?

先感谢您..

Cra*_*aig 5

您的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];
} 
Run Code Online (Sandbox Code Playgroud)