-2

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

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

4

2 回答 2

2

首先符合CLLocationManagerDelegate.h文件,如下所示

@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...");
}
于 2013-07-22T06:17:11.923 回答
0

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

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

把这部分代码放在你的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];

让我知道,.????

于 2013-07-22T06:48:31.757 回答