Ben
Ben

Reputation: 1031

IOS get location with CLLocationManager from lat/long

I am trying to Reverse geocode location from Lat/Long value that I get earlier in the App and I would like from this coordinate to find the city name, country name and ISO.

I am currently using CLLocationManager to get actual location information with the folowing code:

//Auto geolocation and find city/country
locationManager.delegate=self;

//Get user location
[locationManager startUpdatingLocation];
[self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler: 
 ^(NSArray *placemarks, NSError *error) {

     //Get nearby address
     CLPlacemark *placemark = [placemarks objectAtIndex:0];

     //String to hold address
     locatedAtcountry = placemark.country;
     locatedAtcity = placemark.locality;
     locatedAtisocountry = placemark.ISOcountryCode;

     //Print the location to console
     NSLog(@"Estas en %@",locatedAtcountry);
     NSLog(@"Estas en %@",locatedAtcity);
     NSLog(@"Estas en %@",locatedAtisocountry);

     [cityLabel setText:[NSString stringWithFormat:@"%@,",locatedAtcity]];
     [locationLabel setText:[NSString stringWithFormat:@"%@",locatedAtcountry]];

     //Set the label text to current location
     //[locationLabel setText:locatedAt];

 }];

It is working perfectly but, It is possible to do the same from Long/Lat value that I had already saved in the device and not with the current location like on the actual code ?

Update and solution:

Thanks Mark for the answer, I finally use the following code to get info from saved coordinate:

 CLLocation *location = [[CLLocation alloc] initWithLatitude:37.78583400 longitude:-122.40641700];

[self.geoCoder reverseGeocodeLocation: location completionHandler: 
 ^(NSArray *placemarks, NSError *error) {

     //Get nearby address
     CLPlacemark *placemark = [placemarks objectAtIndex:0];

     //String to hold address
     locatedAtcountry = placemark.country;
     locatedAtcity = placemark.locality;
     locatedAtisocountry = placemark.ISOcountryCode;

     //Print the location to console
     NSLog(@"Estas en %@",locatedAtcountry);
     NSLog(@"Estas en %@",locatedAtcity);
     NSLog(@"Estas en %@",locatedAtisocountry);

     [cityLabel setText:[NSString stringWithFormat:@"%@",locatedAtcity]];
     [locationLabel setText:[NSString stringWithFormat:@"%@",locatedAtcountry]];

     //Set the label text to current location
     //[locationLabel setText:locatedAt];

 }];

Upvotes: 0

Views: 4696

Answers (1)

Mark Granoff
Mark Granoff

Reputation: 16938

Yes. Create a CLLocation object using the initWithLatitude:longitude: method using your saved lat/lon values, and pass that to reverseGeocodeLocation:.

I am surprised that you say this is working (although, if you're on the simulator, location services are simulated anyway, which might be the reason) because when you call startUpdatingLocation, your implementation of CLLocationManagerDelegate methods like locationManager:didUpdateToLocation:fromLocation: get called. (You've implemented these right?) It is only when this (and other) delegate method is called that you can be certain that you have successfully determined the user's location.

You may want to read up on the CLLocationManagerDelegate protocol and on Location Services best practices as documented by Apple.

Upvotes: 2

Related Questions