问题
I am using GoogleMap-IOS-1.8.1 SDK for showing map. I have to draw may GMSPolyline on map. After on a particular event i have to remove only all GMSPolyline, so how can i do that...? As GoogleMaps/documentation/ios say to use two method to do it.
1. [mapView_ clear];
2. Set your GMSPolyline's map property to nil
Here 1st approach remove all Marker and Overlay as well. which is i don't want. And for 2nd one i don't think that it is good way to save all poliline's reference. and then set it to nil. Is there any better way to do it....??
Here what i would like to do.
for (GMSPolyline *polylineToremove in mapView_.polyline)
{
[mapView_ removeOverlay:overlayToRemove];
}
回答1:
You do need to do as you've said - store a reference to all of the polylines you've added (eg in an array), and then loop over them and set their map
property to nil.
回答2:
You just need to set GMSPolyline map property to nil.
GMSPolyline *polyline;
polyline.map = nil;
回答3:
This is code you need to remove any overlayView from GMSMapView. You can also do it with GMSMarkers, GMSPolyline.
for (GMSPolyline *polylineToRemove in arrPolylineAdded){
polylineToRemove.map = nil;
polylineToRemove = nil;
}
I have just checked :) for Google Map SDK Version 1.9.2.
回答4:
Just use below properly of Google Map:
mapView.clear()
Clears all markup that has been added to the map, including markers, polylines and ground
回答5:
Using Swift 3; Using this function first remove all polyline
import GoogleMaps
var polylineArray = [GMSPolyline]()
for root: GMSPolyline in self.polylineArray
{
if root.userData as! String == "root"
{
root.map = nil
}
}
then drow again the polyline
func showPath(polyStr :String)
{
let path = GMSPath(fromEncodedPath: polyStr)
DispatchQueue.main.async
{
let polyline = GMSPolyline(path: path)
//MARK: remove the old polyline from the GoogleMap
for root: GMSPolyline in self.polylineArray {
if root.userData as! String == "root" {
root.map = nil
}
}
polyline.strokeWidth = 2.0
polyline.strokeColor = sDefaultViewColorPrimaryDark
polyline.userData = "root"
polyline.map = self.mapView
let bounds = GMSCoordinateBounds(path: path!)
self.mapView!.animate(with: GMSCameraUpdate.fit(bounds,withPadding: 15.0))
self.polylineArray.append(polyline)
//self.mapView!.moveCamera(update)
}
}
来源:https://stackoverflow.com/questions/25102778/remove-gmspolyline-from-gmsmapview