gpt4 book ai didi

ios - 我们应该如何使用 Swift 在谷歌地图中显示两个纬度和经度之间的路径

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:32:42 25 4
gpt4 key购买 nike

在这里,我有源纬度和经度以同样的方式我也有目的地纬度和经度现在,我想显示这两个纬度和经度之间的路径

sourcelatitude = 12.9077869892472
sourcelongitude = 77.5870421156287

print(sourcelatitude!)
print(sourcelongitude!)

destinationlatitude = 12.907809
destinationlongitude = 77.587066

print(destinationlatitude!)
print(destinationlongitude!)

谁能帮我解决这个问题

最佳答案

试试这个

let origin = "\(12.9077869892472),\(77.5870421156287)"
let destination = "\(12.907809),\(77.587066)"
let url = URL(string: "https://maps.googleapis.com/maps/api/directions/json?origin=\(origin)&destination=\(destination)")
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"]
let path = GMSPath.init(fromEncodedPath: points!)
let polyline = GMSPolyline(path: path)
polyline.strokeColor = .blue
polyline.strokeWidth = 4.0
polyline.map = mapViewX//Your GMSMapview
}
})
}
}catch let error as NSError{
print(error)
}
}
}).resume()

关于ios - 我们应该如何使用 Swift 在谷歌地图中显示两个纬度和经度之间的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44679616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com