【问题标题】:IOS CLLocationManager is returning latitude: 0.000000 longitude: 0.000000IOS CLLocationManager 正在返回纬度:0.000000 经度:0.000000
【发布时间】:2015-06-08 10:28:30
【问题描述】:

我一直在尝试获取当前位置 IOS8 并关注线程 Location Services not working in iOS 8,但获取的是 00.00,00.00 而不是当前位置。关于我哪里做错的任何建议。

ViewController.h

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

 #define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

 @interface ViewController : UIViewController <CLLocationManagerDelegate>

 @property (strong, nonatomic) CLLocationManager *locationManager;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

 - (void)viewDidLoad {

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.distanceFilter = kCLDistanceFilterNone;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
NSLog(@"%@", [self deviceLocation]);


CLLocation *location = [self.locationManager location];

// Configure the new event with information from the location
CLLocationCoordinate2D coordinate = [location coordinate];

NSString *latitude = [NSString stringWithFormat:@"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", coordinate.longitude];


NSLog(@"%@",latitude);
NSLog(@"%@",longitude);

[super viewDidLoad];

}

 - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.

}

 -(void)setUpMap
 {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
#ifdef __IPHONE_8_0
if(IS_OS_8_OR_LATER) {
    // Use one or the other, not both. Depending on what you put in info.plist
    [self.locationManager requestAlwaysAuthorization];
}
#endif
[self.locationManager startUpdatingLocation];

 }



 -(NSString *)deviceLocation 
 {
   return [NSString stringWithFormat:@"latitude: %f longitude: %f",         self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
 }
  - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
 {
   [manager stopUpdatingLocation];

CLLocationCoordinate2D coordinate_currentlocation = [newLocation coordinate];

float latitude_current = newLocation.coordinate.latitude;
float longitude_current = newLocation.coordinate.longitude;


NSLog(@"%f",latitude_current);
NSLog(@"%f",longitude_current);

NSLog(@"Current latitude :%f",coordinate_currentlocation.latitude);
NSLog(@"Current longitude :%f",coordinate_currentlocation.longitude);
} 
@end

我还在 info.plist 中添加了以下键

<key>NSLocationAlwaysUsageDescription</key>
<string>Your location is needed for this app.</string>

【问题讨论】:

    标签: ios objective-c ios8 location


    【解决方案1】:

    在 InfoPlist.strings 文件中添加此字符串

    1) NSLocationWhenInUseUsageDescription
    
    2) NSLocationAlwaysUsageDescription
    

    试试这个代码

    locationManagerApp=[[CLLocationManager alloc] init];
    
        locationManagerApp.delegate = self;
        locationManagerApp.distanceFilter = kCLDistanceFilterNone;
        locationManagerApp.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    
        if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        {        
               [locationManagerApp requestAlwaysAuthorization];
        }
    
        [locationManagerApp startUpdatingLocation];
        CLLocation *location1 = [locationManagerApp location];
        CLLocationCoordinate2D coordinate = [location1 coordinate];
        self.latValue= [NSString stringWithFormat:@"%f", coordinate.latitude];
        self.longValue = [NSString stringWithFormat:@"%f", coordinate.longitude];
        NSLog(@"Latitude  = %@", self.latValue);
        NSLog(@"Longitude = %@", self.longValue);
    

    然后在设备中运行您的项目。如果您在模拟器中运行项目,则不会得到 lat/long。

    获取模拟器上的当前位置(选择任意一个位置)。

    【讨论】:

    • 忘了提到我正在设备中获取坐标,但是一旦我在模拟器上获取坐标,我想测试它的其他工作流程。
    【解决方案2】:

    代码有错误,实际检查授权的函数没有调用。我将所有代码移至 viewDidLoad,如下所示,它起作用了。感谢您对此进行调查。

    - (void)viewDidLoad {
    
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    
    #ifdef __IPHONE_8_0
            if(IS_OS_8_OR_LATER)
            {
                [self.locationManager requestAlwaysAuthorization];
            }
    #endif
                [self.locationManager startUpdatingLocation];
                [self.locationManager startUpdatingLocation];
    
                NSLog(@"%@", [self deviceLocation]);
    
    [super viewDidLoad];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-30
      • 2011-03-11
      相关资源
      最近更新 更多