Question

I want to find the current location of user based on latitude and longitude. Prevoiusly I have done it using MKReverseGeocoder but as it is depracated in ios5 ,I going with CLGeocoder.But unable to get the current location .

- (void)viewDidLoad
{
 locationManager = [[CLLocationManager alloc] init];
        [locationManager setDelegate:self];
        [locationManager setDistanceFilter:kCLDistanceFilterNone];
        [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        [locationManager startUpdatingLocation];

}
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    CLLocation *currentLocation = newLocation;

    locationLabelOne=newLocation.coordinate.latitude;


    locationLabelTwo=newLocation.coordinate.longitude;
    geocoder = [[CLGeocoder alloc] init];


    [locationManager stopUpdatingLocation];

    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
           NSString *get = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                                 placemark.subThoroughfare, placemark.thoroughfare,
                                 placemark.postalCode, placemark.locality,
                                 placemark.administrativeArea,
                                 placemark.country];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];
    NSLog(@"%@",placemark);
}

When I NSLog placemark it is returning null.Where I m going wrong?

Was it helpful?

Solution

Possible issues:

1) locationManager:didUpdateToLocation:fromLocation is deprecated iOS 6.0 you should use locationManager:didUpdateLocations: instead.

 - (void)locationManager:(CLLocationManager *)manager
 didUpdateLocations:(NSArray *)locations
{
     currentLocation=[locations lastObject];
     // rest code will be same
 }

2) Also [locationManager stopUpdatingLocation]; sounds fishy. IMO you should not use that in CLLocationManagerDelegatemethod.

P.S.

i) For more details in Forward/Reverse geocoding you can look into Apple's sample code GeocoderDemo

ii) If you use Google API for Reverse Geocoding it will have all data available it has been for years but CLGeocoder will not have complete data like street name, pin code for countries like India. What happens is, when you do Reverse Geocoding requests, you pass co ordinates to Core Location which connects to web service in background(which developer won't come to know) and returns a user-readable address. So it may be possible this web service may not be able to return data as accurate as GOOGLE API.

OTHER TIPS

I had the same problem, then with some experimentation I solved my problem…

Make sure: -90 < latitude < 90 -180 < longitude < 180

It doesn't seem to perform a basic mod function. Hope that helps...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top