gpt4 book ai didi

swift - MapKit Swift 将地址转换为协调地址

转载 作者:行者123 更新时间:2023-11-30 10:01:43 25 4
gpt4 key购买 nike

我有这个功能:

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

我在代码中调用它来将地址(字符串“地址”)转换为坐标,然后转换到我的 map View 上。我在第三行收到错误消息

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

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

最佳答案

   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。使用与您要传入的内容相同的内容。

关于swift - MapKit Swift 将地址转换为协调地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38156145/

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