views:

2388

answers:

5

hii i want to retrive user's current city name ... can any one help me .. urget require .. thanks

+1  A: 

Read the documentation for MKReverseGeocoder -- the documentation, guides & sample applications are provided by Apple for a reason.

Nathan de Vries
+2  A: 

You have to get user's current location and then use MKReverseGeocoder to recognize the city.

There's great example in iPhone App Programming Guide, chapter 8. Once you've got location initialize geocoder, set the delegate and read country from the placemark. Read the documentation for MKReverseGeocodeDelegate and create methods:

  • reverseGeocoder:didFindPlacemark:
  • reverseGeocoder:didFailWithError:

    MKReverseGeocoder *geocoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    geocoder.delegate = self;
    [geocoder start];
    
Marcin Zbijowski
+16  A: 

What you have to do is setup a CLLocationManager that will find your current coordinates. With the current coordinates you need to use MKReverseGeoCoder to find your location.

- (void)viewDidLoad 
{  
    // this creates the CCLocationManager that will find your current location
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    [locationManager startUpdatingLocation];
}

// this delegate is called when the app successfully finds your current location
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    // this creates a MKReverseGeocoder to find a placemark using the found coordinates
    MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
    geoCoder.delegate = self;
    [geoCoder start];
}

// this delegate method is called if an error occurs in locating your current location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
{
 NSLog(@"locationManager:%@ didFailWithError:%@", manager, error);
}
// this delegate is called when the reverseGeocoder finds a placemark
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    MKPlacemark * myPlacemark = placemark;
    // with the placemark you can now retrieve the city name
    NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressCityKey];
}

// this delegate is called when the reversegeocoder fails to find a placemark
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error);
}
tul697
A: 

The question is -Is it right to use MKReverseGeoCoding class without conjunction with the MKMapView ??

ishank
A: 

Hi,

when I release or autorelease the CLLocationManager as you do, it doesn't work. The delegate functions are not called.

When should I release CLLocationManager ?

When I don't release it, it works but the delegate function are called three times. Do you know why ?

(sorry for my bad english, I'm french)