【问题标题】:iOS Get User Location as NSStringiOS 获取用户位置作为 NSString
【发布时间】:2013-06-16 14:44:49
【问题描述】:
我想在我的应用程序中完成的是获取当前用户位置并将其显示在屏幕上的 UILabel 中。我想要一个当前用户位置的NSString,其格式类似于:@"City, State/Country"。这将是应用启动时的一次性操作。
我之前没有 iOS 定位方面的经验,我想就此获得一些建议 - 我确信这是一项非常简单的任务。
【问题讨论】:
标签:
ios
cocoa-touch
nsstring
core-location
【解决方案1】:
流程如下:
将CoreLocation.framework 添加到您的项目中。见Linking to a Library or a Framework。如果您想使用我在下面使用的地址簿常量,您可能还想将AddressBook.framework 添加到您的项目中。
Start location services。为此,“显着变化”服务(准确度较低,但功耗较低)可能足以达到城市级别的准确度。
当位置管理器通知您用户的位置时,然后perform a reverse geocode 该位置。
停止定位服务。
因此,这可能看起来像:
#import <CoreLocation/CoreLocation.h>
#import <AddressBook/AddressBook.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[self startSignificantChangeUpdates];
}
- (void)startSignificantChangeUpdates
{
if ([CLLocationManager locationServicesEnabled])
{
if (!self.locationManager)
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startMonitoringSignificantLocationChanges];
}
}
- (void)stopSignificantChangesUpdates
{
[self.locationManager stopUpdatingLocation];
self.locationManager = nil;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations lastObject];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary = [placemark addressDictionary];
NSString *city = addressDictionary[(NSString *)kABPersonAddressCityKey];
NSString *state = addressDictionary[(NSString *)kABPersonAddressStateKey];
NSString *country = placemark.country;
self.label.text = [NSString stringWithFormat:@"%@, %@, %@", city, state, country];
}];
[self stopSignificantChangesUpdates];
}
请注意,位置管理器的位置通知取决于用户是否选择与您的应用共享,并且即使在最佳情况下也是异步发生的。同样,反向地理编码也是异步发生的。
参见位置感知编程指南中的Getting User Location。
【解决方案2】:
使用-reverseGeocodeLocation:completionHandler: 中的CLGeocoder。
试试这个代码 sn-p,唯一的窍门是你从地理编码器返回的CLPlacemark(有关可用信息,请参阅Documentation)有一堆并不总是一致的信息,这是我在一个较旧项目中的尝试之一,试图测试位置、街道名称等...用你的用例进行测试以找到一个好的匹配:
- (void)getLocationStringForCoordinates:(CLLocationCoordinate2D)coordinates {
if ( CLLocationCoordinate2DIsValid(coordinates) ) {
CLLocation *photoLocation = [[CLLocation alloc] initWithLatitude:coordinates.latitude longitude:coordinates.longitude];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:photoLocation
completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark *locationPlacemark = [placemarks lastObject];
// Location (popular name, street, area)
NSString *location = locationPlacemark.subLocality ? locationPlacemark.subLocality : (locationPlacemark.name ? locationPlacemark.name : locationPlacemark.thoroughfare);
// sometimes the location can be the same
// as the city name (for small villages), if so
// make sure location is nil to skip it
// else if
// the location name is not being used but is very short 9less then 20 letters, use that instead
if([locationPlacemark.name isEqualToString:locationPlacemark.locality] && [location isEqualToString:locationPlacemark.name])
location = @"";
else if ( ![locationPlacemark.name isEqualToString:location] && locationPlacemark.name.length < 20 )
location = locationPlacemark.name;
// city
NSString *city = locationPlacemark.subAdministrativeArea ? locationPlacemark.subAdministrativeArea : locationPlacemark.locality;
city = city.length > 0 ? [@", " stringByAppendingString:city] : city;
NSString *locationName = [NSString stringWithFormat:@"%@%@", location, city];
}];
}
}