首先導(dǎo)入頭文件 : #import<CoreLocation/CoreLocation.h>
在info.plist文件中添加:
注意:
1.[CLLocationManager locationServicesEnabled]判斷定位是否開啟是判斷的整個手機系統(tǒng)的定位是否打開,并不是針對這一應(yīng)用.
2.具體在本應(yīng)用中的是否已經(jīng)授權(quán)定位要通過代理方法判斷
如果定位失敗(未授權(quán))則會執(zhí)行此代理方法
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
}
代碼如下:
@interface ViewController (){
CLLocationManager * locationManager;
NSString * currentCity; //當(dāng)前城市
}
@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];? ? ? ? ?
}? ? ? ? ??
//設(shè)置代理? ? ? ??
[self.locationManager setDelegate:self]; ? ? ? ??
//設(shè)置定位精度 ? ?
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];? ? ? ? ? ??
//設(shè)置距離篩選? ? ? ? ? ??
[self.locationManager setDistanceFilter:100];?
//開始定位? ? ? ? ? ??
[self.locationManager startUpdatingLocation];? ? ? ? ? ??
//設(shè)置開始識別方向? ? ? ? ? ?
[self.locationManager startUpdatingHeading]; ? ?
}
}else{ ? ??
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil ? message:@"您沒有開啟定位功能" ? ? ? ? ? ? ? ?delegate:nil ? ?cancelButtonTitle:@"確定" ? otherButtonTitles:nil, nil]; ?
[alertView show];? ?
}
}
#pragma mark --- 如定位失敗(未授權(quán))則會執(zhí)行次函數(shù)方法 ---
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{? ?
?UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允許\"定位\"提示" message:@"請在設(shè)置中打開定位" preferredStyle:UIAlertControllerStyleAlert];? ??
UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打開定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {? ? ? ?
//打開定位設(shè)置? ??
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 = @"無法定位當(dāng)前城市";
}
self.cityL.text = self.currentCity;
if (![self.state isEqualToString:@"跳首頁企業(yè)包團"]) {
self.placeTF.text = self.currentCity;
}
NSLog(@"%@",self.currentCity); //這就是當(dāng)前的城市
NSLog(@"%@",placeMark.name);//具體地址:? xx市xx區(qū)xx街道
}
else if (error == nil && placemarks.count == 0) {
NSLog(@"No location and error return");
}
else if (error) {
NSLog(@"location error: %@ ",error);
}
}];
}