如何使用Swift将坐标转换为地址

时间:2018-09-26 14:16:15

标签: ios swift geocoding xcode10

我尝试使用reverseGeocoordinate在我的locationEnd上显示DestinationSearchBar的地址,但是我不知道该怎么做。

func viewController(_ viewController: GMSAutocompleteViewController, didAutocompleteWith place: GMSPlace) {
    let camera = GMSCameraPosition.camera(withLatitude: place.coordinate.latitude, longitude: place.coordinate.longitude, zoom: 15.0)

   if locationSelected == .startLocation {
        addressSearchBar.text = "\(place.coordinate.latitude), \(place.coordinate.longitude)"
        locationStart = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
        createMarker(titleMarker: "Punto de recojo", iconMarker: #imageLiteral(resourceName: "marker"), latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
    } else {
        DestinationSearchBar.text = "\(place.coordinate.latitude), \(place.coordinate.longitude)"
        locationEnd = CLLocation(latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)
        createMarker(titleMarker: "Punto de destino", iconMarker: #imageLiteral(resourceName: "marker"), latitude: place.coordinate.latitude, longitude: place.coordinate.longitude)



    self.mapView.camera = camera
    self.dismiss(animated: true, completion: nil)

}
}

1 个答案:

答案 0 :(得分:2)

Apple提供反向地理位置api

let address = CLGeocoder.init()
    address.reverseGeocodeLocation(CLLocation.init(latitude: latitude, longitude:longitude)) { (places, error) in
        if error == nil{
            if let place = places{
                //here you can get all the info by combining that you can make address
            }
        }
    }

别忘了进口

import MapKit
import CoreLocation