7

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];
}
LarryF
  • 4,925
  • 4
  • 32
  • 40
Gaurav Pandey
  • 1,953
  • 3
  • 23
  • 37

5 Answers5

4

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.

Saxon Druce
  • 17,406
  • 5
  • 50
  • 71
  • 1
    Actually i want to skip reference part. i don't want store reference if there is another way. There should be some property of MapView thorough which we can get all Type of overLay. – Gaurav Pandey Aug 03 '14 at 11:58
  • No, there isn't. There used to be, but you can see in the release notes at https://developers.google.com/maps/documentation/ios/releases that Google removed this in version 1.7. Now the recommended approach is to maintain your own reference of what you've added, if you want to be able to delete it later. – Saxon Druce Aug 03 '14 at 13:12
4

You just need to set GMSPolyline map property to nil.

GMSPolyline *polyline;
polyline.map = nil;
darshan
  • 1,115
  • 1
  • 14
  • 29
2

Just use below properly of Google Map:

mapView.clear()

Clears all markup that has been added to the map, including markers, polylines and ground

iVarun
  • 6,496
  • 2
  • 26
  • 34
  • 1
    above line clear mapview, due to that all markers with polyline also remove. when you want to remove all things like marker and polylline then clear map. And when you have to clear polyline then use below code polyline.map = nil – Protocol Jun 25 '18 at 12:51
  • @RahulFate Yes this line will clear all polylines on map. What you are looking for? – iVarun Jun 25 '18 at 12:53
0

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.

Linh Nguyen
  • 2,006
  • 1
  • 20
  • 16
-1

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)
    }
}
pansora abhay
  • 892
  • 10
  • 16