Get location name from Latitude & Longitude in iOS

前端 未结 12 876
遥遥无期
遥遥无期 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:36

    If the location you are trying to get is listed in this list then there is something wrong in your code..

    http://developer.apple.com/library/ios/#technotes/tn2289/_index.html#//apple_ref/doc/uid/DTS40011305

    otherwise CLGeoCoder doesnt have addresses to other countries.

    I faced the same problem so i used this to get the address.. Gives a really accurate address.

    http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false

    0 讨论(0)
  • 2020-11-28 07:36

    use this API for getting the data and pass lat and long values.

    API for Geo location

    0 讨论(0)
  • 2020-11-28 07:39

    I implemented Niru's solution in Swift 3, posting here should anybody need it:

    let geocoder = CLGeocoder()
    geocoder.reverseGeocodeLocation(self.location!) { (placemarksArray, error) in
    
        if (placemarksArray?.count)! > 0 {
    
            let placemark = placemarksArray?.first
            let number = placemark!.subThoroughfare
            let bairro = placemark!.subLocality
            let street = placemark!.thoroughfare
    
            self.addressLabel.text = "\(street!), \(number!) - \(bairro!)"
        }
    }
    
    0 讨论(0)
  • 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!

    0 讨论(0)
  • 2020-11-28 07:49

    Use google api for reverse geotagging :

    URL : http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true_or_false

    From : https://developers.google.com/maps/documentation/geocoding/

    0 讨论(0)
  • 2020-11-28 07:51

    First of all, filter locations in didUpdateToLocation to prevent use cached or wrong locations for geocoding

    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
    
    if (abs(locationAge) > 5.0) return;
    
    if (newLocation.horizontalAccuracy < 0) return;
    

    Also, try to move the reverseGeoCoding method away from didUpdateToLocation

    0 讨论(0)
提交回复
热议问题