问题
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..:)
回答1:
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];
回答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 !
回答3:
Check This one and try it in your code
Remove a marker in Google Maps sdk
回答4:
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
回答5:
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];
回答6:
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.
回答7:
When you tap on specific marker this will remove that marker
- (BOOL)mapView:(GMSMapView *)mapView didTapMarker:(GMSMarker *)marker {
marker.map = nil;
return YES;
}
回答8:
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
}
来源:https://stackoverflow.com/questions/21200914/remove-particular-gmsmarker-from-gmsmapview-using-google-map-sdk-in-ios