項(xiàng)目中用到了城市定位亲茅,獲取所在城市的經(jīng)緯度以及名稱。由于沒有用到其他更復(fù)雜的地圖功能,所以就選擇了用系統(tǒng)自帶的定位來(lái)獲取當(dāng)前經(jīng)緯度與地理信息氛悬。下面直接上代碼。
1.在info.plist中加入以下兩個(gè)字段
允許在前臺(tái)使用時(shí)獲取GPS的描述
定位權(quán)限:Privacy - Location When In Use Usage Description
允許永久使用GPS描述
定位權(quán)限: Privacy - Location Always Usage Description
如下圖:
2.加入framework包:CoreLocation.framework如下圖:
3.1 在.m中引入CoreLocation/CoreLocation.h頭文件耘柱,并且遵循CLLocationManagerDelegate代理如捅。然后定義一個(gè)CLLocationManager對(duì)象,并且聲明變量调煎,代碼如下:
#import "KYAchievementManagerVC.h"
@interface JBHomeVC ()<CLLocationManagerDelegate>
{
CLLocationManager*locationmanager;//定位服務(wù)
NSString*strlatitude;//經(jīng)度
NSString*strlongitude;//緯度
}
3.2 調(diào)用方法
- (void)viewDidLoad {
[super viewDidLoad];
[self startLocation];
}
3.3 開始定位
#pragma mark - 定位
//開始定位
-(void) startLocation
{
//判斷定位功能是否打開
if ([CLLocationManager locationServicesEnabled]) {
locationmanager = [[CLLocationManager alloc]init];
locationmanager.delegate = self;
[locationmanager requestAlwaysAuthorization];
[locationmanager requestWhenInUseAuthorization];
//設(shè)置尋址精度
locationmanager.desiredAccuracy = kCLLocationAccuracyBest;
locationmanager.distanceFilter = 5.0;
[locationmanager startUpdatingLocation];
}
}
3.4 定位失敗后的代理方法
#pragma mark CoreLocation delegate (定位失敗)
//定位失敗后調(diào)用此代理方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
//設(shè)置提示提醒用戶打開定位服務(wù)
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"允許定位提示" message:@"請(qǐng)?jiān)谠O(shè)置中打開定位" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"打開定位" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alert addAction:okAction];
[alert addAction:cancelAction];
[self presentViewController:alert animated:YES completion:nil];
}
3.5 定位成功后的代理方法
#pragma mark 定位成功后則執(zhí)行此代理方法
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
[locationmanager stopUpdatingHeading];
//舊址
CLLocation *currentLocation = [locations lastObject];
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
//打印當(dāng)前的經(jīng)度與緯度
NSLog(@"%f,%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
//反地理編碼
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error)
{
NSLog(@"反地理編碼");
NSLog(@"反地理編碼%ld",placemarks.count);
if (placemarks.count > 0) {
CLPlacemark *placeMark = placemarks[0];
self.label_city.text = placeMark.locality;
if (!self.label_city.text) {
self.label_city.text = @"無(wú)法定位當(dāng)前城市";
}
/*看需求定義一個(gè)全局變量來(lái)接收賦值*/
NSLog(@"城市----%@",placeMark.country);//當(dāng)前國(guó)家
NSLog(@"城市%@",self.label_city.text);//當(dāng)前的城市
NSLog(@"%@",placeMark.subLocality);//當(dāng)前的位置
NSLog(@"%@",placeMark.thoroughfare);//當(dāng)前街道
NSLog(@"%@",placeMark.name);//具體地址
}
}];
}
- 打印數(shù)據(jù)
- 修改模擬器經(jīng)緯度
6.修改模擬器語(yǔ)言
Setting(模擬器中的應(yīng)用程序)- General - Language & Region - iPhone Language - 簡(jiǎn)體中文