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:
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];
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];
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.
Check This one and try it in your code
Remove a marker in Google Maps sdk
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
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
}