简体   繁体   English

使用CLGeocoder查找最近的城市

[英]Using CLGeocoder to find the nearest city

So I've been looking at this for a while, and I can't seem to find a solution anywhere. 因此,我已经研究了一段时间,而且似乎无法在任何地方找到解决方案。 I am trying to use CLGeocoder in an iOS app to find the nearest city to where the user long taps on the map. 我正在尝试在iOS应用中使用CLGeocoder来查找用户在地图上长按的最近的城市。 I have two main problems: 我有两个主要问题:

  1. Even if the user is very zoomed out, and the user taps on say New York (which is what they intend), because of how zoomed out the map is, CLGeocoder often returns a nearby city instead of the more obvious answer of New York. 即使用户非常放大,并且用户轻按了纽约(这就是他们想要的),但由于地图的缩小程度, CLGeocoder经常返回附近的城市,而不是纽约的更明显答案。 I can't work out how to set the "fuzziness" of the search to overcome this issue. 我无法解决如何设置搜索的“模糊性”以克服此问题。
  2. In many instances the City field is null in the returned placemark object, most commonly in remote areas such as deserts or oceans. 在许多情况下,返回的地标对象中的“ City字段为空,最常见的是在沙漠或海洋等偏远地区。 In this case, I would ideally want to find the nearest object that actually has City defined, but can't work out how to do this. 在这种情况下,理想情况下,我希望找到实际定义了City的最近的对象,但无法解决该问题。

For Get all Detail of your Location by using CLGeocoder 通过使用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]);
        }

    }];


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM