【发布时间】:2020-07-31 12:44:36
【问题描述】:
我只想删除用户在路线上通过的折线
这些方法用于绘制运行平稳的折线
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,想知道是否有人可以告诉我这是否可行以及如何实现?
【问题讨论】: