第一步.添加庫(kù) CoreLocation.framework
第二步.在info.plist文件里添加 這兩個(gè)描述 獲得用戶的允許權(quán)限
1.Privacy - Location When In Use Usage Description -> 是否允許此App在使用期間訪問(wèn)你的位置?
2.Privacy - Location Always Usage Description -> 是否允許此App永久訪問(wèn)你的位置?
第三步.開(kāi)始寫代碼了
頭文件和代理
#import <CoreLocation/CoreLocation.h>
@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic,strong ) CLLocationManager *locationManager;//定位服務(wù)
@property (nonatomic,copy) NSString *currentCity;//城市
@property (nonatomic,copy) NSString *strLatitude;//經(jīng)度
@property (nonatomic,copy) NSString *strLongitude;//維度
- (void)viewDidLoad {
[super viewDidLoad];
[self locatemap];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)locatemap{
if ([CLLocationManager locationServicesEnabled]) {
_locationManager = [[CLLocationManager alloc]init];
_locationManager.delegate = self;
[_locationManager requestAlwaysAuthorization];
_currentCity = [[NSString alloc]init];
[_locationManager requestWhenInUseAuthorization];
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager.distanceFilter = 5.0;
[_locationManager startUpdatingLocation];
}
}
#pragma mark - 定位失敗
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
UIAlertController *alert = [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) {
NSURL *settingURL = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication]openURL:settingURL];
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}];
[alert addAction:cancel];
[alert addAction:ok];
[self presentViewController:alert animated:YES completion:nil];
}
#pragma mark - 定位成功
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
[_locationManager stopUpdatingLocation];
CLLocation *currentLocation = [locations lastObject];
CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
//當(dāng)前的經(jīng)緯度
NSLog(@"當(dāng)前的經(jīng)緯度 %f,%f",currentLocation.coordinate.latitude,currentLocation.coordinate.longitude);
//這里的代碼是為了判斷didUpdateLocations調(diào)用了幾次 有可能會(huì)出現(xiàn)多次調(diào)用 為了避免不必要的麻煩 在這里加個(gè)if判斷 如果大于1.0就return
NSTimeInterval locationAge = -[currentLocation.timestamp timeIntervalSinceNow];
if (locationAge > 1.0){//如果調(diào)用已經(jīng)一次聋伦,不再執(zhí)行
return;
}
//地理反編碼 可以根據(jù)坐標(biāo)(經(jīng)緯度)確定位置信息(街道 門牌等)
[geoCoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (placemarks.count >0) {
CLPlacemark *placeMark = placemarks[0];
_currentCity = placeMark.locality;
if (!_currentCity) {
_currentCity = @"無(wú)法定位當(dāng)前城市";
}
//看需求定義一個(gè)全局變量來(lái)接收賦值
NSLog(@"當(dāng)前國(guó)家 - %@",placeMark.country);//當(dāng)前國(guó)家
NSLog(@"當(dāng)前城市 - %@",_currentCity);//當(dāng)前城市
NSLog(@"當(dāng)前位置 - %@",placeMark.subLocality);//當(dāng)前位置
NSLog(@"當(dāng)前街道 - %@",placeMark.thoroughfare);//當(dāng)前街道
NSLog(@"具體地址 - %@",placeMark.name);//具體地址
NSString *message = [NSString stringWithFormat:@"%@,%@,%@,%@,%@",placeMark.country,_currentCity,placeMark.subLocality,placeMark.thoroughfare,placeMark.name];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:message delegate:self cancelButtonTitle:nil otherButtonTitles:@"好", nil];
[alert show];
}else if (error == nil && placemarks.count){
NSLog(@"NO location and error return");
}else if (error){
NSLog(@"loction error:%@",error);
}
}];
}
希望這些工作中的節(jié)點(diǎn)總結(jié)能幫助到大家 謝謝