【问题标题】:Use address to create route with google maps (without latitude and longitude)使用地址与谷歌地图创建路线(无经纬度)
【发布时间】:2017-10-24 07:17:06
【问题描述】:

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

示例:约翰·肯尼迪国际机场,范威克高速公路,牙买加,纽约

我用 google maps 构建路线的代码:

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)

    }
}

我使用 variablefirebase 获取地址并构建路由:

var studioInfoFromDetail: Hall?

studioInfoFromDetail?.studioAddress

这个变量rus language的地址。

例子:

Москва,ул。 Правды д.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")!

但我得到一个零:(

【问题讨论】:

    标签: ios swift google-maps street-address


    【解决方案1】:

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

    更新:

    由于您在插值中使用了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"
    }
    

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

    【讨论】:

    • 我试过let originalString = "\(self.studioInfoFromDetail!.studioAddress)" let escapingString = originalString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) print("string: \(escapingString!)") 得到这个string: %D0%9C%D0%BE%D1%81%D0%BA%D0%B2%D0%B0,%201-%D0%B9%20%D0%9A%D1%80%D0%B0%D1%81%D0%BD%D0%BE%D0%B3%D0%B2%D0%B0%D1%80%D0%B4%D0%B5%D0%B9%D1%81%D0%BA%D0%B8%D0%B9%20%D0%BF%D1%80%D0%BE%D0%B5%D0%B7%D0%B4,%20%D0%B4.21%20%D1%81%D1%82%D1%80.2 这就是你的意思?
    • 是的。这应该允许您创建URL 的实例。最初,您得到 nil 是因为您传递给构造函数的 url 字符串格式不正确,其查询部分包含西里尔文文本。
    • 我在 let googleUrl = URL(string: "comgooglemaps://?daddr=\(escapingString)&amp;directionsmode=driving")! 中添加了 escapingString,但我仍然没有得到 nil
    • 试试let urlString = "comgooglemaps://?daddr=\(escapingString)&amp;directionsmode=dri‌​ving"; print(urlString) 请在此处发布打印的内容。你很可能遇到this
    • 我决定尝试 CLGeocoder,我得到了我想要的。无论如何,感谢您的帮助。
    【解决方案2】:

    我试试这个,它对我有帮助

    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)
    
                }
    
            })
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-13
      • 2012-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多