【发布时间】:2009-11-23 20:19:52
【问题描述】:
我正在尝试添加到我的程序中,以在 GPS 中定位此人,但将值设置为 该人所在的州例如: GPS 从他/她的 iphone 定位人,然后返回他们所在的州,所以说它的加利福尼亚州,然后将状态变量设置为加利福尼亚州作为字符串,如果有人举个例子,任何帮助表示感谢,谢谢!
【问题讨论】:
标签: iphone geocoding core-location reverse-geocoding
我正在尝试添加到我的程序中,以在 GPS 中定位此人,但将值设置为 该人所在的州例如: GPS 从他/她的 iphone 定位人,然后返回他们所在的州,所以说它的加利福尼亚州,然后将状态变量设置为加利福尼亚州作为字符串,如果有人举个例子,任何帮助表示感谢,谢谢!
【问题讨论】:
标签: iphone geocoding core-location reverse-geocoding
您可以使用Core Location 查找位置,然后使用MKReverseGeocoder 从位置获取状态。
【讨论】:
您需要做的是设置一个 CLLocationManager 来查找您当前的坐标。使用当前坐标,您需要使用 MKReverseGeoCoder 找到您的位置。
- (void)viewDidLoad { // 这将创建 CCLocationManager,它将找到您当前的位置 CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease]; locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters; [locationManager startUpdatingLocation]; } // 当应用程序成功找到您的当前位置时调用此委托 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { // 这将创建一个 MKReverseGeocoder 以使用找到的坐标来查找地标 MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate]; geoCoder.delegate = self; [地理编码器开始]; } // 如果在定位当前位置时发生错误,则调用此委托方法 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"locationManager:%@ didFailWithError:%@", manager, error); } // 当 reverseGeocoder 找到地标时调用此委托 - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark { MKPlacemark * myPlacemark = 地标; // 使用地标,您现在可以检索城市名称 NSString *city = [myPlacemark.addressDictionary objectForKey:(NSString*) kABPersonAddressStateKey]; } // 当反向地理编码器找不到地标时调用此委托 - (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error { NSLog(@"reverseGeocoder:%@ didFailWithError:%@", geocoder, error); }【讨论】:
只是对上述代码的一些更正:
1. 使用 kABPersonAddressStateKey 会给你州,而不是城市
2. 如果您在编译 kABPersonAddressStateKey 时遇到问题,请将 AddressBook 框架添加到您的项目中并包含:
#import <AddressBook/AddressBook.h>
在您的 .m 文件中
【讨论】:
使用反向地理编码是您所需要的,而且很简单。
给定一个包含 CLLocation *location 属性的类,只需使用我为我的一个项目编写的这个代码狙击手,你就完成了。
-(void)loadAddress:(void (^)(NSError *error))completion {
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:self.location
completionHandler:^(NSArray *placemarks, NSError *error) {
NSString *address = nil;
if (error) {
self.placemark = nil;
self.address = NSLocalizedString(@"Unknown address.",@"");
}
else {
CLPlacemark * placeMark = [placemarks firstObject];
self.placemark = placeMark;
NSDictionary *d = placeMark.addressDictionary;
address = [(NSArray*)d[@"FormattedAddressLines"] componentsJoinedByString:@" "];
self.address = address;
}
// Call caller
if (completion) completion(error);
}];
}
请注意,您将对获取 d[@"State"] 而不是 d[@"FormattedAddressLines"] 更感兴趣。 另请注意,反向地理编码需要 Internet 访问(它作为 Web 服务实现)并且受到数量限制。最有可能的是,您每分钟不应超过几个呼叫。如果您超出 Apple 设置的配额,您将收到错误消息。
为方便起见,这里是placeMark.addressDictionary 属性存储的KV:
{
City = Millbrae;
Country = "Etats-Unis";
CountryCode = US;
FormattedAddressLines = (
"I-280 N",
"Half Moon Bay, CA 94019",
"Etats-Unis"
);
Name = "I-280 N";
State = CA;
Street = "I-280 N";
SubAdministrativeArea = "San Mat\U00e9o";
SubLocality = "Bay Area";
Thoroughfare = "I-280 N";
ZIP = 94019;
}
【讨论】: