使用地址创建带有 google 地图的路线(没有纬度和经度)
Use address to create route with google maps (without latitude and longitude)
如何使用 地址 来创建带有 google 地图 的路线?不使用 latitude 和 longitude,因为在我的 firebase 中我不使用纬度和经度。我只使用 完整地址。
示例:John F.Kennedy 国际机场,Van Wyck 高速公路,牙买加,纽约。
我用 google 地图构建路线的代码:
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 部分。在插入 url 字符串之前调用 studioAddress
上的方法 addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
。
更新:
由于您在插值中使用了 String
可选(又名 String?
/ Optional<String>
)(参见我在评论中提到的 ):
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"
}
您也可以强制解包(使用运算符 !
),但我不建议这样做。
我试试这个,它对我有帮助
let googleMaps = UIAlertAction(title: "Google Maps", style: .default, handler: { (action: UIAlertAction) in
if (UIApplication.shared.canOpenURL(URL(string: "comgooglemaps://")!)) {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString((self.studioInfoFromDetail?.studioAddress)!, completionHandler: { (placemarks, error) in
guard error == nil else { return }
guard let placemarks = placemarks else { return }
let placemark = placemarks.first
let longitude = placemark?.location?.coordinate.longitude
let latitude = placemark?.location?.coordinate.latitude
print("lat: \(latitude!), lon: \(longitude!)")
let googleUrl = URL(string: "comgooglemaps://?daddr=\(latitude!),\(longitude!)&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)
}
})
}
如何使用 地址 来创建带有 google 地图 的路线?不使用 latitude 和 longitude,因为在我的 firebase 中我不使用纬度和经度。我只使用 完整地址。
示例:John F.Kennedy 国际机场,Van Wyck 高速公路,牙买加,纽约。
我用 google 地图构建路线的代码:
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 部分。在插入 url 字符串之前调用 studioAddress
上的方法 addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
。
更新:
由于您在插值中使用了 String
可选(又名 String?
/ Optional<String>
)(参见我在评论中提到的
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"
}
您也可以强制解包(使用运算符 !
),但我不建议这样做。
我试试这个,它对我有帮助
let googleMaps = UIAlertAction(title: "Google Maps", style: .default, handler: { (action: UIAlertAction) in
if (UIApplication.shared.canOpenURL(URL(string: "comgooglemaps://")!)) {
let geocoder = CLGeocoder()
geocoder.geocodeAddressString((self.studioInfoFromDetail?.studioAddress)!, completionHandler: { (placemarks, error) in
guard error == nil else { return }
guard let placemarks = placemarks else { return }
let placemark = placemarks.first
let longitude = placemark?.location?.coordinate.longitude
let latitude = placemark?.location?.coordinate.latitude
print("lat: \(latitude!), lon: \(longitude!)")
let googleUrl = URL(string: "comgooglemaps://?daddr=\(latitude!),\(longitude!)&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)
}
})
}