简体   繁体   English

无法从mapview中删除所有注释

[英]Not able to remove all Annotation from mapview

I am getting problem while removing annotations from MKMapView. 从MKMapView中删除注释时遇到问题。 I have already search for same and found so many answers but couldn't find satisfactory ans. 我已经搜索过相同的内容并找到了很多答案,但找不到令人满意的答案。 Below is my code summary: 以下是我的代码摘要:

I have created my custom class as MyMapViewPoints and created one function as 我已经将我的自定义类创建为MyMapViewPoints并创建了一个函数

- initWithZTitle:(NSString *)title andCoordinate:(CLLocationCoordinate2D)location

Whenever I want to add annotation I simply create one object of MyMapViewPoints and 每当我想添加注释时,我只需创建一个MyMapViewPoints对象

[mapView addAnnotation:newAnnotation];

When I want to remove all mapview points (annotations) I execute following code: 当我想删除所有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]];
    } 
}

But some of the annotation points remains on map. 但是一些注释点仍然在地图上。 If I have added six points and tried to remove all using above code 2 mapview points (annotations) retains. 如果我添加了6个点并试图删除所有使用上面的代码2 mapview点(注释)保留。 Any ideas? 有任何想法吗?

just try this code... 试试这个代码......

NSArray *existingpoints = mapView.annotations;
if ([existingpoints count] > 0)
    [mapView removeAnnotations:existingpoints];

UPDATE: 更新:

Also try this code... 也试试这个代码......

for (int i =0; i < [mapView.annotations count]; i++) { 
    if ([[mapView.annotations objectAtIndex:i] isKindOfClass:[MyMapViewPoints class]]) {                      
         [mapView removeAnnotation:[mapView.annotations objectAtIndex:i]]; 
       } 
}

将您的数据源数组添加到以下行并删除所有annoation

[yourMapview removeAnnotations:datasourceArray];

You're modifying an array whilst also trying to loop through it. 你正在修改一个数组,同时也试图循环它。 During the first iteration of that loop i=0 and if it matches the class you remove it from the annotations. 在该循环的第一次迭代期间, i=0 ,如果它与该类匹配,则将其从注释中删除。 If you remove the item at index 0, they all move up by one, so the one that was at index 1 is now at index 0. But you also increase i by one and during the next loop you look at index 1, completely missing the item that has now shifted to index 0. 如果删除索引0处的项目,它们都会向上移动一个,因此索引1处的项目现在处于索引0.但是您还将i增加1并且在下一个循环中您查看索引1,完全丢失现在已转移到索引0的项目。

index 0 1 2 3
item  A B C D

Check index 0, remove item at index 0. 检查索引0,删除索引0处的项目。

index 0 1 2
item  B C D

Now check index at 1 and you've skipped B . 现在检查索引为1 ,你跳过B。

You should try these solutions How to delete all Annotations on a MKMapView 您应该尝试这些解决方案如何删除MKMapView上的所有注释

Also [yourMapView annotations] doesn't promise to return them in any specific order so every time you call it the indexes could be different. 此外,[yourMapView注释]不承诺以任何特定顺序返回它们,因此每次调用它时索引都可能不同。 If you want to do any loop through the annotations you're best to save it as an NSArray* and refer to that copy from then on. 如果你想通过注释进行任何循环,最好将它保存为NSArray *并从那时起引用该副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM