Search code examples
iosobjective-cgoogle-maps-markersgoogle-maps-sdk-iosgmsmapview

how to remove particular markers from GMS?


I asked earlier how to show different markerInfoWindow in this question, and now I'm trying to delete a particular marker when the user clicks on the button on the left corner.

first in .h file :

NSMutableArray *ADSMarray;
GMSMarker *adsMarker;

Then I created Ads marker like this:

    for (int l=0 ; l<self.ADS.count; l++) {
    CLLocationCoordinate2D pos = CLLocationCoordinate2DMake([[[self.ADS objectAtIndex:l] objectForKey:@"lati"] doubleValue],[[[self.ADS objectAtIndex:l] objectForKey:@"longi"] doubleValue]);
    NSLog(@"Ads:: %f",[[[self.ADS objectAtIndex:l] objectForKey:@"longi"] doubleValue]);
    adsMarker = [[GMSMarker alloc]init];
    adsMarker.position=pos;
    //marker.infoWindowAnchor = CGPointMake(0.44f, 0.45f);
    adsMarker.draggable = NO;
    adsMarker.appearAnimation=YES;
    NSMutableArray*tempArray = [[NSMutableArray
                                 alloc] init];
    [tempArray addObject:@"ADS"];
    [tempArray addObject:[self.ADS objectAtIndex:l]];

    adsMarker.userData = tempArray;
    adsMarker.map = mapView_;
    adsMarker.icon=[GMSMarker markerImageWithColor:[UIColor blueColor]];

}

then in IBAction to remove them I wrote:

for (int i =0; i<self.ADS.count; i++) {
       // adsMarker.map = nil;
        [adsMarker setMap:nil];
    }

Hi


Solution

  • When you add a marker store a reference to it. Then when you want to remove it, set its map property to nil - that will remove it from the map.