iOS: get Accurate Reverse Geocoding using CLLocationManager?

拟墨画扇 提交于 2019-12-11 09:34:28

问题


Background: I want to allow users to get their location within seconds when they want it. However, the location is mostly inaccurate. I want to ask the user to if they would like to try again for a more accurate address.

Question: How to code such that it guarantees that the next guess, is always better than the last guess.

What I have tried:

CLLocationManager *locationManager;

- (CLLocationManager *)locationManager {
    if (locationManager != nil) {
        return locationManager;
    }
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager setDelegate:self];
    return locationManager;
}

-(void)myLocationClickedWithCount: (int) count{
    [[self locationManager] startUpdatingLocation];
    CLLocation *location = [locationManager location];
    CLGeocoder *locationGeocoded = [[CLGeocoder alloc] init];
    [locationGeocoded reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        if (!placemark){
            if (count > 0){
                //placemark not available, try again after 1 second
                sleep(1);
                [self myLocationClickedWithCount:count-1];
            }
            else{
                //Unable to get location after 3 tries
                UIAlertView *locationNotFoundAlert = [[UIAlertView alloc]initWithTitle:@"Unable to Locate" message:@"Would you like to try again?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
                locationNotFoundAlert.tag = locationNotFoundAlertTag;
                [locationNotFoundAlert show];
            }
        }else{
            // got location but not accurate.
            if (location.horizontalAccuracy > accuracyRadius) {
                UIAlertView *locationInaccurateAlert = [[UIAlertView alloc]initWithTitle:@"Inaccurate Location" message:[NSString stringWithFormat:@"Your GPS shows the radius of %.2f meters. Would you like to try again?",location.horizontalAccuracy] delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
                locationInaccurateAlert.tag = locationInaccurateAlertTag;
                [locationInaccurateAlert show];
            }else {
                address.text = placemark;
            }
        }
    }];
}

Update When the user asks again, it can take a little longer to guess. I just want to know how I should code it. IE: Should I call [[self locationManager] startUpdatingLocation]; again? or will that reset my location so far. Hence, how can i improve my guess the second time based on the current information GPS has (the longer GPS is on, the better accuracy right?).


回答1:


there is nothing much you can do regarding accuracy via code... apart from

   [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

which you have already done... As far as I have seen if you are on 3G or EDGE than your accuracy increases considerably (accuracy level of 5-10m can be achieved).

you can refer this article by Apple which talks how the iphone GPS accuracy works...

hoping this helps..



来源:https://stackoverflow.com/questions/10127405/ios-get-accurate-reverse-geocoding-using-cllocationmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!