Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3: How to remove the tracked polyline in Google map

I have draw the polylines, but I don't no that what coding can remove the tracked polyline in the google map. I'm trying some coding in below.


this is no working.

var polyline = GMSPolyline()
   polyline.map = nil 

this is working to remove tracked polyline, but when I'm maked the markers and tap it, marker can not sow the info window.

mapView.clear()

Thank you

like image 284
ShingHung Avatar asked Oct 16 '25 14:10

ShingHung


2 Answers

Solution:

  1. create polyline: teaching polyline by GoogleMapsAPIs
  2. create variable oldPolylineArr/oldPolyline type GMSPolyline/GMSPolyline_Array
  3. store the polyline to the oldPolyline
  4. remove the polyline

Sample Coding:

//Step 1:
var mapView = GMSMapView()
let path = GMSMutablePath()
path.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))
path.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.0))
path.add(CLLocationCoordinate2D(latitude: 37.45, longitude: -122.2))
path.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.2))
path.add(CLLocationCoordinate2D(latitude: 37.36, longitude: -122.0))
let rectangle = GMSPolyline(path: path)

//Step 2:
var oldPolylineArr = [GMSPolyline]()

Array

//Step 3:
oldPolyline.append(rectangle)

//Step 4:
for p in (0 ..< oldPolylineArr.count) {
    oldPolylineArr[p].map = nil
}

is NOT Array

//Step 3:
oldPolyline = polyline

//Step 4:
oldPolyline.map = nil
like image 98
ShingHung Avatar answered Oct 18 '25 22:10

ShingHung


Maybe a better solution: define your routePolyline as an optional variable then each time you want assign it a path, check it with if let... and set map to nil

if let routePolyline = routePolyline {
        routePolyline.map = nil
    }

// create new polyline
let path: GMSPath = GMSPath(fromEncodedPath: p)!
routePolyline = GMSPolyline(path: path)
routePolyline?.map = mapView
like image 35
Alirezak Avatar answered Oct 18 '25 23:10

Alirezak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!