如何从IOS中的用户获取当前位置?

如何从IOS中的用户获取当前位置?

如何从IOS中的用户获得当前位置?



ITMISS
浏览 745回答 3
3回答

PIPIONE

在IOS 6中,- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation不受欢迎。使用以下代码代替- (void)locationManager:(CLLocationManager *)manager      didUpdateLocations:(NSArray *)locations {     CLLocation *location = [locations lastObject];     NSLog(@"lat%f - lon%f", location.coordinate.latitude, location.coordinate.longitude);}对于IOS 6~8,上述方法仍然是必要的,但您必须处理授权。_locationManager = [CLLocationManager new];_locationManager.delegate = self;_locationManager.distanceFilter = kCLDistanceFilterNone;_locationManager.desiredAccuracy = kCLLocationAccuracyBest;if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0 &&     [CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedWhenInUse    //[CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways    ) {      // Will open an confirm dialog to get user's approval      [_locationManager requestWhenInUseAuthorization];      //[_locationManager requestAlwaysAuthorization];} else {     [_locationManager startUpdatingLocation]; //Will update location immediately }这是处理用户授权的委托方法#pragma mark - CLLocationManagerDelegate- (void)locationManager:(CLLocationManager*)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{     switch (status) {     case kCLAuthorizationStatusNotDetermined: {         NSLog(@"User still thinking..");     } break;     case kCLAuthorizationStatusDenied: {         NSLog(@"User hates you");     } break;     case kCLAuthorizationStatusAuthorizedWhenInUse:     case kCLAuthorizationStatusAuthorizedAlways: {         [_locationManager startUpdatingLocation]; //Will update location immediately     } break;     default:         break;     }}
打开App,查看更多内容
随时随地看视频慕课网APP