使用Objective-C中的CoreLocation无法获得经度和纬度

时间:2013-07-22 06:13:35

标签: ios gps core-location

我想获取用户的坐标,一旦用户启动应用程序,它应该初始化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开发。

2 个答案:

答案 0 :(得分:2)

首先在 .h 文件中符合CLLocationManagerDelegate

@interface CurrentLocationWithGPS : NSObject<CLLocationManagerDelegate>

您需要在此方法中设置CLLocation的委托。

- (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...");
}

答案 1 :(得分:0)

您是否在输出中获得当前位置。或者你只是试图找回坐标?..

无论如何,它仍然令人困惑

将此部分代码放在getLocations方法

  

我希望你根本不检查位置服务。您   应该为你的应用程序启用LocationServices你总是得到   null值检查条件:

 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];

让我知道,。????