gpt4 book ai didi

ios - 使用地址通过谷歌地图创建路线(没有经纬度)

转载 作者:行者123 更新时间:2023-11-28 08:00:04 25 4
gpt4 key购买 nike

如何使用 addressgoogle maps 创建路线?不使用 latitudelongitude,因为在我的 firebase 中我不使用纬度和经度。我只使用完整地址。

示例:John F.Kennedy 国际机场、Van Wyck 高速公路、牙买加、纽约

我使用谷歌地图构建路线的代码:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

if (control as? UIButton)?.buttonType == UIButtonType.detailDisclosure {

mapView.deselectAnnotation(view.annotation, animated: false)

} else if (control as? UIButton)?.buttonType == UIButtonType.custom {

mapView.deselectAnnotation(view.annotation, animated: false)

let alertController = UIAlertController(title: "Выберите навигатор для построение маршрута", message: nil, preferredStyle: .actionSheet)

let googleMaps = UIAlertAction(title: "Google Maps", style: .default, handler: { (action: UIAlertAction) in

if (UIApplication.shared.canOpenURL(URL(string: "comgooglemaps://")!)) {

let googleUrl = URL(string: "comgooglemaps://?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York&directionsmode=driving")!

UIApplication.shared.open(googleUrl, options: [:], completionHandler: nil)

} else {

let itunesGoogleMaps = URL(string: "https://itunes.apple.com/us/app/google-maps-gps-navigation/id585027354?mt=8")

UIApplication.shared.open(itunesGoogleMaps!, options: [:], completionHandler: nil)

}

})

let cancel = UIAlertAction(title: "Отмена", style: .cancel, handler: nil)

alertController.addAction(googleMaps)
alertController.addAction(cancel)

self.present(alertController, animated: true, completion: nil)

}
}

我使用变量firebase获取地址并构建路由:

var studioInfoFromDetail: Hall?

studioInfoFromDetail?.studioAddress

这个变量rus 语言 上有地址。

示例:

Москва, ул. Правды д.24, строение 3 英文会这样 Moscow, Pravdy street, home 24, building 1

Москва, ул.Электрозаводская, д.21 英文会这样 Moscow, Elektrozavodska street, home 21

那么我该如何放置在这个变量中

let googleUrl = URL(string: "comgooglemaps://?daddr=John+F.+Kennedy+International+Airport,+Van+Wyck+Expressway,+Jamaica,+New+York&directionsmode=driving")! 

我来自 firebase地址

我试着这样写:

let googleUrl = URL(string: "comgooglemaps://?daddr=\(self.studioInfoFromDetail?.studioAddress)&directionsmode=driving")!

但是我得到一个零:(

最佳答案

我认为您需要转义 url 字符串的非 ASCII 部分。调用方法 addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) 在你的 studioAddress在插入 url 字符串之前。

更新:

由于您使用的是 String插值中的可选(又名 String?/Optional<String> )(请参阅我在评论中提到的 this question):

let str = "comgooglemaps://?daddr=\(self.studioInfoFromDetail?.studioAddress)&directionsmode=driving"

您将获得:

"comgooglemaps://?daddr=\Optional(<... your string ...>)&directionsmode=driving"

因此您需要在使用前展开可选的。

if let address = self.studioInfoFromDetail?.studioAddress {
let str = "comgooglemaps://?daddr=\(address)&directionsmode=driving"
}

您也可以强制解包(使用运算符 ! ),但我不建议这样做。

关于ios - 使用地址通过谷歌地图创建路线(没有经纬度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46904409/

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