尝试在用户位置到定义位置之间指定方向线。精确定位是正确的,准确地在所需的位置,但没有“线”方向引导用户位置到定义的位置。
就像这样:
以及绘制地图和解析google maps API JSON的代码:
func DrawDirection(url: String, cord: CLLocationCoordinate2D){
request(url).responseJSON { response in
let parsed = try? JSONSerialization.jsonObject(with: response.data!, options: []) as! [String:Any]
let routes = parsed!["routes"] as! [[String:Any]]
for route in routes {
let dictArr = route["legs"] as? [[String:Any]]
let dict = dictArr![0]["steps"] as? [[String:Any]]
let start = dict![0]["start_location"] as? [String:Any]
let lat = start!["lat"] as! Double
let long = start!["lng"] as! Double
let dotcoordinate = CLLocationCoordinate2D(latitude: CLLocationDegrees(lat), longitude: CLLocationDegrees(long))
let routePoly = route["overview_polyline"] as! [String:Any]
let points = routePoly["points"] as! String
let line = points
DispatchQueue.main.async {
let bounds = GMSCoordinateBounds(coordinate: cord, coordinate: dotcoordinate)
let update = GMSCameraUpdate.fit(bounds, with: UIEdgeInsetsMake(170, 30, 30, 30))
self.CinemaLoc.moveCamera(update)
}
self.AddingPolyLine(encString: line, dir: cord, dot: dotcoordinate)
}
}
}
func AddingPolyLine(encString: String, dir: CLLocationCoordinate2D, dot: CLLocationCoordinate2D){
let dotpath: GMSMutablePath = GMSMutablePath()
dotpath.add(CLLocationCoordinate2DMake(dir.latitude, dir.longitude)) //original loc
dotpath.add(CLLocationCoordinate2DMake(dot.latitude, dot.longitude)) //starting points
let dotpoly = GMSPolyline(path: dotpath)
dotpoly.map = self.CinemaLoc
dotpoly.strokeWidth = 3.0
let styles:[Any] = [GMSStrokeStyle.solidColor(.blue), GMSStrokeStyle.solidColor(.clear)]
let lengths:[Any] = [10,5]
dotpoly.spans = GMSStyleSpans(dotpoly.path!,styles as!
[GMSStrokeStyle], lengths as! [NSNumber], GMSLengthKind.rhumb)
let poly = GMSPolyline(path: dotpath)
poly.strokeWidth = 3.0
poly.strokeColor = .red
poly.map = self.CinemaLoc
}
cord
是用户位置一直在尝试所有可能的堆栈溢出方法,但没有得到任何更改,有时有日志说“未能在路径~/GoogleMaps.bundle/GMSCacheStorage.momd/加载优化模型。”
我的密码有问题吗?
最佳答案
只应为特定路径创建一次路径,然后可以向路径添加坐标。把let dotpath: GMSMutablePath = GMSMutablePath()
放在方法调用中是错误的,每个循环都会调用它。
关于ios - map 方向线未显示在 map 上,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49767339/