#import <CoreLocation/CoreLocation.h> 使用到的頭檔案 要引入CoreLocation這個包
<CLLocationManagerDelegate> 使用的代理名稱
//1.使用定位服務
//設定app有通路定位服務的權限
//在使用應用期間 / 始終(app在背景)
//info.plist檔案添加以下兩條(或者其中一條):
//NSLocationWhenInUseUsageDescription 在使用應用期間
//NSLocationAlwaysUsageDescription 始終
//2.LocationManager 對象管理相關的定位服務
_manager = [[CLLocationManager alloc] init];
//manager判斷: 手機是否開啟定位 / app是否有通路定位的權限
//[CLLocationManager locationServicesEnabled]; //手機是否開啟定位
//[CLLocationManager authorizationStatus]; //app通路定位的權限的狀态
if (![CLLocationManager locationServicesEnabled] || [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse) {
[_manager requestWhenInUseAuthorization]; //向使用者請求通路定位服務的權限
}
_manager.delegate = self;
_manager.desiredAccuracy = kCLLocationAccuracyBest;
_manager.distanceFilter = 1.0f;
[_manager startUpdatingLocation];
//定位代理經緯度回調
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[_manager stopUpdatingLocation];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
NSDictionary *test = [placemark addressDictionary];
// Country(國家) State(城市) SubLocality(區) Name全稱
NSLog(@"%@", [test objectForKey:@"Name"]);
}
}];
}