我目前正在学习 Swift 的 Google Maps API,因此我有几个问题希望你们能回答。碰巧我正在尝试创建一个简单地为用户提供方向的应用程序,就像苹果地图和谷歌地图一样,它只是为用户绘制一条方向路线。我已经成功地完成了这项工作,但我正在努力解决的问题 - 是简单地“更新”方向,而不是每次用户位置更新时不断“创建”新的方向路线。为了做到这一点,我将如何进行?
这是修改后的代码,我在 stackOverflow 上找到了其他地方,我目前正在使用它来绘制方向:
func drawPath(startLocation: CLLocation, endLocation: CLLocation)
{
let origin = "\(startLocation.coordinate.latitude),\(startLocation.coordinate.longitude)"
let destination = "\(endLocation.coordinate.latitude),\(endLocation.coordinate.longitude)"
let url = "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)&mode=driving"
Alamofire.request(url).responseJSON { response in
print(response.request as Any) // original URL request
print(response.response as Any) // HTTP URL response
print(response.data as Any) // server data
print(response.result as Any) // result of response serialization
let json = JSON(data: response.data!)
let routes = json["routes"].arrayValue
// print route using Polyline
for route in routes
{
let routeOverviewPolyline = route["overview_polyline"].dictionary
let points = routeOverviewPolyline?["points"]?.stringValue
let path = GMSPath.init(fromEncodedPath: points!)
let polyline = GMSPolyline.init(path: path)
polyline.strokeWidth = 4
polyline.strokeColor = UIColor.red
polyline.map = self.mapView
}
}
}
然而,我正在努力解决的是使其“活动” - 并根据用户当前位置更新方向。换句话说,避免在用户“身后”留下不再需要的标记距离。有任何想法吗?而且,是否也无法获取“到达目的地的时间”和“到达目的地的距离”等信息?或者这是我必须做的事情,提供指向 Google 地图应用程序的直接链接?
提前致谢!
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)