如何从googlemap中删除所有标记而不是mapview.clear(ios objective-c)

时间:2017-10-18 05:54:45

标签: ios google-maps google-maps-markers

我正在尝试从Google地图中移除现有的所有标记,我们可以通过map.clear执行,但我不想删除地图上的所有内容(折线,多边形),我只想删除标记

我正在根据数组计数

创建标记
-(void)annotationCreationForCoordinatesOfArray:(NSMutableArray *)array
{
     for (int i=0; i<array.count; i++)
    {
        CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[[array objectAtIndex:i] objectForKey:@"latitude"] doubleValue], [[[array objectAtIndex:i] objectForKey:@"longitude"] doubleValue]);

        mark = [GMSMarker markerWithPosition:position];

        NSString *annoNumber = [NSString stringWithFormat:@"%i",i];
        mark.title = annoNumber;
        mark.map = _mapView;
        mark.tracksViewChanges = YES;
        mark.draggable = YES;
        mark.icon = [UIImage imageNamed:@"Mappin.png"];

    }
}

1 个答案:

答案 0 :(得分:0)

试试这段代码吧。

创建全局数组

NSMutableArray *removalMarkerArray;

现在在全局数组中添加所有标记

   removalMarkerArray=[[NSMutableArray alloc]init];

 -(void)annotationCreationForCoordinatesOfArray:(NSMutableArray *)array{

    for (int i=0; i<array.count; i++){

    CLLocationCoordinate2D position = CLLocationCoordinate2DMake([[[array objectAtIndex:i] objectForKey:@"latitude"] doubleValue], [[[array objectAtIndex:i] objectForKey:@"longitude"] doubleValue]);

    mark = [GMSMarker markerWithPosition:position];

    NSString *annoNumber = [NSString stringWithFormat:@"%i",i];
    mark.title = annoNumber;
    mark.map = _mapView;
    mark.tracksViewChanges = YES;
    mark.draggable = YES;
    mark.icon = [UIImage imageNamed:@"Mappin.png"];

    [removalMarkerArray addObject:mark];

     }
  }

然后在哪里清除所有标记

  for (GMSMarker *marker in removalMarkerArray ){
        marker.map = nil;
   }