Objective c 检查mapView是否已包含注释

Objective c 检查mapView是否已包含注释,objective-c,xcode,annotations,mkmapview,mkannotationview,Objective C,Xcode,Annotations,Mkmapview,Mkannotationview,我有一种方法,可以在点击另一个注释(ann1)时添加辅助注释(ann2)。但是,当我取消选择并重新选择完全相同的注释(ann1)时,ann2会自行重新创建注释,并再次添加注释。是否有方法检查地图上是否已经存在注释,如果是,则不执行任何操作,否则添加新注释。我已经检查过了,但对我没有帮助。。任何建议都将不胜感激。这就是我到目前为止所做的: fixedLocationsPin *pin = [[fixedLocationsPin alloc] init]; pin.title

我有一种方法,可以在点击另一个注释(ann1)时添加辅助注释(ann2)。但是,当我取消选择并重新选择完全相同的注释(ann1)时,ann2会自行重新创建注释,并再次添加注释。是否有方法检查地图上是否已经存在注释,如果是,则不执行任何操作,否则添加新注释。我已经检查过了,但对我没有帮助。。任何建议都将不胜感激。这就是我到目前为止所做的:

    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];
            }
在这种情况下,我得到的日志已经在地图上,但我也得到了添加到地图的注释的放置动画。有什么想法吗


提前感谢您。

您的
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];
} 

根据我对克雷格回答的评论,我认为解决方案可能是这样的:

import MapKit

extension MKMapView {

    func containsAnnotation(annotation: MKAnnotation) -> Bool {

        if let existingAnnotations = self.annotations as? [MKAnnotation] {
            for existingAnnotation in existingAnnotations {
                if existingAnnotation.title == annotation.title
                && existingAnnotation.coordinate.latitude == annotation.coordinate.latitude
                && existingAnnotation.coordinate.longitude == annotation.coordinate.longitude {
                  return true
                }
            }
        }
        return false
    }
}
此代码允许您检查地图视图是否包含给定注释。在所有注释的“for”循环中使用此选项:

for annotation in annotations {
  if mapView.containsAnnotation(annotation) {
    // do nothing
  } else {
    mapView.addAnnotation(annotation)
  }
PS:如果您需要向地图视图添加新注释,此功能非常有效。但是如果您还需要删除条目,则可能需要执行相反的操作:检查新注释数组中是否存在每个现有注释;如果没有,请将其拆下。
或者,您可以删除所有内容,然后再次添加所有内容(但随后您将设置更改动画…

注释数组存在于地图对象中,因此您只需进行检查

if ( yourmap.annotations.count==0)
{
NSLog(@"no annotations");
}

您是否正在从地图视图中删除注释。。为什么要删除它?谢谢..你告诉我它再次添加na,所以我有一个疑问..我得到注释AnimateDrop,所以我猜它正在重新添加/复制自己。。是否有办法查看它是否实际添加了另一个或仅模拟动画?如果(MKMapRectContainsPoint(mapView.visibleMapRect,MKMapPointForCoordinate(pins.coordinate)),则使用此选项它将检查map rect的可见区域中的管脚是否为visible map rect中的另一个点我不知道该如何工作。检查标题和坐标是否相等是个好主意,但这里的逻辑似乎是错误的。一旦确定所有现有点都已被解析,您就需要添加注释。没错,Fred,它需要到达循环的末尾,并确保没有要添加的项,然后添加它。我会更新代码。是的,现在可以了。也看看我的答案。虽然它是用Swift编写的,但是它的优点是,如果找到一个条目,它就会立即存在,而不是继续不必要地解析注释这段代码检查mapView中是否有注释,而不是mapView中是否包含给定注释。
NSNumber *latCord = [row valueForKey:@"latitude"];
NSNumber *longCord = [row valueForKey:@"longitude"];
NSString *title = [row valueForKey:@"name"];

CLLocationCoordinate2D coord;
coord.latitude = latCord.doubleValue;
coord.longitude = longCord.doubleValue;
MapAnnotation *annotation = [[MapAnnotation alloc]initWithCoordinate:coord withTitle:title];
if([mkMapView.annotations containsObject:annotation]==YES){
    //add codes here if the map contains the annotation.
}else {
    //add codes here if the annotation does not exist in the map.
}
        if (sampleMapView.annotations.count > 0) {             
            sampleMapView.removeAnnotation(detailMapView.annotations.last!)
        }