How can I remove the GMSPolyline in Swift 3

2019-07-28 12:15发布

I'm making the GMSPolyline in the GoogleMap. When I want to remove the GMSPolyline by coding polyline.map = nil, but it can not working for me. I'm copy some coding in the below.

(added the marker before, I need remove the polyline only)

Thank you for your help!!!

my coding:

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    //MARK: create the GMSPolyline
    let polyline = GMSPolyline()
    //MARK: remove the old polyline from the GoogleMap 
    polyline.map = nil

    let origin = "\(mapView.myLocation!.coordinate.latitude),\(mapView.myLocation!.coordinate.longitude)"
    let destination = "\(marker.position.latitude),\(marker.position.longitude)"

    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving"

    Alamofire.request(url).responseJSON { response in

        let json = JSON(data: response.data!)
        let routes = json["routes"].arrayValue

        //MARK: print route using Polyline
        for route in routes
        {
            let routeOverviewPolyline = route["overview_polyline"].dictionary
            let points = routeOverviewPolyline?["points"]?.stringValue
            let path = GMSPath(fromEncodedPath: points!)
            polyline = GMSPolyline(path: path)
            polyline.strokeWidth = 4
            polyline.strokeColor = UIColor.yellow
            polyline.isTappable = true
            polyline.map = self.mapView
        }
    }
    return false
}

3条回答
Evening l夕情丶
2楼-- · 2019-07-28 12:39

Declare polyline outside the function as a class property.

var routePolyline: GMSPolyline? = nil

Then to clear old polyline before adding new one, use-

self.routePolyline?.map = nil

This will work as expected!

查看更多
\"骚年 ilove
3楼-- · 2019-07-28 12:43
   for poll in mapkit.overlays {

            mapkit.removeOverlay(poll)
        }
查看更多
地球回转人心会变
4楼-- · 2019-07-28 13:06

you should clear the map before adding new polyline on the map.

clear() function of GMSMapView

/**

Clears all markup that has been added to the map, including markers, polylines and ground overlays.
This will not clear the visible location dot or reset the current mapType.

*/

Try this

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {
    //MARK: create the GMSPolyline
    let polyline = GMSPolyline()
    //MARK: remove the old polyline from the GoogleMap 
    mapView.clear()
    //TODO:- Redraw all marker here.
    let origin = "\(mapView.myLocation!.coordinate.latitude),\(mapView.myLocation!.coordinate.longitude)"
    let destination = "\(marker.position.latitude),\(marker.position.longitude)"

    let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving"

    Alamofire.request(url).responseJSON { response in

        let json = JSON(data: response.data!)
        let routes = json["routes"].arrayValue

        //MARK: print route using Polyline
        for route in routes
        {
            let routeOverviewPolyline = route["overview_polyline"].dictionary
            let points = routeOverviewPolyline?["points"]?.stringValue
            let path = GMSPath(fromEncodedPath: points!)
            polyline = GMSPolyline(path: path)
            polyline.strokeWidth = 4
            polyline.strokeColor = UIColor.yellow
            polyline.isTappable = true
            polyline.map = self.mapView
        }
    }
    return false
}
查看更多
登录 后发表回答