CLGeocoder返回其他国家/地区的位置

时间:2020-12-07 09:25:28

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?

3 个解决方案

#1


2  

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.

虽然看起来似乎不太可能,但似乎a)苹果公司并不打算在一个地区附近进行前沿地理编码业务名称,或者b)这是一个错误。

The CLGeocoder reference, in its overview, states:

CLGeocoder参考资料在其概述中指出:

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:

这肯定意味着一个真实的物理地址作为搜索字符串(尤其是“用户可读地址”部分)。但是,geocodeAddressString:inRegion:completionHandler:的文档声明搜索字符串是:

... 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.

哪个更模糊。我尝试运行你的代码,代码与它非常相似,通过我的几个项目得到了相同的结果。即使是Apple的GeocoderDemo样本也会出现这个问题(虽然我住在加利福尼亚州距离你的长/长的例子只有150英里),所以我们当中没有人写过。

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


4  

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

这是一个适合我的解决方法。我正在使用geocodeAddressString:completionHandler:不考虑区域。

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:.

或者,如果您想继续使用区域:而不是使用distanceFromLocation:来比较距离,您可以使用CLRegion的containsCoordinate:。

Hope it helps.

希望能帮助到你。

#3


0  

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

我修好了你的答案。您在翻转GPS坐标时出错。如果你这样更改它们,查询效果很好!

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

#1


2  

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.

虽然看起来似乎不太可能,但似乎a)苹果公司并不打算在一个地区附近进行前沿地理编码业务名称,或者b)这是一个错误。

The CLGeocoder reference, in its overview, states:

CLGeocoder参考资料在其概述中指出:

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:

这肯定意味着一个真实的物理地址作为搜索字符串(尤其是“用户可读地址”部分)。但是,geocodeAddressString:inRegion:completionHandler:的文档声明搜索字符串是:

... 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.

哪个更模糊。我尝试运行你的代码,代码与它非常相似,通过我的几个项目得到了相同的结果。即使是Apple的GeocoderDemo样本也会出现这个问题(虽然我住在加利福尼亚州距离你的长/长的例子只有150英里),所以我们当中没有人写过。

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


4  

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

这是一个适合我的解决方法。我正在使用geocodeAddressString:completionHandler:不考虑区域。

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:.

或者,如果您想继续使用区域:而不是使用distanceFromLocation:来比较距离,您可以使用CLRegion的containsCoordinate:。

Hope it helps.

希望能帮助到你。

#3


0  

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

我修好了你的答案。您在翻转GPS坐标时出错。如果你这样更改它们,查询效果很好!

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