gpt4 book ai didi

swift - 使用 Swift 将 CLLocationCoordinate2d 转换为街道地址

转载 作者:搜寻专家 更新时间:2023-11-01 06:12:40 25 4
gpt4 key购买 nike

我正在尝试将 CLLocationCoordinates2d 坐标转换为实际街道地址。

我尝试过 CLGeocoder,但没有成功。这是我提供纬度和经度坐标的代码。

    let manager = CLLocationManager()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{

guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
// let locationtext = "locations = \(locValue.latitude) \(locValue.longitude)"
locationTxt.text = "\(locValue.latitude) \(locValue.longitude)"

}

谢谢!

最佳答案

func convertLatLongToAddress(latitude:Double,longitude:Double){

let geoCoder = CLGeocoder()
let location = CLLocation(latitude: latitude, longitude: longitude)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in

// Place details
var placeMark: CLPlacemark!
placeMark = placemarks?[0]

// Location name
if let locationName = placeMark.location {
print(locationName)
}
// Street address
if let street = placeMark.thoroughfare {
print(street)
}
// City
if let city = placeMark.locality {
print(city)
}
// State
if let state = placemark.administrativeArea {
print(state)
}
// Zip code
if let zipCode = placeMark.postalCode {
print(zipCode)
}
// Country
if let country = placeMark.country {
print(country)
}
})

}

关于swift - 使用 Swift 将 CLLocationCoordinate2d 转换为街道地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51905877/

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