Search code examples
iosobjective-ccllocationmanagerclgeocoder

How do I extract the lat and long from the following code?


How do I extract the lat and long from the follwoing code:

CLGeocoder *geocoder = [CLGeocoder new];
[geocoder geocodeAddressString:city completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
    NSLog(@"Error: %@", [error localizedDescription]);
}

if ([placemarks count] > 0) {

    CLPlacemark *placemark = [placemarks lastObject]; 
    NSLog(@"Location is: %@", placemark.location);

}

from the nslog output I get:

Location is: <+45.46368100,+9.18817140> +/- 100.00m (speed -1.00 mps / course -1.00) @ 25/09/2013 11:04:20 British Summer Time

I would like to just get the lat and lon and store them into variables.


Solution

  • CLLocationCoordinate2D coordinate = placemark.location.coordinate
    

    then:

    coordinate.longitude
    coordinate.latitude
    

    docs: https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLPlacemark_class/Reference/Reference.html