26

I am building an iOS app using storyboards and Google Maps. Using iOS6

My application features the split view navigation as seen in the facebook app

On my left view I am selecting an item in a list which has lat/long cords and showing it on my map on the following method

- (void)viewWillAppear:(BOOL)animated

I would like to remove all markers in this method before I add another one (so only one marker is on the map), is there a way to do this? Below is my code to add a marker to the mapView

Thanks in advance - Jon

- (void)loadView
{
    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:poi.lat
                                                            longitude:poi.lon
                                                                 zoom:15];
    mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];

    mapView.myLocationEnabled = YES;
    self.view = mapView;
    mapView.mapType = kGMSTypeHybrid;

    //Allows you to tap a marker and have camera pan to it
    mapView.delegate = self;
}

-(void)viewWillAppear:(BOOL)animated
{
    GMSMarkerOptions *options = [[GMSMarkerOptions alloc] init];
    options.position = CLLocationCoordinate2DMake(poi.lat, poi.lon);
    options.title =  poi.title;
    options.snippet = poi.description;
    options.icon =  [UIImage imageNamed:@"flag-red.png"];
    [mapView addMarkerWithOptions:options];

    [mapView animateToLocation:options.position];
    [mapView animateToBearing:0];
    [mapView animateToViewingAngle:0];
}
Michael Dautermann
  • 86,557
  • 17
  • 155
  • 196
jchri853
  • 324
  • 1
  • 4
  • 11

7 Answers7

54

To remove all markers

mapView.clear()

To remove a specific marker

myMarker.map = nil
Bassant Ashraf
  • 1,271
  • 1
  • 13
  • 21
35

To remove all markers simple do:

[self.mapView clear];
jturolla
  • 5,837
  • 6
  • 24
  • 40
  • this remove markers and all items of map, better solution? – Jose Pose S Aug 29 '16 at 09:44
  • Note that this clears everything on the map. So if you have markers and maybe polylines, it'll also clear them as well. If you have like both you'll want to put all the markers in an array, loop through the array and clear each one. This will keep your polylines. – Micah Montoya Nov 10 '16 at 15:33
15

Please refer to the Google Map documentation: Google Maps SDK for iOS

Please refer to the section title "Remove a marker". Always check documentation for such methods.

iOSGuru248
  • 356
  • 2
  • 6
  • I am using a GMSMapView - Google maps Class. Unless you can apply those methods to google maps? – jchri853 Apr 17 '13 at 18:47
  • haha NP, Missed that in the documentation, thanks for taking the time to help me! – jchri853 Apr 17 '13 at 18:55
  • 6
    You should have answered that instead. – jturolla Jan 27 '14 at 18:08
  • 9
    Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – John Dvorak Jan 27 '14 at 18:40
  • @iOSGuru248 what if I only want remove marker not polyline. this will also remove path drawn? – Rushi trivedi Aug 23 '17 at 13:14
  • To remove a specific marker (keep track of all markers, why? because `map.markers` are not available anymore): `let markers = [GMSMarker](), markers.append(marker)` then `markers[targetIndex].map = nil` – Hassan Tareq Dec 17 '19 at 04:21
6

mapView.clear()

// It will clear all markers from GMSMapView.

Hitesh Chauhan
  • 1,340
  • 13
  • 15
1

To remove a single marker

yourMarkerName.map = nil

To remove all markers

yourMapViewName.clear()

0

mapView.clear() is not a good idea . because The Places SDK for iOS enforces a default limit of 1,000 requests per 24 hour period.(If your app exceeds the limit, the app will start failing. Verify your identity to get 150,000 requests per 24 hour period.) whit mapView.clear() the requests increase . the best way is clear each marker and polylines .

  • 2
    I don't think this is true. `mapView.clear()` should only have to do with the UI and it makes no sense that it does an additional request to the Maps API. – Vasil Garov Dec 26 '18 at 12:17
0

It's a little bit tricky here if you want to remove only one marker among a group of your markers.

 let marker = GMSMarker(position: coordinate) //
  marker.icon = UIImage(named: "ic_pin_marker")
  marker.map = mapView

  mapView.selectedMarker = marker //  the specific marker you want to remove or modify by set it as selectedMarker on the map.

then you want to remove or modify

mapView.selectedMarker.map = nil //example to remove the marker form the map.

Hopefully useful with your condition.

Leang Socheat
  • 976
  • 2
  • 10
  • 20