ios - 如何使用多个航点swift4绘制路线
问题描述
我想使用开始位置和结束位置在多个点之间绘制路线,我正在按照一些示例和此链接获取 json 以获取航点 https://directionsdebug.firebaseapp.com/?origin=place_id%3AChIJ59dGBGBwTDkRlQsbxkBkqNw&destination=place_id%3AChIJ0TGAdgh6TDkRTFcRvOIXIOY&mode=驾驶&waypoints=Kotri%2C%20Jamshoro%2C%20Sindh%2C%20Pakistan&alternatives=true
这是我的代码,我没有使用链接获得所有积分请指导我如何使用起点和航点
func drawpath() {
let url = "https://maps.googleapis.com/maps/api/directions/json?origin=place_id%3AChIJ59dGBGBwTDkRlQsbxkBkqNw&destination=place_id%3AChIJ0TGAdgh6TDkRTFcRvOIXIOY&mode=driving&waypoints=Kotri%2C%20Jamshoro%2C%20Sindh%2C%20Pakistan&alternatives=true&key=AIzaSyCct09KdoyIc3VV5Bziw5Tk9MF0RhWXTNE"
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 = try! JSON(data: response.data!)
let routes = json["routes"][0]["legs"][0]["steps"].arrayValue
print("route is\(routes)")
// print route using Polyline
for route in routes
{
let routeOverviewPolyline = route["polyline"].dictionary
let points = routeOverviewPolyline?["points"]?.stringValue
print("ppoint\(String(describing: points))")
print("routeOverviewPolyline\(String(describing: routeOverviewPolyline))")
let path = GMSPath.init(fromEncodedPath: points!)
let polyline = GMSPolyline.init(path: path)
polyline.strokeWidth = 4
polyline.strokeColor = UIColor.red
polyline.map = self.mapview
}
}
解决方案
我在确定要创建的折线时发现错误,您应该使用响应中的“overview_polyline”对象
所以我对你所做的代码做了一些改动:
func drawpath() {
let url = "https://maps.googleapis.com/maps/api/directions/json?origin=place_id%3AChIJ59dGBGBwTDkRlQsbxkBkqNw&destination=place_id%3AChIJ0TGAdgh6TDkRTFcRvOIXIOY&mode=driving&waypoints=Kotri%2C%20Jamshoro%2C%20Sindh%2C%20Pakistan&alternatives=true&key=AIzaSyCct09KdoyIc3VV5Bziw5Tk9MF0RhWXTNE"
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 = try! JSON(data: response.data!)
let routes = json["routes"][0]["overview_polyline"]["points"].stringValue
let path = GMSPath.init(fromEncodedPath: routes)
let polyline = GMSPolyline.init(path: path)
polyline.strokeWidth = 4
polyline.strokeColor = UIColor.red
polyline.map = self.mapview
}
我得到的输出:
希望能有所帮助。
推荐阅读
- azure - 摩卡测试错误 - 在上下文中。
- javascript - 在第二次反应中渲染 swagger-editor 的问题
- javascript - 为什么这个 html 附加结构与数组结构不同
- google-cloud-platform - 云域注册限制通过组织政策
- nest-simulator - 如何在 Nest 模拟器中实现感受野编码?
- javascript - 在 div 中滚动部分时将活动类添加到菜单 li
- django - 使用 slug 或 PK 时,视图上下文的导航栏问题。(姜戈)
- javascript - 如何检查上传和下载的文件是否与 cypress 具有相同的内容?
- regex - 是否可以在提示中格式化我的 git 分支名称的部分颜色?
- react-native - React Native - 自定义组件中的模态未显示