是否有可能通过给定的地理坐标集(例如通过Google地图等网络服务或其他第三方)获得对人类最友好的有意义地标(例如街道交叉路口)的文字描述?我正在使用MapKit,但我怀疑它是否内置任何内容。Turns out it does.
例如,我正在寻找类似最近的街道交叉口(实际上这是理想的选择)或任何种类的“地名”之类的东西。像:
E Hastings St and Main St
128 W Cordova St
PNE Fairgrounds
最佳答案
因此,我完成了一个遇到此确切问题的项目...您可以参考apple开发人员网页中的CLPlacemark class reference。
这是我使用它的方式,我想您也必须像我一样向项目添加corelocation库。
#import <CoreLocation/CoreLocation.h>
现在在
.h file:
添加CLLocation委托:
<CLLocationManagerDelegate>
然后添加以下实例变量:
CLLocationManager *locationManager;
CLLocation *currentLocation;
现在在
.m file:
-(void) viewDidLoad{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move, location is updated
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // get best current locaton coords
locationManager.headingFilter = 1;
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
}
现在,在
CLLocationManager
方法之后或之前,以您认为合适的方式实现viewDidLoad
委托方法:#pragma mark CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
currentLocation = [locations objectAtIndex:0];
[locationManager stopUpdatingLocation];
NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
[geocoder reverseGeocodeLocation:currentLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
if (error){
NSLog(@"Geocode failed with error: %@", error);
return;
}
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"City = %@ : State = %@ : Country = %@ : Zip Code = %@", placemark.locality, placemark.administrativeArea, placemark.ISOcountryCode, placemark.postalCode);
}];
}
运行代码,并确保如果您使用iOS模拟器,请确保通过单击控制台输出屏幕上方的箭头按钮来模拟位置。
仅在构建并运行项目后才会显示。
希望这对您有帮助:)
这是我得到的输出:
2013-08-02 20:39:46.935 PowerOneApp[3449:c07] Detected Location : 37.785834, -122.406417
2013-08-02 20:39:49.773 PowerOneApp[3449:c07] City = San Francisco : State = California : Country = US : Zip Code = 94115
编辑
抱歉,写此答案时,我意识到您需要更多精确的坐标。
这里:
#pragma mark CLLocationManager Delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
currentLocation = [locations objectAtIndex:0];
[locationManager stopUpdatingLocation];
NSLog(@"Detected Location : %f, %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
[geocoder reverseGeocodeLocation:currentLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
if (error){
NSLog(@"Geocode failed with error: %@", error);
return;
}
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"%@", placemark.addressDictionary);
NSLog(@"%@", [placemark.addressDictionary valueForKey:@"Street"]);
}];
}
继承人的输出:
2013-08-02 20:44:26.052 PowerOneApp[3531:c07] Detected Location : 37.785834, -122.406417
2013-08-02 20:44:26.340 PowerOneApp[3531:c07] {
City = "San Francisco";
Country = "United States";
CountryCode = US;
FormattedAddressLines = (
"Apple Store, San Francisco",
"1800 Ellis St",
"San Francisco, CA 94115-4004",
"United States"
);
Name = "Apple Store, San Francisco";
PostCodeExtension = 4004;
State = California;
Street = "1800 Ellis St";
SubAdministrativeArea = "San Francisco";
SubLocality = "Union Square";
SubThoroughfare = 1800;
Thoroughfare = "Ellis St";
ZIP = 94115;
}
2013-08-02 20:44:26.340 PowerOneApp[3531:c07] 1800 Ellis St
如您所见,有一个名为
addressDictionary
的属性,该属性返回CLPlacemark类拥有的所有属性。因此,在这里,如果您熟悉NSDictionary
对象,那么我只是先输出整个字典,然后指定要记录的值。希望这可以帮助您解决确切的问题:)