【问题标题】:MapKit Swift Converting Address to CoordinatedMapKit Swift将地址转换为协调
【发布时间】:2016-07-02 03:12:48
【问题描述】:

我有这个功能:

func addressToCoordinatesConverter(address: NSString) {
    let geoCoder = CLGeocoder()
    geoCoder.geocodeAddressString(address, completionHandler: { (placemarks, error) -> Void in
        if error = nil {
            if placemarks!.count = 0 {
                let annotation = MKAnnotation()
                annotation.coordinate = (placemark.location?.coordinate)!
                self.mapView.showAnnotations([annotation], animated: true)
                self.mapView.selectedAnnotations(annotation, animated: true)
            }
        }
    })

我在我的代码中调用它来将地址(字符串“地址”)转换为坐标,然后转换到我的 mapView 上。我在这里的第三行出现错误提示

"NSString is not implicitly convertible to 'String'; did you mean to use 'as' to explicitly convert" 

如何才能最好地修复此错误?谢谢

【问题讨论】:

  • 按照错误提示。为什么你不在函数中使用 String 呢? func addressToCoordinatesConverter(address: String)

标签: swift mapkit geocoding


【解决方案1】:
   func addressToCoordinatesConverter(address: String) {
        let geoCoder = CLGeocoder()
        geoCoder.geocodeAddressString(address, completionHandler: { (placemarks, error) -> Void in
            if error = nil {
                if placemarks!.count = 0 {
                    let annotation = MKAnnotation()
                    annotation.coordinate = (placemark.location?.coordinate)!
                    self.mapView.showAnnotations([annotation], animated: true)
                    self.mapView.selectedAnnotations(annotation, animated: true)
                }
            }
        })

func addressToCoordinatesConverter(address: NSString) {
    let geoCoder = CLGeocoder()
    geoCoder.geocodeAddressString(address as! String, completionHandler: { (placemarks, error) -> Void in
        if error = nil {
            if placemarks!.count = 0 {
                let annotation = MKAnnotation()
                annotation.coordinate = (placemark.location?.coordinate)!
                self.mapView.showAnnotations([annotation], animated: true)
                self.mapView.selectedAnnotations(annotation, animated: true)
            }
        }
    })

第一个传递一个字符串,第二个传递一个 NSString。使用与您要传入的内容相同的那个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-09
    • 1970-01-01
    • 2013-05-09
    • 2017-03-10
    相关资源
    最近更新 更多