gpt4 book ai didi

ios - 如何将地理坐标转换为准确的地址?

转载 作者:行者123 更新时间:2023-11-28 06:35:22 30 4
gpt4 key购买 nike

我想将包含纬度和经度值的用户位置反转为准确地址。但是不行,执行后返回空字符串。

此外,Xcode 建议我使用 CNPostalAddress,我不知道它是什么。

这是函数

import Foundation
import CoreLocation
import AddressBook`

func reverseGeocoder()->String{
let info:String?
var geocoder = CLGeocoder()
var currentUserLocation = CLLocation(latitude: 30, longitude: 122)
geocoder.reverseGeocodeLocation(currentUserLocation, completionHandler: {
(placemarks,error) -> Void in
if placemarks != nil && placemarks?.count > 0{
let placemark = placemarks![0] as CLPlacemark
let addressDictionary = placemark.addressDictionary! as NSDictionary

let str:NSMutableString = ""
if let address = addressDictionary.objectForKey(kABPersonAddressStreetKey) as? String{
str.appendString(address)
}

if let state = addressDictionary.objectForKey(kABPersonAddressStateKey) as? String{
str.appendString(state)
}

if let city = addressDictionary.objectForKey(kABPersonAddressCityKey) as? String{
str.appendString(city)
}

info = str as String
}
}
)
return info
}

这里是警告 Warnnings

如果你能帮我解决它,我将不胜感激:)

最佳答案

import AddressBook替换为import Contacts并替换

addressDictionary.objectForKey(kABPersonAddressStreetKey)addressDictionary.objectForKey(CNPostalAddress.Street)

addressDictionary.objectForKey(kABPersonAddressStateKey)addressDictionary.objectForKey(CNPostalAddress.State)

addressDictionary.objectForKey(kABPersonAddressCityKey)addressDictionary.objectForKey(CNPostalAddress.City)

您可以在Apple Douments中获得更多信息

使用完成

func reverseGeocoder (completion: (info:String) -> Void){

var geocoder = CLGeocoder()
var currentUserLocation = CLLocation(latitude: 30, longitude: 122)
geocoder.reverseGeocodeLocation(currentUserLocation, completionHandler: {
(placemarks,error) -> Void in
if placemarks != nil && placemarks?.count > 0{
let placemark = placemarks![0] as CLPlacemark
let addressDictionary = placemark.addressDictionary! as NSDictionary

let str:NSMutableString = ""
if let address = addressDictionary.objectForKey(kABPersonAddressStreetKey) as? String{
str.appendString(address)
}

if let state = addressDictionary.objectForKey(kABPersonAddressStateKey) as? String{
str.appendString(state)
}

if let city = addressDictionary.objectForKey(kABPersonAddressCityKey) as? String{
str.appendString(city)
}
completion(info: str as String)

}
}
)

关于ios - 如何将地理坐标转换为准确的地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39229190/

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