CLLocationManager requestLocation不调用didUpdateLocations

时间:2016-03-24 07:55:21

标签: ios objective-c iphone cocoa-touch core-location

我正在尝试使用[self.locationManager requestLocation]获取当前用户位置但由于某种原因,未调用委托方法locationManager: didUpdateLocations:

我正在从requestLocationUIButton)处理程序中调用IBOutlet

知道为什么吗?

谢谢!

2 个答案:

答案 0 :(得分:0)

右键单击info.plist打开为 - &gt;源代码将以下代码添加到<dict>...</dict> <key>NSLocationAlwaysUsageDescription</key> <string>App would like to use your location.</string> <key>NSLocationUsageDescription</key> <string>I need Location</string> <key>NSLocationWhenInUseUsageDescription</key> <string>App would like to use your location.</string> <key>UIMainStoryboardFile</key> 导入

#import <CoreLocation/CoreLocation.h>
#import <MobileCoreServices/MobileCoreServices.h>

然后进入viewcontroller.h添加CLLocationManagerDelegate 在viewcontroller.m文件中添加此方法。

-(void)getCurrentLocation{
self.locationManager = [[CLLocationManager alloc] init];
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager requestAlwaysAuthorization];
}
self.locationManager.delegate = self;
self.locationManager.distanceFilter = 10;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
}

然后调用CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manage
didUpdateLocations:(NSArray *)locations
{
CLLocation *currentAction = [locations objectAtIndex:0];
CLLocation *crnLoc = [locations lastObject];

[self.locationManager stopUpdatingLocation];
CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;

CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
    // If the event is recent, do something with it.
    NSLog(@"latitude~~~: %+.8f, longitude~~~: %+.8f\n",
          location.coordinate.latitude,
          location.coordinate.longitude);
    self.latitudeLabel.text=[NSString stringWithFormat:@"%.8f", 
    crnLoc.coordinate.latitude];
    self.longitudeLabel.text=[NSString stringWithFormat:@"%.8f", 
      crnLoc.coordinate.longitude];
}
self.latitudeLabel.text=[NSString stringWithFormat:@"%.8f",
crnLoc.coordinate.latitude];
self.longitudeLabel.text=[NSString stringWithFormat:@"%.8f",
crnLoc.coordinate.longitude];


[geocoder reverseGeocodeLocation:currentAction
completionHandler:^(NSArray *placemarks, NSError *error)
 {
     if (!(error))
     {
         CLPlacemark *placemark = [placemarks objectAtIndex:0];

         NSString *locatedAt = [[placemark.addressDictionary
    valueForKey:@"FormattedAddressLines"]
    componentsJoinedByString:@", "];
    NSString *Address = [[NSString alloc]initWithString:locatedAt];
         NSLog(@"Adress %@",Address);
     }
     else
     {
         NSLog(@"\n Current Location Not Detected \n");
         return;
     }
 }];
 }

希望此代码可以帮助您。

答案 1 :(得分:0)

我认为您的问题与this answer有关。

如果要将CLLocationManager实例创建为函数级实例,则在函数完成后(可能在调用委托之前)的某个时刻将对其进行处理。如果您将其创建为类级实例,则将调用该委托,因为CLLocationManager实例仍将存在。