views:

360

answers:

1

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
A: 

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.

Vladimir
Thanks a Ton !!!! I get this code from somewhere from the blog and not really sure what is supposed to do. Could you please explain this.Thanks in advance.
Ashutosh
Also if you can understand could you please tell me what is the significance of kABPersonAddressCityKey because actually it was not declared and i added the line NSString *kABPersonAddressCityKey.I would really appreciate itThanks,
Ashutosh
Thanks a lot man!!!But still this code is not doing anything except showing the map and showing the current location which i did with the use of interface builder. So i was wondering what's the significance of this code what is it supposed to do.
Ashutosh
it receives placemark in didFindPlacemark method but does nothing with it. You can add logging there to see some results (and if they are valid or not)
Vladimir