首先导入头文件 : #import<CoreLocation/CoreLocation.h>
在info.plist文件中添加:
注意:
1.[CLLocationManager locationServicesEnabled]判断定位是否开启是判断的整个手机系统的定位是否打开,并不是针对这一应用.
2.具体在本应用中的是否已经授权定位要通过代理方法判断
如果定位失败(未授权)则会执行此代理方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
}
代码如下:
@interface ViewController (){
CLLocationManager * locationManager;
NSString * currentCity; //当前城市
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self locate];
}
#pragma mark --------------------- 定位 -----------------------
-(void)lacate{
if([CLLocationManager locationServicesEnabled]){
if(!_locationManager){
self.locationManager = [[CLLocationManager alloc] init];
if(
[self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager requestAlwaysAuthorization];
}
//设置代理
[self.locationManager setDelegate:self];
//设置定位精度
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//设置距离筛选
[self.locationManager setDistanceFilter:100];
//开始定位
[self.locationManager startUpdatingLocation];
//设置开始识别方向
[self.locationManager startUpdatingHeading];
}
}else{
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil message:@"您没有开启定位功能" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alertView show];
}
}
#pragma mark --- 如定位失败(未授权)则会执行次函数方法 ---
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允许\"定位\"提示" message:@"请在设置中打开定位" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打开定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//打开定位设置
NSURL *settingsURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:settingsURL];
}];
UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { }];
[alertVC addAction:cancel];
[alertVC addAction:ok];
[self presentViewController:alertVC animated:YES completion:nil];
}
//定位成功
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray*)locations {
[self.locationManager stopUpdatingLocation];
CLLocation *currentLocation = [locations lastObject];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
//反编码
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray* _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count > 0) {
CLPlacemark *placeMark = placemarks[0];
self.currentCity = placeMark.locality;
if (!self.currentCity) {
self.currentCity = @"无法定位当前城市";
}
self.cityL.text = self.currentCity;
if (![self.state isEqualToString:@"跳首页企业包团"]) {
self.placeTF.text = self.currentCity;
}
NSLog(@"%@",self.currentCity); //这就是当前的城市
NSLog(@"%@",placeMark.name);//具体地址: xx市xx区xx街道
}
else if (error == nil && placemarks.count == 0) {
NSLog(@"No location and error return");
}
else if (error) {
NSLog(@"location error: %@ ",error);
}
}];
}