'Remove GMSPolyline from GMSMapView
I am using GoogleMap-IOS-1.8.1 SDK for displaying a map. I have to draw a GMSPolyline on the map. After a particular event I have to remove all GMSPolyline paths except for markers. I'm not sure of the best way to do that. The GoogleMaps documentation for iOS describe two methods to sue.
1. [mapView_ clear];
2. Set the GMSPolyline map property to nil
Here the first approach removes all Markers and Overlays as well. This isn't exactly what I want. And for 2nd one it doesn't seem like the best method to save all of the GMSPolyline object references and then go back and set them all to nil.
Is there a better way to do accomplish this task, or is this the only proper / correct way to do this?
I was hoping for something like the following.
for (GMSPolyline *polylineToremove in mapView_.polyline)
{
[mapView_ removeOverlay:overlayToRemove];
}
Solution 1:[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.
Solution 2:[2]
You just need to set GMSPolyline map property to nil.
GMSPolyline *polyline;
polyline.map = nil;
Solution 3:[3]
Just use below properly of Google Map:
mapView.clear()
Clears all markup that has been added to the map, including markers, polylines and ground
Solution 4:[4]
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.
Solution 5:[5]
Using Swift 3; Using this function first remove all polyline
import GoogleMaps
var polylineArray = [GMSPolyline]()
override func viewDidLoad() {
super.viewDidLoad()
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)
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Saxon Druce |
| Solution 2 | darshan |
| Solution 3 | iVarun |
| Solution 4 | Linh Nguyen |
| Solution 5 |
