MapKit Swift将地址转换为Coordinated

时间:2016-07-02 03:12:49

标签: swift mapkit geocoding

我有这个功能:

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

我在我的代码中调用它来将地址(字符串'address')转换为坐标,然后转换为我的mapView。我在第三行收到错误

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

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

1 个答案:

答案 0 :(得分: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)
            }
        }
    })

第一个传递一个String,第二个传递一个NSString。使用与您想要传递的内容相同的那个。