CLGeocoder returning locations in other countries

牧云@^-^@ 提交于 2020-01-01 08:45:17

问题


I have the following code:

CLGeocoder *_geo = [[CLGeocoder alloc] init];
CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter: CLLocationCoordinate2DMake(37.33233141, -122.03121860) radius:100 identifier:@"San Francisco"];
[_geo geocodeAddressString:@"Starbucks" inRegion:region
             completionHandler:^(NSArray *placemarks, NSError *error)
{
    NSLog("%@", placemarks);
}];

This returns a Starbucks location in the Philippines even though the center is in the middle of San Francisco.

(NSArray *) $3 = 0x07cd9d10 <__NSArrayM 0x7cd9d10>(
Starbuck's, Metro Manila, Quezon City, Republic of the Philippines @ <+14.63617752,+121.03067530> +/- 100.00m, region (identifier <+14.63584900,+121.02951050> radius 166.35) <+14.63584900,+121.02951050> radius 166.35m
)

Any ideas?


回答1:


Though it seems unlikely, it certainly appears that either a) Apple didn't intend on forward geocoding business names near a region, or b) this is a bug.

The CLGeocoder reference, in its overview, states:

Forward-geocoding requests take a user-readable address and find the corresponding latitude and longitude value...

which certainly implies a real physical address as the search string (especially the "user-readable address" part). However, the docs for geocodeAddressString: inRegion: completionHandler: states that the search string is:

... a string describing the location you want to look up...

which is even more vague. I tried running your code, and code very similar to it, through a couple of my projects and I get the same result. Even Apple's GeocoderDemo sample exhibits the problem (although I live in California just ~150 miles from your lat/long example), so it's certainly nothing either of us have written.

I set out looking to help solve this problem for/with you! But, this bit of is research is all I've got. Best of luck.




回答2:


Here is a workaround that is working for me. I'm using geocodeAddressString: completionHandler: without taking regions into account.

After that, I build an array of placemarks based on the returned one but only including the nearest ones.

Note that this code doesn't check if there has been an error. I've removed it to make it simpler.

CLLocation *centerLocation = [[CLLocation alloc] initWithLatitude:37.33233141 longitude:-122.03121860];

CLLocationDistance maxDistance = <YOUR_MAX_DISTANCE>;

CLGeocoder *geo = [[CLGeocoder alloc] init];
[geo geocodeAddressString:addressString
        completionHandler:^(NSArray *placemarks, NSError *error)
{
    NSMutableArray *filteredPlacemarks = [[NSMutableArray alloc] init];
    for (CLPlacemark *placemark in placemarks)
    {
        if ([placemark.location distanceFromLocation:centerLocation] <= maxDistance)
        {
            [filteredPlacemarks addObject:placemark];
        }
    }

    // Now do whatever you want with the filtered placemarks.
}];

Alternatively, if you want to keep working with regions: instead of comparing distances with distanceFromLocation:, you can use CLRegion's containsCoordinate:.

Hope it helps.




回答3:


I fixed your answer. You have a mistake where you flipped the GPS coords. If you change them like this, the query works great!

http://maps.googleapis.com/maps/api/geocode/json?bounds=37.832331,-121.531219|36.832331,-122.531219&language=en&address=starbucks&sensor=true



来源:https://stackoverflow.com/questions/10202550/clgeocoder-returning-locations-in-other-countries

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