如何在swift中使用谷歌地图方向绘制路线



我正在使用swift开发自己的iOS应用程序。我现在面临着谷歌地图方向的问题。我现在无法使用谷歌地图方向从两个坐标绘制路线。

下面显示了代码。

func drawRoute(from source: CLLocationCoordinate2D, to destination: CLLocationCoordinate2D) {

let url = "https://maps.googleapis.com/maps/api/directions/json?origin= (source.latitude),(source.longitude)&destination=(destination.latitude),(destination.longitude)&mode=driving&key=(GoogleMapKey.directionKey)"

self.showLoadingView()

print(url)

AF.request(url, encoding: JSONEncoding.default).responseJSON { response in
self.hideLoadingView()

switch response.result {
case .success(let value):
print("************")
print(JSON(value))
let jsonData = JSON(value)
let routes = jsonData["routes"].arrayValue

for route in routes {
let overview_polyline = route["overview_polyline"].dictionaryValue
let points = overview_polyline["points"]!.stringValue
self.drawPath(from: points)
ProgressHUD.showError("success")
}
case .failure(let error):
print(error.localizedDescription)
ProgressHUD.showError(error.localizedDescription)
}
}
}

你能帮我吗?感谢

我使用此方法创建路由。首先,它将尝试在谷歌地图应用程序中创建路线。如果用户没有谷歌地图应用程序,默认的地图应用程序将启动。

func openMaps(lat: Double, lng: Double) {
if let googleMapsUrl = URL(string: "comgooglemaps://?saddr=&daddr=(lat),(lng)&directionsmode=driving"), UIApplication.shared.canOpenURL(googleMapsUrl) {
UIApplication.shared.open(googleMapsUrl)
} else {
let placemark = MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lng), addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = "Target location"
mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving])
}
}

最新更新