iOS獲取當前定位位置經緯度及城市

.m

#import <CoreLocation/CoreLocation.h>

@interface HomeViewController ()<CLLocationManagerDelegate>{

NSString* currentCity;//當前城市

}

@property (strong, nonatomic) CLLocationManager* locationManager;?

- (void)viewDidLoad {

? ? [super viewDidLoad];

? ? // Do any additional setup after loading the view.

? ? [self startLocation];

}

#pragma mark---------判斷定位操作是否被允許-----------

-(void)startLocation{

if ([CLLocationManager locationServicesEnabled]) {//判斷定位操作是否被允許


? ? ? ? self.locationManager= [[CLLocationManager alloc]init];


? ? ? ? self.locationManager.delegate = self;//遵循代理


? ? ? ? self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;


? ? ? ? self.locationManager.distanceFilter = 10.0f;


? ? ? ? [_locationManager requestWhenInUseAuthorization];//使用程序其間允許訪問位置數據(iOS8以上版本定位需要)


? ? ? ? [self.locationManager startUpdatingLocation];//開始定位


? ? }else{//不能定位用戶的位置的情況再次進行判斷,并給與用戶提示


? ? ? ? //1.提醒用戶檢查當前的網絡狀況


? ? ? ? //2.提醒用戶打開定位開關

? ? ? ? ? ? UIAlertController* alertVC = [UIAlertControlleralertControllerWithTitle:@"允許\"定位\"提示"message:@"請在設置中打開定位"preferredStyle:UIAlertControllerStyleAlert];

? ? ? ? ? ? UIAlertAction* ok = [UIAlertActionactionWithTitle:@"打開定位"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {

? ? ? ? ? ? ? ? //打開定位設置

? ? ? ? ? ? ? ? NSURL*settingsURL = [NSURLURLWithString:UIApplicationOpenSettingsURLString];

? ? ? ? ? ? ? ? [[UIApplicationsharedApplication]openURL:settingsURL];

? ? ? ? ? ? }];

? ? ? ? ? ? UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {


? ? ? ? ? ? }];

? ? ? ? ? ? [alertVCaddAction:cancel];

? ? ? ? ? ? [alertVCaddAction:ok];

? ? ? ? ? ? [self presentViewController:alertVC animated:YES completion:nil];


? ? }


}

#pragma mark---------定位成功---------

- (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray*)locations{


? ? //當前所在城市的坐標值

? ? CLLocation*currLocation = [locations lastObject];


?? // NSLog(@"當前經度=%f 當前緯度=%f 當前高度=%f", currLocation.coordinate.latitude, currLocation.coordinate.longitude, currLocation.altitude);

? ? //根據經緯度反向地理編譯出地址信息

? ? CLGeocoder* geoCoder = [[CLGeocoderalloc]init];


? ? [geoCoderreverseGeocodeLocation:currLocationcompletionHandler:^(NSArray*placemarks,NSError*error) {


? ? ? ? ? ? ? ? if(placemarks.count>0) {

? ? ? ? ? ? ? ? ? ? CLPlacemark*placeMark = placemarks[0];

? ? ? ? ? ? ? ? ? ? currentCity= placeMark.locality;

? ? ? ? ? ? ? ? ? ? if(!currentCity) {

? ? ? ? ? ? ? ? ? ? ? ? currentCity=@"無法定位當前城市";

? ? ? ? ? ? ? ? ? ? }


?? ? ? ? ? ? ? ? ? // NSLog(@"%@",currentCity); //這就是當前的城市

? ? ? ? ? ? ? ? ?// ? NSLog(@"%@",placeMark.name);//具體地址:? xx市xx區(qū)xx街道

? ? ? ? ? ? ? ? //


//? ? ? ? for (CLPlacemark * placemark in placemarks) {

//

//? ? ? ? ? ? NSDictionary *address = [placemark addressDictionary];

//

//? ? ? ? ? ? //? Country(國家)? State(省)? City(市)

//? ? ? ? ? ? NSLog(@"#####%@",address);//所有返回屬性


//

//? ? ? ? ? ? NSLog(@"%@", [address objectForKey:@"Country"]);

//

//? ? ? ? ? ? NSLog(@"%@", [address objectForKey:@"State"]);

//

//? ? ? ? ? ? NSLog(@"%@", [address objectForKey:@"City"]);

//

//? ? ? ? }


? ? }];


}

#pragma mark---------定位失敗-----------

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


? ? if([errorcode] ==kCLErrorDenied){

? ? ? ? //訪問被拒絕

? ? ? ? ALERT(@"提示", @"位置訪問被拒絕");

? ? }

? ? if ([error code] == kCLErrorLocationUnknown) {

? ? ? ? //無法獲取位置信息

? ? ? ? ALERT(@"提示", @"無法獲取位置信息");

? ? }

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容