1

只是试图确定用户的位置。这是代码:

self.locationManager = [[CLLocationManager alloc] init];
//self.locationManager.delegate = self;
self.locationManager.distanceFilter = 80.0;
[self.locationManager startUpdatingLocation];

self.geoCoder = [[CLGeocoder alloc] init];
[self.geoCoder reverseGeocodeLocation: self.locationManager.location completionHandler:

 ^(NSArray *placemarks, NSError *error) {

     CLPlacemark *placemark = [placemarks objectAtIndex:0];

     UIAlertView *locationAlert = [[UIAlertView alloc] initWithTitle:@"Location" message:[NSString stringWithFormat:@"The app has determined that your country is: %@ (%@)", placemark.country, placemark.ISOcountryCode] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
     [alert show];


 }];

第一次运行该应用程序时,我会弹出双重警报。locationAlert 首先弹出,通知用户一个(空)国家,然后几乎同时弹出默认弹出窗口,表明该应用程序想使用我的位置。如果我单击“是”,下次我运行该应用程序时,我将获得用户正确国家/地区的单一弹出窗口。

所以我的问题是,我怎样才能首先获得访问用户位置的权限,然后运行此代码来查找他们的位置,而不会因为我在未经许可的情况下查询他们的位置而得到空响应?

4

1 回答 1

2

反向地理定位代码尝试使用location来自 的属性,但在调用委托方法locationManager之前它是无效的。 CLLocationManagerlocationManager:didUpdateToLocation:fromLocation:

您应该有一个像这样的委托方法来处理位置更新。

于 2013-05-23T23:59:38.140 回答