reggian
reggian

Reputation: 657

CLGeocoder Reverse Geocode considering horizontal accuracy

Is there a good solution to reverse geocode considering the horizontal accuracy of the location?

For instance this is the result for the location (and is regardless of horizontal accuracy):

{
    City = "San Francisco";
    Country = "United States";
    CountryCode = US;
    FormattedAddressLines =     (
        "246 Powell St",
        "San Francisco, CA  94102-2206",
        "United States"
    );
    Name = "246 Powell St";
    PostCodeExtension = 2206;
    State = CA;
    Street = "246 Powell St";
    SubAdministrativeArea = "San Francisco";
    SubLocality = "Union Square";
    SubThoroughfare = 246;
    Thoroughfare = "Powell St";
    ZIP = 94102;
}

I would like to get the result considering accuracy. E.g.:

I have figured I could probably do this by requesting reverse geocoding for multiple coordinates that are roughly on the edges of the provided horizontal accuracy and then intersecting the results. But is there a more clean way?

Upvotes: 2

Views: 368

Answers (1)

AMI289
AMI289

Reputation: 1108

Did you mean CLGeocoder?

I think that requesting multiple reverse geocoding will probably do more harm then good.
From Apple's CLGeocoder documentation (which can be found here):

Applications should be conscious of how they use geocoding.  
Geocoding requests are rate-limited for each app,  
so making too many requests in a short period of time  
may cause some of the requests to fail.

So I suggest not going this way.

Could be better ways, but just at the top of my head, you could probably use some sort of if-else method that uses the horizontalAccuracy property.

It's been a while since I've used MapKit so could be that my code below isn't 100% accurate but I can't test its functionality at the moment (it should compile though), but it'll give you an idea of how this can be done.

For example:

// here geocoder is your CLGeocoder object  
// and location is the CLLocation you try to reverse geocode
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {  
    if(error) {  
        NSLog(@"Error occurred: %@", [error localizedDescription]);  
    } else { // No error has occurred  
        if([placemarks count]) { // Just another step of precaution  
            CLPlacemark *placemark = placemarks[0];  // assume the first object is our correct place mark  
            if(location.horizontalAccuracy <= 10) { // equal or less than 10 meters  
                NSLog(@"Result: %@ %@", placemark.subThoroughfare, placemark.thoroughfare); // 246 Powell St  
            } else if (location.horizontalAccuracy <= 100) { // equal or less than 100 meters  
                NSLog(@"Result: %@", placemark.subLocality);  // Union Square  
            } else if (location.horizontalAccuracy <= 100000) { // equal or less than 100km  
                NSLog(@"Result: %@", placemark.subAdministrativeArea); // San Francisco
            }
        }
    }
}];  

When I would be able to test its functionality myself (could be a couple of hours, or a couple of days) I would edit if changes needed to be made.

Let me know if you are having issues or you have more questions.

Good luck mate.

Upvotes: 1

Related Questions