首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在IOS中使用CLLocation获取用户当前位置

在IOS中使用CLLocation获取用户当前位置
EN

Stack Overflow用户
提问于 2015-12-03 16:08:05
回答 5查看 16K关注 0票数 1

我想要获取用户的当前位置&地址,这是我使用CLLocationManager并添加了核心位置框架所做的。但回应是什么也没有。请分享您的评论。

代码语言:javascript
复制
locationManager = [[CLLocationManager alloc] init]; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
locationManager.delegate = self; [locationManager startUpdatingLocation]; 

代表们是

代码语言:javascript
复制
(void)locationManager:(CLLocationManager *)manager didFailWithError: (NSError *)error { 
    NSLog(@"didFailWithError: %@", error); 
    UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
    [errorAlert show]; 
}

(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { 
    CLLocation *currentLocation = locations.first; 
    [locationManager setDelegate:nil]; 
    if (currentLocation != nil) { 
        NSString *longitudeLabel = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; 
        NSString *latitudeLabel = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2015-12-03 17:20:27

检查我的代码,您可以从当前位置获取地址

代码语言:javascript
复制
- (void)viewDidLoad {
CLGeocoder *ceo;

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
[locationManager startUpdatingLocation];
}



-(void)viewDidAppear:(BOOL)animated{
ceo= [[CLGeocoder alloc]init];
[self.locationManager requestWhenInUseAuthorization];
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}
    CLLocationCoordinate2D coordinate;

coordinate.latitude=locationManager.location.coordinate.latitude;
coordinate.longitude=locationManager.location.coordinate.longitude;
//CLLocationCoordinate2D  ctrpoint;
//  ctrpoint.latitude = ;
//ctrpoint.longitude =f1;
//coordinate.latitude=23.6999;
//coordinate.longitude=75.000;
MKPointAnnotation *marker = [MKPointAnnotation new];
marker.coordinate = coordinate;
NSLog(@"%f",coordinate.latitude);
//[self.mapView addAnnotation:marker];


CLLocation *loc = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude
                   ];
[ceo reverseGeocodeLocation:loc
          completionHandler:^(NSArray *placemarks, NSError *error) {
              CLPlacemark *placemark = [placemarks objectAtIndex:0];
              NSLog(@"placemark %@",placemark);
              //String to hold address
              NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
              NSLog(@"addressDictionary %@", placemark.addressDictionary);

              NSLog(@"placemark %@",placemark.region);
              NSLog(@"placemark %@",placemark.country);  // Give Country Name
              NSLog(@"placemark %@",placemark.locality); // Extract the city name
              NSLog(@"location %@",placemark.name);
              NSLog(@"location %@",placemark.ocean);
              NSLog(@"location %@",placemark.postalCode);
              NSLog(@"location %@",placemark.subLocality);

              NSLog(@"location %@",placemark.location);
              //Print the location to console
              NSLog(@"I am currently at %@",locatedAt);


              _City.text=[placemark.addressDictionary objectForKey:@"City"];
              [locationManager stopUpdatingLocation];
          }

 ];
}





#pragma mark - CLLocationManagerDelegate

请添加这两种方法

代码语言:javascript
复制
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

希望这能行得通

谢谢

票数 3
EN

Stack Overflow用户

发布于 2015-12-03 16:21:11

您需要添加

"NSLocationAlwaysUsageDescription"

"NSLocationWhenInUseUsageDescription"键输入

包含要显示的消息的Info.plist

票数 3
EN

Stack Overflow用户

发布于 2015-12-03 16:16:30

在调用startUpdatingLocation之前,需要先调用请求权限的函数。

代码语言:javascript
复制
locationManager = [[CLLocationManager alloc] init]; 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
locationManager.delegate = self;
[locationManager requestWhenInUseAuthorization]; 
[locationManager startUpdatingLocation];
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34060722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档