iphone - 无法从 map View 中删除所有注释

标签 iphone ios xcode annotations mkmapview

从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/

相关文章:

iphone - 当显示 UIViewController 时如何使用键盘来编辑字段?

IOS 在多个表行中使用相同的 View /引用

jquery - Phonegap 应用程序 Jquery 页面滚动在 iphone 5 中不起作用

iphone - 未调用 didSelectRowAtIndexPath。 UITableViewController 子类化

javascript - Vue SPA 中的视频无法在 iPhone 上自动播放/加载

iphone - UITabBarItem 图像颜色为灰色,而原始图像为白色

ios - 我的 ViewController Subview Delegate 做错了什么?

ios - Xcode 的 "Target Membership"是否与 Bundle 相关?

ios - Swift:如何根据当天的当前时间显示两个图像之一

ios - XCode - EXC_BAD_ACCESS。 -[非A型保留]