26
votes

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];
}
7

7 Answers

15
votes

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.

56
votes

To remove all markers

mapView.clear()

To remove a specific marker

myMarker.map = nil
35
votes

To remove all markers simple do:

[self.mapView clear];
6
votes

mapView.clear()

// It will clear all markers from GMSMapView.

1
votes

To remove a single marker

yourMarkerName.map = nil

To remove all markers

yourMapViewName.clear()

1
votes

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.

0
votes

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 .