Remove particular GMSMarker from GMSMapview using Google Map sdk in ios
Asked Answered
S

9

13

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..:)

Sawtoothed answered 18/1, 2014 at 6:30 Comment(3)
Please add your code snippet how you add Marker on map,than it will possible to understand.Egghead
I also use that but its can't work..Sawtoothed
@Sawtoothed did you find out the way? i also want to move my particular marker. how can i do this?Lexicography
S
0

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];
Sawtoothed answered 26/3, 2014 at 10:42 Comment(1)
Hello, I'm having a very similar problem to you, however I'm generating markers in a loop and want to remove those markers, not just one. I tried your implementation but it didn't seem to work. Can you possible look at my post? Thanks #26812420Champlain
W
28

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];
Weylin answered 21/1, 2015 at 6:19 Comment(0)
L
3

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

Longshore answered 6/1, 2016 at 4:13 Comment(1)
Hi, Why you receive a GMSMapView if you wouldn't use it?Singsong
N
2

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 !

Niple answered 27/1, 2015 at 13:3 Comment(0)
E
1

Check This one and try it in your code

Remove a marker in Google Maps sdk

Egghead answered 18/1, 2014 at 7:3 Comment(0)
P
1

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.

Peatroy answered 3/5, 2014 at 5:56 Comment(0)
S
0

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];
Sawtoothed answered 26/3, 2014 at 10:42 Comment(1)
Hello, I'm having a very similar problem to you, however I'm generating markers in a loop and want to remove those markers, not just one. I tried your implementation but it didn't seem to work. Can you possible look at my post? Thanks #26812420Champlain
D
0

When you tap on specific marker this will remove that marker

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

    marker.map = nil;
    return YES;
}
Duad answered 9/2, 2018 at 8:58 Comment(0)
M
0

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
}
Motto answered 26/11, 2019 at 10:19 Comment(1)
Welcome to Stack Overflow! Please don't answer just with source code. Try to provide a nice description about how your solution works. See: How do I write a good answer?. ThanksCoverture
N
0

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!

Noyade answered 29/9, 2020 at 20:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.