views:

467

answers:

2

I'm trying to find the current location and then use the geocoder to find the city,state. This is what I have:

- (void)viewDidLoad 
{  
    self.locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    geoCoder.delegate = self;
    [geoCoder start];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark{
    //NSLog(@"Reverse Geocoder completed");
    MKPlacemark * myPlacemark = placemark;

    NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*)kABPersonAddressCityKey];
    NSString *state = [myPlacemark.addressDictionary objectForKey:(NSString*)kABPersonAddressStateKey];
    location = [[NSString alloc] initWithFormat:@"%@,%@", city, state];
}

I've tested the reverse Geocoder and it works fine when I manually type create a coordinate of my current location: (50.4, -104.6). However, when I run this code with a breakpoint in "didUpdateToLocation" delegate method, the newLocation gives me a coordinate of 37.3,-122.0 which is Cupertino, California. I think I'm setting up the locationManager wrong so any ideas?

A: 

errr... that explains it... thanks.

tul697
A: 

Hi Tul697

im searching for this type of problem for my project would you be able to share your code?

Silent
which part do you need help with? the code above uses LocationManager to find the current location. Inside didUpdateToLocation delegate it passes the coordinates to geocoder to perform a reverse geocode lookup. In the didFindPlacemark delegate, it uses the placemark found to extract the city and state.
tul697
im trying to embed the same thing but start it once the user starts the app and it would be only one time. So i was thinking it would go on a delegate class, i need help on how to embed it i tried finding examples on apple connection but no luck.
Silent

related questions