Use CLLocationManager and MKReverseGeocoder to get name of city

Ashutosh picture Ashutosh · Jul 25, 2010 · Viewed 7.1k times · Source

I am trying to get the name of the city of user's current location by using MKReverseGeoCoder but it has some errors which I cannot recognize. Here are the details:

It has some errors which I cannot recognize

Undefined symbols:
  ".objc_class_name_CLLocationManager", referenced from:
      literal-pointer@__OBJC@__cls_refs@CLLocationManager in mapViewController.o
  "_kCLLocationAccuracyNearestTenMeters", referenced from:
      _kCLLocationAccuracyNearestTenMeters$non_lazy_ptr in mapViewController.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Here;s my code:

mapViewController.m

//
//  mapViewController.m
//  map
//
//  Created by Ashutosh Tiwari on 7/23/10.
//  Copyright ISU 2010. All rights reserved.
//
#import <MapKit/MapKit.h>
#import "mapViewController.h"

@implementation mapViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];

    [super viewDidLoad];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{

    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    geoCoder.delegate = self;
    [geoCoder start];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
    NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    MKPlacemark * myPlacemark = placemark;

    NSString *kABPersonAddressCityKey;
    NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);
}

- (void)dealloc {
    [super dealloc];
}

@end

Answer

Vladimir picture Vladimir · Jul 25, 2010

Add CoreLocation.framework to link to your project (target settings/ Linked libraries / add / choose CoreLocation.framework)

Add: Briefly what each method does:

  • viewDidLoad:
    Creates CLLocationManager instance and starts updating location - to get current user coordinates

  • locationManager:didUpdateToLocation:
    Gets called when CLLocationManager receives user coordinates. Now we can pass them to MKReverseGeocoder to get information of user location (country, city etc)

  • locationManager:didFailWithError: and reverseGeocoder:didFailWithError:
    Handle possible errors - just log them in current implementation

  • reverseGeocoder:didFindPlacemark:
    Gets called when MKReverseGeocoder finds info for your coordinate, you can retrieve info you need from corresponding fields of MKPlacemark instance you get.

kABPersonAddressCityKey - is a key string for City field in placemark address dictionary. It is defined in ABPerson.h header because address fields for placemark and ABRecord address are the same. So to use that key you may need to link with AddressBook framework as well.