iOS: CoreLocation實(shí)現(xiàn)定位當(dāng)前城市

首先導(dǎo)入頭文件 : #import<CoreLocation/CoreLocation.h>

在info.plist文件中添加:

注意:

1.[CLLocationManager locationServicesEnabled]判斷定位是否開(kāi)啟是判斷的整個(gè)手機(jī)系統(tǒng)的定位是否打開(kāi),并不是針對(duì)這一應(yīng)用.

2.具體在本應(yīng)用中的是否已經(jīng)授權(quán)定位要通過(guò)代理方法判斷

如果定位失敗(未授權(quán))則會(huì)執(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];?

//開(kāi)始定位? ? ? ? ? ??

[self.locationManager startUpdatingLocation];? ? ? ? ? ??

//設(shè)置開(kāi)始識(shí)別方向? ? ? ? ? ?

[self.locationManager startUpdatingHeading]; ? ?

}

}else{ ? ??

UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:nil ? message:@"您沒(méi)有開(kāi)啟定位功能" ? ? ? ? ? ? ? ?delegate:nil ? ?cancelButtonTitle:@"確定" ? otherButtonTitles:nil, nil]; ?

[alertView show];? ?

}

}

#pragma mark --- 如定位失敗(未授權(quán))則會(huì)執(zhí)行次函數(shù)方法 ---

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{? ?

?UIAlertController * alertVC = [UIAlertController alertControllerWithTitle:@"允許\"定位\"提示" message:@"請(qǐng)?jiān)谠O(shè)置中打開(kāi)定位" preferredStyle:UIAlertControllerStyleAlert];? ??

UIAlertAction * ok = [UIAlertAction actionWithTitle:@"打開(kāi)定位" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {? ? ? ?

//打開(kāi)定位設(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 = @"無(wú)法定位當(dāng)前城市";

}

self.cityL.text = self.currentCity;

if (![self.state isEqualToString:@"跳首頁(yè)企業(yè)包團(tuán)"]) {

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);

}

}];

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容