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

时间:2012-05-03 23:17:42

标签: iphone ios geolocation core-location clgeocoder

我可以成功地通过locality的{​​{1}}方法获得结果(例如ISOcountryCodeCLGeocoder等)。
但是我怎样才能将这个地方与结果相匹配?

例如:如果结果的城市(地区)是reverseGeocodeLocation:completionHandler:,我只需使用

即可匹配
Hangzhou City

但是如你所知,有数百万个城市,我们不可能逐一将城市名称和硬编码放入我的应用程序中。

那么,有什么方法可以解决这个问题吗?或者是否存在任何框架?或者只有几个文件包含国家/地区和城市的名称与if ([placemark.locality isEqualToString:@"Hangzhou City"]) {...} 的结果相匹配?即使是模糊的坐标匹配解决方案也是可以的(我的意思是,一个城市有自己的区域,我可以通过坐标确定城市,但我现在仍然需要获得每个城市的区域)。


部署目标iOS5.0

1 个答案:

答案 0 :(得分:1)

有一种更简单的方法,您可以使用反向GeocodeLocation来获取该地点的信息。你必须知道这不适用于每个城市的想法。 有关更多信息,请查看Apple的CLGeocoder Class ReferenceGeocoding Location Data文档。

因此,您可以创建和处理服务的对象

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

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

@property (nonatomic,retain) CLLocationManager *locationManager;

@end

和实施

#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