(1)首先导入CoreLocation.framework
(2)在info.plist文件中添加 NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription
(3)导入头文件<span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:System;font-size:14px;"><span style="font-weight: normal;"><span style="font-family:SimSun;font-size:14px;">#import <CoreLocation/CoreLocation.h></span></span></span></span></span>
(4)遵守协议<span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:System;font-size:14px;"><span style="font-weight: normal;"><span style="font-family:SimSun;font-size:14px;">@interface AppDelegate ()<CLLocationManagerDelegate>
</span></span></span></span></span>
(5)设置属性<span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:System;font-size:14px;"><span style="font-weight: normal;"><span style="font-family:SimSun;font-size:14px;">@property(nonatomic,strong)CLLocationManager *locationManager;</span></span></span></span></span>
(6)调用方法<span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:System;font-size:14px;"><span style="font-weight: normal;"><span style="font-family:SimSun;font-size:14px;">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [self isFirstLaunchApp] ? self.firstLaunchVC : self.mainVC;
[self setAppearanceColor]; // 设置Navigateion和Tabbar
[self locate]; // 开始定位
[self.window makeKeyAndVisible];
return YES;
}</span></span></span></span></span>
(7)实现方法<span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:System;font-size:14px;"><span style="font-weight: normal;"><span style="font-family:SimSun;font-size:14px;">- (void)locate{
// 判断定位操作是否被允许
if([CLLocationManager locationServicesEnabled]) {
//定位初始化
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 10;
[_locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据(iOS8定位需要)
[_locationManager startUpdatingLocation];//开启定位
}else {
NSLog(@"用户没有允许定位");
}
// 开始定位
[_locationManager startUpdatingLocation];
}</span></span></span></span></span>
(8)实现代理方法<span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:System;font-size:14px;"><span style="font-weight: normal;"><span style="font-family:SimSun;font-size:14px;">//实现定位协议回调方法
#pragma mark - CoreLocation Delegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
// 获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根据经纬度反向地理编译出地址信息
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error){
if (array.count > 0){
CLPlacemark *placemark = [array objectAtIndex:0];
//将获得的所有信息显示到label上
NSLog(@"当前的位置信息 : %@",placemark.name);
//获取省份和城市
NSString *province = placemark.administrativeArea;
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
NSString *cityName = [NSString stringWithFormat:@"%@%@",province,city];//拼接所需的字符串
NSLog(@"当前的省份及城市为 : %@",cityName);
}else if (error == nil && [array count] == 0){
NSLog(@"No results were returned.");
}else if (error != nil){
NSLog(@"An error occurred = %@", error);
}
}];
//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
[manager stopUpdatingLocation];
}</span></span></span></span></span>
<span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:System;font-size:14px;"><span style="font-weight: normal;"><span style="font-family:SimSun;font-size:14px;">#import <CoreLocation/CoreLocation.h></span></span></span></span></span>
<span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:System;font-size:14px;"><span style="font-weight: normal;"><span style="font-family:SimSun;font-size:14px;">@interface AppDelegate ()<CLLocationManagerDelegate>
</span></span></span></span></span>
<span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:System;font-size:14px;"><span style="font-weight: normal;"><span style="font-family:SimSun;font-size:14px;">@property(nonatomic,strong)CLLocationManager *locationManager;</span></span></span></span></span>
<span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:System;font-size:14px;"><span style="font-weight: normal;"><span style="font-family:SimSun;font-size:14px;">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [self isFirstLaunchApp] ? self.firstLaunchVC : self.mainVC;
[self setAppearanceColor]; // 设置Navigateion和Tabbar
[self locate]; // 开始定位
[self.window makeKeyAndVisible];
return YES;
}</span></span></span></span></span>
<span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:System;font-size:14px;"><span style="font-weight: normal;"><span style="font-family:SimSun;font-size:14px;">- (void)locate{
// 判断定位操作是否被允许
if([CLLocationManager locationServicesEnabled]) {
//定位初始化
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 10;
[_locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据(iOS8定位需要)
[_locationManager startUpdatingLocation];//开启定位
}else {
NSLog(@"用户没有允许定位");
}
// 开始定位
[_locationManager startUpdatingLocation];
}</span></span></span></span></span>
<span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:Times New Roman;font-size:14px;font-weight: normal;"><span style="font-family:System;font-size:14px;"><span style="font-weight: normal;"><span style="font-family:SimSun;font-size:14px;">//实现定位协议回调方法
#pragma mark - CoreLocation Delegate
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
//此处locations存储了持续更新的位置坐标值,取最后一个值为最新位置,如果不想让其持续更新位置,则在此方法中获取到一个值之后让locationManager stopUpdatingLocation
CLLocation *currentLocation = [locations lastObject];
// 获取当前所在的城市名
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
//根据经纬度反向地理编译出地址信息
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *array, NSError *error){
if (array.count > 0){
CLPlacemark *placemark = [array objectAtIndex:0];
//将获得的所有信息显示到label上
NSLog(@"当前的位置信息 : %@",placemark.name);
//获取省份和城市
NSString *province = placemark.administrativeArea;
NSString *city = placemark.locality;
if (!city) {
//四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
city = placemark.administrativeArea;
}
NSString *cityName = [NSString stringWithFormat:@"%@%@",province,city];//拼接所需的字符串
NSLog(@"当前的省份及城市为 : %@",cityName);
}else if (error == nil && [array count] == 0){
NSLog(@"No results were returned.");
}else if (error != nil){
NSLog(@"An error occurred = %@", error);
}
}];
//系统会一直更新数据,直到选择停止更新,因为我们只需要获得一次经纬度即可,所以获取之后就停止更新
[manager stopUpdatingLocation];
}</span></span></span></span></span>