Question

I am integrating google maps sdk. Its all work fine. But how to remove particular Marker(Pin Point) when second will appear.(I am not using Mapkit)

I want the following:

If i tap on map then one marker pin is generate at that location now if i tap on another location on map then two pins are displayed but i want to remove the old marker pin.

I also use,

[self.mapView clear];

But it was clear all other marker points from GMSMapview.

Following is the code to add pin on Map:

            GMSMapView *mapView;
            GMSMarker *currLocMarker = [[GMSMarker alloc] init];
            currLocMarker.map  = nil;
            [currLocMarker setTitle:NSLocalizedString(@"current_location_title", nil)];
            currLocMarker.icon = [UIImage imageNamed:@"pin_fetch_location.png"];
            currLocMarker.position = CLLocationCoordinate2DMake(pCoordinate.latitude, pCoordinate.longitude);
            currLocMarker.map = self.mapView;

Please help me to solve out this stuff..!!

Thanks in advance..:)

Was it helpful?

Solution 6

Yes, I got that solution. Add pin like the following:

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinates {

pCoordinate.latitude =coordinates.latitude;
pCoordinate.longitude =coordinates.longitude;

[[GMSGeocoder geocoder] reverseGeocodeCoordinate:CLLocationCoordinate2DMake(coordinates.latitude, coordinates.longitude) completionHandler:^(GMSReverseGeocodeResponse *resp, NSError *error)
                {
     [currLocMarker setTitle:NSLocalizedString(@"current_location_title", nil)];
     currLocMarker.icon = [UIImage imageNamed:@"pin.png"];
     currLocMarker.position = CLLocationCoordinate2DMake(coordinates.latitude,       coordinates.longitude);
     currLocMarker.map = self.mapView;} ] ;}

Please remove the following line if you used in the above:

GMSMarker *currLocMarker = [[GMSMarker alloc] init];

OTHER TIPS

To remove a particular pin from GMSMapView keep reference of pin (if there are multiple then use array) then use this code

currLocMarker.map  = nil;

To remove all things including pins poly lines from GMSMapView use this code

[ _mapView clear];

I made like this:

GMSMarker *myMarker;

- (void)mapView:(GMSMapView *)mapView didTapAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        if (myMarker) {
            myMarker.map = nil;
            myMarker = nil;
        }
        myMarker = [[GMSMarker alloc] init];
        myMarker.position = CLLocationCoordinate2DMake(coordinate.latitude, coordinate.longitude);
        myMarker.title = @"title";
        myMarker.map = mapView_;
    }];
}

and worked well for me !

This worked for me -

func removeMarkers(mapView: GMSMapView){
    for (index, _) in markers.enumerate() {
        //print("Item \(index): \(element)")
                    self.markers[index].map = nil
    }
}

where

    var markers = [GMSMarker]()

markers contains all the marker overlays for the mapView

Check This one and try it in your code

Remove a marker in Google Maps sdk

loop all marker in the map , and you can use title or snippet to decide which marker you remove

as map.markers is no longer to use in google map ios sdk , you need to have a nsmutablearray to store all marker for looping purpose

and you can make use of userData of the marker , marker.userData , which i prefer to store a nsdictionary information in the marker in order to prevent from duplicate name of title .

cheers.

When you tap on specific marker this will remove that marker

- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {

    marker.map = nil;
    return YES;
}

swift 5

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool
{
    let alertcontrolserver = UIAlertController.init(title : nil, message : "Are you sure you want to Remove ! ", preferredStyle: .alert)
        let okbtn = UIAlertAction(title: "Yes", style: .default, handler: { UIAlertAction in  marker.map = nil
             })
    let cancelbtn = UIAlertAction(title: "No", style: .default, handler: nil)
        alertcontrolserver.addAction(okbtn)
       alertcontrolserver.addAction(cancelbtn)
        self.present(alertcontrolserver, animated: true, completion: nil)
    return true
}

In case you have different markers, and you want to remove only specific markers from the map, then you have to hold that marker(s) object.

say if you have 
var removableMarkers: [GMSMarker]?

you have to append those markers in the above array when adding markers to map

Now, when you want to remove those markers:
  _ = self.removableMarkers.map({
            $0.map = nil
        })
self.RemovableMarkers = []

That's it!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top