jchri853
jchri853

Reputation: 354

Remove markers from google maps iOS

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

Upvotes: 28

Views: 42884

Answers (7)

To remove a single marker from map.

yourMarkerName.map = nil

To remove all markers from map.

yourMapViewName.clear()

Upvotes: 2

Leang Socheat
Leang Socheat

Reputation: 1174

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.

Upvotes: 1

maryam chaharsooghi
maryam chaharsooghi

Reputation: 54

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 .

Upvotes: 0

Hitesh Chauhan
Hitesh Chauhan

Reputation: 1540

mapView.clear()

// It will clear all markers from GMSMapView.

Upvotes: 7

Bassant Ashraf
Bassant Ashraf

Reputation: 1609

To remove all markers

mapView.clear()

To remove a specific marker

myMarker.map = nil

Upvotes: 61

jturolla
jturolla

Reputation: 6746

To remove all markers simple do:

[self.mapView clear];

Upvotes: 36

iOSGuru248
iOSGuru248

Reputation: 366

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.

Upvotes: 18

Related Questions