標籤:
if ([UIDevice currentDevice].systemVersion.doubleValue > 8.0) {
[self.locMgr requestAlwaysAuthorization];
} else {
[self.locMgr startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status == kCLAuthorizationStatusNotDetermined) {
NSLog(@"等待使用者授權");
}
if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways) {
// 得到使用者授權開始定位
[self.locMgr startUpdatingLocation];
} else {
NSLog(@"授權失敗");
}
}
/**
* 當定位到使用者的位置時,就會調用(調用的頻率比較頻繁)
*/
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
// locations 數組裡存放的是 CLLocation 對象, 一個 CLLocation 對象就代表著一個位置
CLLocation *loc = [locations firstObject];
// 緯度 : loc.coordinate.latitude
// 經度 : loc.coordinate.longitude
NSLog(@"緯度:%f, 經度:%f",loc.coordinate.latitude,loc.coordinate.longitude);
CLLocation *c = [[CLLocation alloc] initWithLatitude:loc.coordinate.latitude longitude:loc.coordinate.longitude];
//建立位置
CLGeocoder *revGeo = [[CLGeocoder alloc] init];
[revGeo reverseGeocodeLocation:c completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
if (!error && [placemarks count] > 0) {
NSDictionary *dict = [[placemarks objectAtIndex:0] addressDictionary];
NSLog(@"%@",[dict objectForKey:@"Country"]);
NSLog(@"%@",[dict objectForKey:@"State"]);
NSLog(@"%@",[dict objectForKey:@"SubLocality"]);
NSLog(@"%@",[dict objectForKey:@"Street"]);// Country(國家) State(城市) SubLocality(區) Street(詳細)
} else {
NSLog(@"ERROR: %@", error); }
}];
[self.locMgr stopUpdatingLocation];
}
iOS開發-定位服務iOS8.0以上授權