【问题标题】:Using CLGeocoder to find the nearest city使用 CLGeocoder 查找最近的城市
【发布时间】:2013-01-27 20:53:16
【问题描述】:

所以我一直在研究这个问题,但我似乎无法在任何地方找到解决方案。我正在尝试在 iOS 应用程序中使用CLGeocoder 来查找用户在地图上长按的最近城市。我有两个主要问题:

  1. 即使用户非常缩小,并且用户点击说纽约(这是他们想要的),由于地图的缩小程度,CLGeocoder 经常返回附近的城市而不是更明显的城市纽约的回答。我不知道如何设置搜索的“模糊性”来克服这个问题。
  2. 在许多情况下,返回的地标对象中的City 字段为空,最常见于沙漠或海洋等偏远地区。在这种情况下,理想情况下,我希望找到实际上定义了 City 的最近对象,但不知道如何执行此操作。

【问题讨论】:

  • 澄清一下,我做了更多的测试,似乎 Apple Maps 或 Google Maps for iOS 都没有解决这个问题。两者都会为丢弃的图钉提供街道特定地址,即使缩小到您不可能瞄准街道。

标签: iphone ios mapkit clgeocoder


【解决方案1】:

使用CLGeocoder获取您位置的所有详细信息

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    [currentLocation stopUpdatingLocation];

    CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:latitudeValue longitude:longitudeValue];


    CLGeocoder * geoCoder = [[CLGeocoder alloc] init];

    [geoCoder reverseGeocodeLocation:myLocation completionHandler:^(NSArray *placemarks, NSError *error) {

        if(!error)
        {
            for (CLPlacemark * placemark in placemarks)
            {
                NSLog(@"placemark.ISOcountryCode %@",placemark.ISOcountryCode);
                NSLog(@"placemark.country %@",placemark.country);
                NSLog(@"placemark.postalCode %@",placemark.postalCode);
                NSLog(@"placemark.administrativeArea %@",placemark.administrativeArea);
                NSLog(@"placemark.locality %@",placemark.locality);
                NSLog(@"placemark.subLocality %@",placemark.subLocality);
                NSLog(@"placemark.subThoroughfare %@",placemark.subThoroughfare);

            }

        }
        else
        {
            NSLog(@"failed getting city: %@", [error description]);
        }

    }];


}

【讨论】:

  • 谢谢,但您误解了我的问题。我的 CLGeocoder 运行良好,我只是想知道如何定位最近的城市,而不是城镇或区域。
猜你喜欢
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 2019-04-21
  • 2022-10-08
  • 1970-01-01
  • 2012-04-27
  • 2011-05-22
  • 1970-01-01
相关资源
最近更新 更多