简体   繁体   English

如何将地点与CLGeocoder的结果匹配?

[英]How can I match the place with the result of CLGeocoder?

I can get the result (like locality , ISOcountryCode , etc) by CLGeocoder 's reverseGeocodeLocation:completionHandler: method successfully. 我可以通过CLGeocoderreverseGeocodeLocation:completionHandler:方法成功获得结果(例如localityISOcountryCode等)。
But how can I match the place with the result? 但是,如何将地点与结果相匹配?

eg: If the city (locality) of result is Hangzhou City , I can match it simply by using 例如:如果结果的城市(地点)是Hangzhou City ,我可以简单地通过使用来匹配它

if ([placemark.locality isEqualToString:@"Hangzhou City"]) {...}

But as you know, there're millions of cities, it's impossible to get the city name one by one and hard code into my app. 但是,正如您所知,有数百万个城市,不可能一一获得城市名称并将硬代码添加到我的应用中。

So, is there any way to solve this problem? 那么,有什么办法可以解决这个问题? Or does there any framework exist? 还是存在任何框架? Or just several files contain countries & cities' name that match the CLGeocoder 's result? 还是只有几个文件包含与CLGeocoder的结果相匹配的国家和城市的名称? Even fuzzy coordinate matching solution is okay (I mean, a city has its own region, and I can determine the city just by coordinate, but I still need to get every city's region area at the moment). 甚至模糊坐标匹配解决方案都是可以的(我的意思是,一个城市有自己的区域,我可以仅通过坐标来确定城市,但此刻我仍然需要获取每个城市的区域面积)。


Deployment Target iOS5.0 部署目标iOS5.0

Well there is a easier way, you can use the reverse GeocodeLocation to get the information of the place. 好了,有一种更简单的方法,您可以使用相反的GeocodeLocation来获取地点的信息。 You have to know this won't work in every city thought. 您必须知道这并非在所有城市思想中都有效。 For more information check Apple's CLGeocoder Class Reference and Geocoding Location Data documentation. 有关更多信息,请参阅 Apple的CLGeocoder类参考地理编码位置数据文档。

So you can create and object that handle the service 因此,您可以创建和处理该服务的对象

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

@interface locationUtility : NSObject<CLLocationManagerDelegate>{
  CLLocationManager *locationManager;
  CLPlacemark *myPlacemark;
  CLGeocoder * geoCoder;
}

@property (nonatomic,retain) CLLocationManager *locationManager;

@end

and the implementation 和实施

#import "locationUtility.h"

@implementation locationUtility
@synthesize locationManager;

#pragma mark - Init
-(id)init {
  NSLog(@"locationUtility - init");
  self=[super init];

  locationManager = [[CLLocationManager alloc] init];
  locationManager.delegate = self;
  locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  locationManager.distanceFilter = kCLDistanceFilterNone;
  [locationManager startMonitoringSignificantLocationChanges];
  geoCoder= [[CLGeocoder alloc] init];
  return self;
}

- (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation
            fromLocation:(CLLocation *) oldLocation {
  [geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
     CLPlacemark *placemark = [placemarks objectAtIndex:0];
     myPlacemark=placemark; 
     // Here you get the information you need  
     // placemark.country;
     // placemark.administrativeArea;
     // placemark.subAdministrativeArea;
     // placemark.postalCode];
    }];
}

-(void) locationManager:(CLLocationManager *) manager didFailWithError:(NSError *) error {
  NSLog(@"locationManager didFailWithError: %@", error.description);
}

@end

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

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