首页 > 解决方案 > 如何删除 Traveled Polyline Google Maps SDK IOS

问题描述

我只想删除用户在路线上通过的折线

这些方法用于绘制运行平稳的折线

func drawPolygon(from source: CLLocationCoordinate2D, to destination: CLLocationCoordinate2D){
googleMaps.isHidden = false
guard let url = URL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=\(source.latitude),\(source.longitude)&destination=\(destination.latitude),\(destination.longitude)&sensor=false&mode=driving&key=KEYHERE") else {
    return
}

URLSession.shared.dataTask(with: url, completionHandler: {(data, response, error) in
    if(error != nil){
        print("error")
    }else{
        do{
            let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
            if json["status"] as! String == "OK"{
                let routes = json["routes"] as! [[String:AnyObject]]
                OperationQueue.main.addOperation({
                    for route in routes{
                        let routeOverviewPolyline = route["overview_polyline"] as! [String:String]
                        let points = routeOverviewPolyline["points"]
                        self.path = GMSPath.init(fromEncodedPath: points!)!
                        let polyline = GMSPolyline(path: self.path)
                        self.drawPath(polyline: polyline)
                    }
                })
            }
        }catch let error as NSError{
            print(error)
        }
    }
}).resume()
}

private func drawPath(polyline : GMSPolyline){

//DispatchQueue.main.async {
    
    polyline.strokeWidth = 6.0
    polyline.strokeColor = #colorLiteral(red: 0, green: 0.8465872407, blue: 0.7545004487, alpha: 1)
    polyline.map = self.googleMaps
    self.addMarker()
    // let bounds = GMSCoordinateBounds(path: path)
    // self.googleMaps.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
    let bounds = GMSCoordinateBounds(coordinate: self.fromLoc!, coordinate: self.toLoc!)
    let update = GMSCameraUpdate.fit(bounds, with: UIEdgeInsets(top: 170, left: 30, bottom: 30, right: 30))
    self.googleMaps.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 20.0))
    self.googleMaps.animate(toZoom: 10)
    self.googleMaps.animate(toViewingAngle: 30)
    self.googleMaps!.moveCamera(update)
  self.timer = Timer.scheduledTimer(timeInterval: 0.003, target: self, selector: #selector(animatePolylinePath), userInfo: nil, repeats: true)
    
//}}

路线和折线工作得非常好。当用户通过路线上的线以获得更好的体验时,我正在考虑删除折线。我正在使用 Swift 5 并想知道是否有人可以告诉我它是否可能以及如何?

标签: iosswiftmaps

解决方案


我正在回答我自己的问题 我解决了这个问题 在另一个帖子上查看我的答案

如何在谷歌地图中删除跟踪的折线


推荐阅读