1

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

ale
  • 6,369
  • 7
  • 55
  • 65
AsimNet
  • 61
  • 1
  • 9

3 Answers3

2

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.

Saxon Druce
  • 17,406
  • 5
  • 50
  • 71
  • If you need to remove multiple markers, then you'll need to store them in an array (or maybe three arrays - one for each of the three colours). – Saxon Druce Jun 28 '14 at 13:05
  • i already did !, but how to remove them ? should i make that array == nil ? – AsimNet Jun 28 '14 at 13:36
  • Can you edit your question to include the code that you use to add the markers, and the code you use to remove them? – Saxon Druce Jun 28 '14 at 16:43
  • Hi AsimNet, you're assigning all of your markers to adsMarker, which means after the end of the loop it will be pointing to the last marker. Instead, add each marker to ADSMArray. Then in your loop, get the markers out of ADSMArray. – Saxon Druce Jun 29 '14 at 10:09
  • sorry Saxon Druce , i didn't understand your answer . i was trying several ways as you suggested , but i didn't understand this part specifically "Then in your loop, get the markers out of ADSMArray".could you please explain it with some code ?.Thank you – AsimNet Jul 01 '14 at 01:31
2

if you want to remove all markers in MapView you can use clear method that already built in GSM ..

Example:

[self.mapView clear];

link: Remove a marker

and if you want to remove all markers with specific color you can use this code if the user click on blue markers button :

NSArray *blueMarkers = @[ markerBlue1, markerBlue2 ];
NSArray *greenMarkers = @[ markerGreen1, markerGreen2 ];
NSArray *purpleMarkers = @[ markerPurple1, markerPurple2 ];
for (GMSMarker *marker in blueMarkers ){
     marker.map = nil;
}
Saleh AlDhobaie
  • 311
  • 4
  • 9
0

To remove all markers

mapView.clear()

To remove a specific marker

myMarker.map = nil