如何删除 Swift 中的 GMSPolyline 3

How can I remove the GMSPolyline in Swift 3

我正在 GoogleMap 中制作 GMSPolyline。当我想通过编码 polyline.map = nil 删除 GMSPolyline 时,它​​对我不起作用。我在下面复制了一些代码。

(之前添加了标记,我只需要删除折线)

感谢您的帮助!!!

我的编码:

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
}

在地图上添加新折线之前,您应该清除地图。

GMSMapView的clear()函数

/**

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.

*/

试试这个

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
}

在函数外声明折线为 class 属性.

var routePolyline: GMSPolyline? = nil

然后在添加新的之前清除旧的多段线,使用-

self.routePolyline?.map = nil

这将按预期工作!

   for poll in mapkit.overlays {

            mapkit.removeOverlay(poll)
        }
 var polyline: GMSPolyline?

 func drawPathOnMap()  {
        if polyline != nil {
          //remove existing the gmspoly line from your map
             polyline!.map = nil
        }

        let points = //Your Point
        let path = GMSPath(fromEncodedPath: points)
        polyline = GMSPolyline(path: path)
        polyline?.strokeWidth = 5.0
        polyline?.strokeColor = UIColor.init(rgb: 0x7688ED)
        polyline!.map = mapView
 }