从MKMapView删除注释时出现问题。我已经搜索了相同的内容,并找到了很多答案,但找不到满意的答案。以下是我的代码摘要:
我将自定义类创建为MyMapViewPoints,并创建了一个函数
- initWithZTitle:(NSString *)title andCoordinate:(CLLocationCoordinate2D)location
每当我要添加注释时,我只需创建一个MyMapViewPoints对象并
[mapView addAnnotation:newAnnotation];
当我想删除所有的mapview点(注释)时,我执行以下代码:
for (int i =0; i < [mapView.annotations count]; i++)
{
if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyMapViewPoints class]])
{
MyMapViewPoints *obj = (MyMapViewPoints *)[mapView.annotations objectAtIndex:i];
if(obj.type != 1)
[mapView removeAnnotation:[mapView.annotations objectAtIndex:i]];
}
}
但是某些注释点仍保留在 map 上。如果我添加了六个点并尝试使用上述代码2删除了所有点,则保留mapview点(注释)。有任何想法吗?
最佳答案
只需尝试此代码...
NSArray *existingpoints = mapView.annotations;
if ([existingpoints count] > 0)
[mapView removeAnnotations:existingpoints];
更新:
也尝试此代码...
for (int i =0; i < [mapView.annotations count]; i++) {
if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyMapViewPoints class]]) {
[mapView removeAnnotation:[mapView.annotations objectAtIndex:i]];
}
}
关于iphone - 无法从 map View 中删除所有注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14117320/