Remove markers from google maps iOS

jchri853 picture jchri853 · Apr 17, 2013 · Viewed 35.3k times · Source

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

Answer

Bassant Ashraf picture Bassant Ashraf · Mar 14, 2016

To remove all markers

mapView.clear()

To remove a specific marker

myMarker.map = nil