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

kly*_*ins 5 xcode reverse geocoding cllocation swift

我试图将 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)"

}
Run Code Online (Sandbox Code Playgroud)

谢谢!

小智 6

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)
        }
    })
    
}
Run Code Online (Sandbox Code Playgroud)