xcode 检查 mapView 是否已包含注释

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15134935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:52:36  来源:igfitidea点击:

Check if mapView already contains an annotation

objective-cxcodeannotationsmkmapviewmkannotationview

提问by user1780591

I have a method of adding secondary nearby annotations (ann2) when I tap on another annotation (ann1). But when I deselect and re-select the exact same annotation (ann1) the ann2 re-creates it self and is getting added again. Is there a way to check if the annotation already exists on the map and if yes then do nothing otherwise add the new annotation. I have already checked this: Restrict Duplicate Annotation on MapViewbut it did not help me.. Any advice is appreciated. This is what I have so far:

当我点击另一个注释 (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];
            }

In this case I get the log already on map but I also get the drop animation of the annotation adding to the map. Any ideas?

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

Thank you in advance..

先感谢您..

回答by Craig

Your forloop isn't checking if the annotation is on the screen, it is checking if the coordinates of the pin are currently within the visible area. Even if it was checking if the pinobject was already in the mapView.annotationsit would never be true, because you've only just created pina few lines earlier, it can't possibly be the same object as on in the mapView.annotations. It might though have the same coordinates and title, and that's what you need to check:

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

回答by zkn

Annotationsarray exist in map object so you just have to check

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

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

回答by handiansom

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

回答by A.G

        if (sampleMapView.annotations.count > 0) {             
            sampleMapView.removeAnnotation(detailMapView.annotations.last!)
        }

回答by Frédéric Adda

Following my comment on Craig's answer, I think the solution could look like something like this :

在我对 Craig 的回答发表评论之后,我认为解决方案可能如下所示:

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

This code allows you to check if a mapView contains a given annotation. Use this in a "for" loop on all your annotations:

此代码允许您检查 mapView 是否包含给定的注释。在所有注释的“for”循环中使用它:

for annotation in annotations {
  if mapView.containsAnnotation(annotation) {
    // do nothing
  } else {
    mapView.addAnnotation(annotation)
  }

PS: this works well if you need to addnew annotations to a mapView. But if you need also to removeentries, you may have to do the opposite: check that each existing annotation exists in the new array of annotations ; if not, remove it. Or you could remove everything and add everything again (but then you will have the change animated ...)

PS:如果您需要向mapView添加新注释,这很有效。但是,如果您还需要删除条目,您可能需要做相反的事情:检查每个现有的注释是否存在于新的注释数组中;如果没有,请将其删除。或者您可以删除所有内容并再次添加所有内容(但随后您的更改将具有动画效果...)