Get location name from Latitude & Longitude in iOS

萝らか妹 提交于 2019-11-26 22:12:24

I'm giving you snippet which I'm using for resolving address. I'm including comment also at neccessary place to understand the code for you. Besides that feel free to ask any question from snippet if you get fail to understand anything.

Write following snippet in didUpdateToLocation method

NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil)
    NSLog(@"longitude = %.8f\nlatitude = %.8f", currentLocation.coordinate.longitude,currentLocation.coordinate.latitude);

// stop updating location in order to save battery power
[locationManager stopUpdatingLocation];


// Reverse Geocoding
NSLog(@"Resolving the Address");

// “reverseGeocodeLocation” method to translate the locate data into a human-readable address.

// The reason for using "completionHandler" ----
   //  Instead of using delegate to provide feedback, the CLGeocoder uses “block” to deal with the response. By using block, you do not need to write a separate method. Just provide the code inline to execute after the geocoding call completes.

[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
 {
    NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
    if (error == nil && [placemarks count] > 0)
    {
        placemark = [placemarks lastObject];

        // strAdd -> take bydefault value nil
        NSString *strAdd = nil;

        if ([placemark.subThoroughfare length] != 0)
            strAdd = placemark.subThoroughfare;

        if ([placemark.thoroughfare length] != 0)
        {
            // strAdd -> store value of current location
            if ([strAdd length] != 0)
                strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark thoroughfare]];
            else
            {
            // strAdd -> store only this value,which is not null
                strAdd = placemark.thoroughfare;
            }
        }

        if ([placemark.postalCode length] != 0)
        {
            if ([strAdd length] != 0)
                strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark postalCode]];
            else
                strAdd = placemark.postalCode;
        }

        if ([placemark.locality length] != 0)
        {
            if ([strAdd length] != 0)
                strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark locality]];
            else
                strAdd = placemark.locality;
        }

        if ([placemark.administrativeArea length] != 0)
        {
            if ([strAdd length] != 0)
                strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark administrativeArea]];
            else
                strAdd = placemark.administrativeArea;
        }

        if ([placemark.country length] != 0)
        {
            if ([strAdd length] != 0)
                strAdd = [NSString stringWithFormat:@"%@, %@",strAdd,[placemark country]];
            else
                strAdd = placemark.country;
        }

Where strAdd will return address using geolocation..

Enjoy Programming !!

Method with completion block:

typedef void(^addressCompletion)(NSString *);

-(void)getAddressFromLocation:(CLLocation *)location complationBlock:(addressCompletion)completionBlock
{
    __block CLPlacemark* placemark;
    __block NSString *address = nil;

    CLGeocoder* geocoder = [CLGeocoder new];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
     {
         if (error == nil && [placemarks count] > 0)
         {
             placemark = [placemarks lastObject];
             address = [NSString stringWithFormat:@"%@, %@ %@", placemark.name, placemark.postalCode, placemark.locality];
             completionBlock(address);
         }
     }];
}

This is how to use it:

CLLocation* eventLocation = [[CLLocation alloc] initWithLatitude:_latitude longitude:_longitude];

[self getAddressFromLocation:eventLocation complationBlock:^(NSString * address) {
        if(address) {
            _address = address;
        }
    }];

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!)"
    }
}

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

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

Once you have latitude, longitude values of a location you can use CLGeocoder here is a tutorial which might help you.

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

API for Geo location

In Swift 4.1 and Xcode 9.4.1, this is one of my solution. Here i'm using geocode API to get the complete address. With this api i can get the village names also.

I'm using reverseGeocodeLocation but it's not getting village address details or village names, it's getting only near by city names. It's one of the finest solution....

func getAddressForLatLng(latitude: String, longitude: String)  { // Call this function

     let url = NSURL(string: "https://maps.googleapis.com/maps/api/geocode/json?latlng=\(latitude),\(longitude)")//Here pass your latitude, longitude
     print(url!)
     let data = NSData(contentsOf: url! as URL)

     if data != nil {
        let json = try! JSONSerialization.jsonObject(with: data! as Data, options: JSONSerialization.ReadingOptions.allowFragments) as! NSDictionary
        print(json)

        let status = json["status"] as! String
        if status == "OK" {

            if let result = json["results"] as? NSArray   {

                if result.count > 0 {
                    if let addresss:NSDictionary = result[0] as? NSDictionary {
                        if let address = addresss["address_components"] as? NSArray {
                            var newaddress = ""
                            var number = ""
                            var street = ""
                            var city = ""
                            var state = ""
                            var zip = ""
                            var country = ""

                            if(address.count > 1) {
                                number =  (address.object(at: 0) as! NSDictionary)["short_name"] as! String
                            }
                            if(address.count > 2) {
                                street = (address.object(at: 1) as! NSDictionary)["short_name"] as! String
                            }
                            if(address.count > 3) {
                                city = (address.object(at: 2) as! NSDictionary)["short_name"] as! String
                            }
                            if(address.count > 4) {
                                state = (address.object(at: 4) as! NSDictionary)["short_name"] as! String
                            }
                            if(address.count > 6) {
                                zip =  (address.object(at: 6) as! NSDictionary)["short_name"] as! String
                            }
                            newaddress = "\(number) \(street), \(city), \(state) \(zip)"
                            print(newaddress)

                            // OR 
                            //This is second type to fetch pincode, country, state like this type of data

                            for i in 0..<address.count {
                                print(((address.object(at: i) as! NSDictionary)["types"] as! Array)[0])
                                if ((address.object(at: i) as! NSDictionary)["types"] as! Array)[0] == "postal_code" {
                                    zip =  (address.object(at: i) as! NSDictionary)["short_name"] as! String
                                }
                                if ((address.object(at: i) as! NSDictionary)["types"] as! Array)[0] == "country" {
                                    country =  (address.object(at: i) as! NSDictionary)["long_name"] as! String
                                }
                                if ((address.object(at: i) as! NSDictionary)["types"] as! Array)[0] == "administrative_area_level_1" {
                                    state =  (address.object(at: i) as! NSDictionary)["long_name"] as! String
                                }
                                if ((address.object(at: i) as! NSDictionary)["types"] as! Array)[0] == "administrative_area_level_2" {
                                    district =  (address.object(at: i) as! NSDictionary)["long_name"] as! String
                                }

                            }

                        }

                    }
                }

            }

        }

    }

}

Call this function like this

self.getAddressForLatLng(latitude: "\(self.lat!)", longitude: "\(self.lng!)")

Try this

[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        //..NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            lblgetaddrees.text = [NSString stringWithFormat:@"%@,%@,%@,%@,%@,%@",
                                  placemark.subThoroughfare, placemark.thoroughfare,
                                  placemark.postalCode, placemark.locality,
                                  placemark.administrativeArea,
                                  placemark.country];
        } else {
            //..NSLog(@"%@", error.debugDescription);
        }
    } ];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!