How can I remove the GMSPolyline in Swift 3

冷暖自知 提交于 2019-12-11 08:26:50

问题


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
}

回答1:


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
}



回答2:


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!




回答3:


   for poll in mapkit.overlays {

            mapkit.removeOverlay(poll)
        }


来源:https://stackoverflow.com/questions/44393200/how-can-i-remove-the-gmspolyline-in-swift-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!