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