CLLocation Manager startUpdatingLocation未启动

时间:2014-07-10 20:13:52

标签: ios objective-c ios7 core-location

我一直在关注本教程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开始更新当前位置并以字符串的形式返回坐标值,以便稍后在我的应用中使用?感谢。

2 个答案:

答案 0 :(得分:1)

使您的类成为CLLocationManagerDelegate的委托。

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

然后将委托设置为自己。

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

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

dispatch_sync(dispatch_get_main_queue(), ^{ 

});

答案 1 :(得分:0)

如果你正在使用IOS 8,你应该做一些事情,以使CLlocationManager正常工作。 你有一个NSLocationAlwaysUsageDescriptionadded添加到您的应用程序info.plist文件?你有两个在模拟器中设置位置? (调试 - &GT;位置)。第三,你必须要求额外的授权才能在ios8中使用核心位置。

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