在 Objective-C 中无法使用 CoreLocation 获取纬度和经度

Posted

技术标签:

【中文标题】在 Objective-C 中无法使用 CoreLocation 获取纬度和经度【英文标题】:Cannot get latitude and longtidute with CoreLocation in Objective-C 【发布时间】:2013-07-22 06:13:35 【问题描述】:

我想获取用户的坐标,一旦用户启动应用程序,它应该初始化 GPS 坐标。我已按照本教程进行操作: http://www.iosdevnotes.com/2011/10/ios-corelocation-tutorial/

我创建了一个名为 CurrentLocationWithGPS 的类

这是标题:

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

@interface CurrentLocationWithGPS : NSObject<CLLocationManagerDelegate>

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error;

- (void) getLocations;

- (Float32) latitude;


@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *currentLocation;

@end

这是实现:

#import "CurrentLocationWithGPS.h"


@implementation CurrentLocationWithGPS

@synthesize locationManager, currentLocation;

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
    self.currentLocation = newLocation;

    if(newLocation.horizontalAccuracy <= 100.0f)  [locationManager stopUpdatingLocation]; 


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error 
    if(error.code == kCLErrorDenied) 
        [locationManager stopUpdatingLocation];
     else if(error.code == kCLErrorLocationUnknown) 
        // retry
     else 
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error retrieving location"
                                                        message:[error description]
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    



- (void) getLocations 
    NSLog(@"GPS Location is initialising...");

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    [locationManager startUpdatingLocation];
    NSLog(@"GPS Location is initialised...");



- (Float32) latitude 

    return currentLocation.coordinate.latitude;


- (Float32) longitude 

    return currentLocation.coordinate.longitude;


@end

我在一个单独的线程中调用 getLocations 函数,这样它就不会阻塞其他任何东西。这是我从另一个类中调用它的方式:

- (void)viewDidLoad

    [super viewDidLoad];

    NSThread *myThread =[[NSThread alloc]initWithTarget:self selector:@selector(locationSet) object:nil];
    [myThread start];



- (void) locationSet 

    CurrentLocationWithGPS *locationFind = [[CurrentLocationWithGPS alloc] init];
    locationFind.getLocations;
    NSLog(@"latitude is %f", locationFind.latitude);

现在问题是 latitudelongitdute 函数都返回 0.000,我在这里缺少什么?我正在为 iOS6.1 开发。

【问题讨论】:

【参考方案1】:

首先在像这样的 .h 文件中符合CLLocationManagerDelegate

@interface CurrentLocationWithGPS : NSObject<CLLocationManagerDelegate>

你需要在这个方法中设置CLLocation的Delegate。

- (void) getLocations
 
    NSLog(@"GPS Location is initialising...");

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
    NSLog(@"GPS Location is initialised...");

【讨论】:

@SarpKaya 现在你遇到了什么问题......? 同样的问题。我将纬度和经度设为 0.0000 查看我编辑的答案以及为什么要在线程 n 上调用 locationSet 所有这些您可以在 viewDidLoad 方法中执行的操作。无需复杂化... 感谢您的回复,我想我错过了两行 locationManager.desiredAccuracy = kCLLocationAccuracyBest;和 locationManager.distanceFilter = kCLDistanceFilterNone;【参考方案2】:

您是否在输出中获得当前位置。或者你只是想检索坐标?..

总之还是很迷茫

把这部分代码放在你的getLocations方法上

我希望您根本没有检查定位服务。你 应该为您的应用启用 LocationServices 否则您总是会得到 空值检查条件:

 if ([CLLocationManager locationServicesEnabled])
        

例如:

       CLLocationManager *locationManager = [[CLLocationManager alloc] init];
        if ([CLLocationManager locationServicesEnabled])
        
            locationManager.delegate = self;
            locationManager.desiredAccuracy = kCLLocationAccuracyBest;
            locationManager.distanceFilter = kCLDistanceFilterNone;
            [locationManager startUpdatingLocation];
        

        location = [locationManager location];
        CLLocationCoordinate2D coordinate = [location coordinate];;
 NSString *str=[[NSString alloc] initWithFormat:@" latitude:%f longitude:%f",coordinate.latitude,coordinate.longitude];

让我知道,.????

【讨论】:

以上是关于在 Objective-C 中无法使用 CoreLocation 获取纬度和经度的主要内容,如果未能解决你的问题,请参考以下文章

专业数字绘画 Corel Painter 2018 18.1.0.651 MacOSX

Coreldraw X3出现的corel A.R.M怎么解决?

Objective-C 框架中的 Swift 类别在运行时无法识别

无法在快速单元测试中使用 Objective-C 类

在 Objective-C 中无法使用 CoreLocation 获取纬度和经度

无法在 Swift 中扩展 Objective-C 类