【发布时间】:2018-06-26 07:10:31
【问题描述】:
我正在使用 Google 地图 在起点和终点之间绘制路径。 它完美地工作,当用户旅行时,使用这个委托函数更新当前位置和绘制路线。
public func mapView(_ mapView: GMSMapView, didChange position: GMSCameraPosition) {
drawPath()
}
地图闪烁我正在使用此功能绘制路径
func drawPath()
{
let urlString = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving"
let url = URL(string: urlString)
URLSession.shared.dataTask(with: url!, completionHandler: {
(data, response, error) in
if(error != nil){
print("error")
}else{
DispatchQueue.main.async {
do{
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
let routes = json["routes"] as! NSArray
self.googleMapView?.clear()
for route in routes
{
let routeOverviewPolyline:NSDictionary = (route as! NSDictionary).value(forKey: "overview_polyline") as! NSDictionary
let points = routeOverviewPolyline.object(forKey: "points")
let path = GMSPath.init(fromEncodedPath: points! as! String)
let polyline = GMSPolyline.init(path: path)
polyline.strokeWidth = 8
polyline.strokeColor = #colorLiteral(red: 0.2420450151, green: 0.487836957, blue: 0.9020499587, alpha: 1)
let bounds = GMSCoordinateBounds(path: path!)
self.googleMapView!.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 30.0))
polyline.map = self.googleMapView
}
}catch let error as NSError{
print("error:\(error)")
}
self.addMarker(loc: desti)
}
}
}).resume()
}
在其他情况下,没有委托功能行进路线折线不清除,但当前位置更新。
谁能给个建议,谢谢。
【问题讨论】:
-
尝试在真实设备上检查。
-
是的,我正在检查真实设备并使用 gpx 文件进行测试。
-
@Leela 显示截图
-
是的,我添加了截图
标签: ios swift google-maps