【问题标题】:CLLocationManager requestLocation isn't calling didUpdateLocationsCLLocationManager requestLocation 没有调用 didUpdateLocations
【发布时间】:2016-07-11 18:19:27
【问题描述】:

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

我从UIButton (IBOutlet) 处理程序调用requestLocation

知道为什么吗?

谢谢!

【问题讨论】:

  • 您是否在Info.plist 文件中设置了NSLocationWhenInUseUsageDescriptionNSLocationAlwaysUsageDescription
  • NSLocationAlwaysUsageDescription
  • 尝试设置另一个。
  • 您是否在 iOS 设置中启用了应用接收位置信息? '允许此应用接收位置信息..'等
  • 您在当前班级中添加了locationManager.delegate = self;

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


【解决方案1】:

右键info.plist open as --> 源代码将以下代码添加到&lt;dict&gt;...&lt;/dict&gt; <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;
     }
 }];
 }

希望此代码对您有所帮助。

【讨论】:

    【解决方案2】:

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

    如果您将 CLLocationManager 实例创建为函数级实例,它将在函数完成后的某个时间点被释放(可能在调用委托之前)。如果您将其创建为类级实例,则会调用委托,因为 CLLocationManager 实例仍然存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      • 1970-01-01
      • 2014-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多