【问题标题】:CLGeocoder returning locations in other countriesCLGeocoder 在其他国家/地区的返回位置
【发布时间】:2012-04-18 03:55:43
【问题描述】:

我有以下代码:

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);
}];

这将返回菲律宾的星巴克位置,即使该中心位于旧金山的中部。

(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
)

有什么想法吗?

【问题讨论】:

    标签: objective-c ios clgeocoder


    【解决方案1】:

    这是一个对我有用的解决方法。我使用geocodeAddressString: completionHandler: 没有考虑地区。

    之后,我根据返回的地标构建一组地标,但只包括最近的地标。

    请注意,此代码不会检查是否存在错误。我已将其删除以使其更简单。

    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.
    }];
    

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

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      虽然看起来不太可能,但显然 a) Apple 不打算在某个地区附近对公司名称进行转发地理编码,或者 b) 这是一个错误。

      CLGeocoder reference 在其概述中指出:

      转发地理编码请求获取用户可读地址并找到相应的纬度和经度值...

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

      ...描述您要查找的位置的字符串...

      这更加模糊。我尝试通过我的几个项目运行您的代码,以及与它非常相似的代码,我得到了相同的结果。 甚至 Apple 的 GeocoderDemo sample 也显示了这个问题(虽然我住在加利福尼亚,距离你的经纬度示例仅约 150 英里),所以我们俩肯定都没有写过。

      我开始寻求帮助/与您一起解决这个问题!但是,这一点是研究是我所拥有的。祝你好运。

      【讨论】:

      • 我想知道官方的谷歌地图应用程序是如何做到的?当我在应用程序中输入 Starbucks 时,他们似乎做得正确
      • 这完全是在黑暗中拍摄的,但他们可能没有使用 CoreLocation。 Google Maps 应用程序出现在 iOS 1.0 中,CoreLocation 直到 iOS 2.0 才出现,CLGeocoder 直到 iOS 5.0 才出现。也无法说明 Google 可能对 Apple 施加了哪些限制,作为其在 iPhone 上免费(直接)向用户收费的 Google 地图技术的一部分。
      【解决方案3】:

      我修正了你的答案。你有一个错误,你翻转了 GPS 坐标。如果您像这样更改它们,则查询效果很好!

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-05
        • 1970-01-01
        • 2015-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多