Get location name from Latitude & Longitude in iOS

前端 未结 12 1014
遥遥无期
遥遥无期 2020-11-28 07:30

I want to find current location name from latitude and longitude,

Here is my code snippet I tried but my log shows null value in all the places except in place

12条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-28 07:45

    Here's how to get the address from Location Coordinate (lat,long).

    You can get the address info such like city, country, postcode etc. from LocationCoordinates with this code

    func getReverSerGeoLocation(location : CLLocation) {
        print("getting Address from Location cordinate")
    
        CLGeocoder().reverseGeocodeLocation(location) {
            placemarks , error in
        
            if error == nil && placemarks!.count > 0 {
                guard let placemark = placemarks?.last else {
                    return
                }
                print(placemark.thoroughfare)
                print(placemark.subThoroughfare)
                print("postalCode :-",placemark.postalCode)
                print("City :-",placemark.locality)
                print("subLocality :-",placemark.subLocality)
                print("subAdministrativeArea :-",placemark.subAdministrativeArea)
                print("Country :-",placemark.country)
            }
        }
    }
    

    Just call above method by passing latitude longitude as argument such like below

        getReverSerGeoLocation(location: CLLocation(latitude: 21.0225, longitude: 72.5714))
    

    Hope this helps!

提交回复
热议问题