【问题标题】:CLLocation Manager startUpdatingLocation not startingCLLocation Manager startUpdatingLocation 未启动
【发布时间】:2014-09-01 08:29:42
【问题描述】:

我一直在关注本教程 http://www.devfright.com/ios-6-core-location-tutorial/ 将定位服务添加到我的 ios 应用程序。我已经在我的 TestFileViewController.h 文件中导入了 Corelocation,如下所示:

 #import <UIKit/UIKit.h>
 #import <FacebookSDK/FacebookSDK.h>
 #import <CoreLocation/CoreLocation.h>

 @interface TestFileViewController : UIViewController <UITableViewDelegate,UITableViewDataSource, NSStreamDelegate>

 @property (nonatomic, strong) CLLocationManager *locationManager;

 @end

然后在我添加的testFileViewController.m ViewDidLoad中(记得实现CLLocationManagerDelegate):

//start checking for location from CoreLocation Framework (currently not working)
if ([CLLocationManager locationServicesEnabled])
{
    NSLog(@"location services enabled");
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    _locationManager.delegate = self;
    [_locationManager startUpdatingLocation];
};

}

(条件 CLLocationManager 执行 true 和 NSLogs)然后在 ViewDidLoad 正下方:

    //fail to find location, handle error
    - (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];
      }

      //show found location, return NSLOG for proof
      - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:            (CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
      {
      NSLog(@"Made it to location Manager");
      CLLocation *location = [locationManager location];

      CLLocationCoordinate2D coordinate = [location coordinate];

      NSLog(@"%f",coordinate.latitude);
      }

我添加了 NSLogs 作为标记,以从控制台确定函数是否被调用,但是 didFailWithError 和 didUpdateToLocation 都没有被调用。

是的,我知道 didUpdateToLocation 已弃用/过时,我将其替换为 didUpdateLocations,但它仍然无法正常工作,因此我将其改回。

如何让我的 locationManager 开始更新当前位置并以字符串形式返回坐标值,以便稍后在我的应用程序中使用?谢谢。

【问题讨论】:

    标签: ios objective-c ios7 core-location


    【解决方案1】:

    如果您使用的是 IOS 8,您应该做一些事情,以使 CLlocationManager 正常工作。 您是否已将 NSLocationAlwaysUsageDescription 添加到您的应用程序 info.plist 文件中?二 你在模拟器中设置了位置吗? (调试-> 位置)。第三个,您必须请求额外授权才能在 ios8 中使用核心位置。

    if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
        [locationManager requestAlwaysAuthorization];
    

    【讨论】:

      【解决方案2】:

      让您的班级成为 CLLocationManagerDelegate 的代表。

      @interface TestFileViewController : UIViewController <UITableViewDelegate,UITableViewDataSource, NSStreamDelegate, **CLLocationManagerDelegate**>
      

      然后将delegate设置为self。

      我还建议创建一个单独的类来处理位置。更简洁的设计。

      还要确保您在主线程上调用 startUpdatingLocation。

      dispatch_sync(dispatch_get_main_queue(), ^{ 
      
      });
      

      【讨论】:

      • 我已经添加了您的建议,但似乎并没有改变正在发生的事情。
      • 抱歉,澄清一下,我将我的类设为委托并将 locationManager 设置为从 testFileViewController.h 类调用,我还将 locationManager 删除到视图下的一个方法确实加载并从选择器。它仍然返回它已启用位置服务,但它实际上并没有开始更新位置。
      • 您是否在 [_locationManager startUpdatingLocation]; 处设置了断点?它会被调用吗?当它出现时,您应该会看到苹果提示。你看见了吗?如果不是,您是否可能较早看到它并拒绝了许可?如果您转到设置和隐私,您是否看到您的应用名称和权限设置为“是”?帮助在聊天窗口上帮助您。 lmk
      • 我在调用之前在条件中添加了一个 NSLog,所以它被调用了。我没有看到苹果提示,并且我在模拟器中工作并启用了位置,所以我不确定如何更改隐私设置。
      • 哦..在模拟器菜单中,转到调试->位置->选择一个位置。试一试。对于隐私,您可以转到设置->隐私->位置->您应该看到您的应用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多