iOS CoreLocation没有获得GPS坐标

时间:2013-03-28 21:00:51

标签: ios objective-c core-location

所以我是iOS开发的新手,我只是想用我当前的GPS坐标更新标签。我没有编译问题,但我的坐标是0.00000, 0.00000

以下是我的.h文件的代码:

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


@interface ViewController : UIViewController{
    IBOutlet CLLocationManager *locationManager;
    IBOutlet UILabel *location;
}

//@property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
//@property (weak, nonatomic) IBOutlet UILabel *location;
@end

以下是我的.m文件的代码:

@implementation ViewController

- (void) updateLabel
{
    NSObject *latitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];
    NSObject *longitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude];

    location.text = [NSString stringWithFormat: @"%@,%@", latitude, longitude];
}

- (void)viewDidLoad
{

    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [self updateLabel];
    [super viewDidLoad];
}

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

@end

4 个答案:

答案 0 :(得分:1)

修正了它:

我没有实现任何委托方法,我没有实现[locationManager startUpdatingLocation]。现在我知道的更好。

.h文件:

@interface MapViewController : UIViewController <CLLocationManagerDelegate>
@property (nonatomic, retain) IBOutlet CLLocationManager *locationManager;
@property (strong, nonatomic) IBOutlet UILabel *location;
@end

.m文件:

- (void) updateCurrentLabel
{
    NSObject *latitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.latitude];
    NSObject *longitude = [NSString stringWithFormat:@"%f", locationManager.location.coordinate.longitude];

    self.location.text = [NSString stringWithFormat: @"Current Location: %@,%@", latitude, longitude];
}

- (void)viewDidLoad
{
    [self getCurrentLocation];
    [super viewDidLoad];
}
-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    [self updateCurrentLabel];
}

-(void) getCurrentLocation
{
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    [locationManager startUpdatingLocation];
}

感谢你指出我是多么高兴。弄清楚了。谢谢你们!

答案 1 :(得分:1)

一旦尝试这样,在ViewDidLoad:

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = (id)self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    [locationManager startUpdatingLocation];
    [self updateLabel];

使用此委托方法,否则您将获得0值:

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
   //No need to do any code...
   // NSLog(@"Got location %f,%f", newLocation.coordinate.latitude,   newLocation.coordinate.longitude);

 }

更新标签方法:

- (void) updateLabel
{
   //Getting Current Latitude and longitude..

    CLLocation *location = [locationManager location];
    float longitude=location.coordinate.longitude;
    float latitude=location.coordinate.latitude;
    NSLog(@"latitude,longitudes are >> %f,%f",latitude,longitude);
    locationlabel.text =  [NSString stringWithFormat:@"%f,%f",longitude,latitude];


  } 

答案 2 :(得分:0)

不要使用locationManager.location.coordinate.latitude,而是保留CLLocationCoordinate2D类型的实例变量。您可以将其称为currentLocation。然后,当您在委托方法locationManager:didUpdateLocations:中获取值时,请设置currentLocation的值。

您必须调用[locationManager startUpdatingLocation]并设置其委托(以及实现该委托方法)。

您目前使用位置管理器的方式是错误的,我认为您最好按照教程来完成基础知识。

答案 3 :(得分:0)

我发现将位置用作单例非常有趣,并将值保存到默认用户。我是年轻的程序员,我正在尝试用oop编写所有代码。我使用如下(此代码仍需要重构,alertUserWithTitle:是NYMessageToUser的类方法,以提醒用户):

//##Header file:
@interface NYLocationManager : NSObject<CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
    float lonngitude;
    float latitude;
    float altitude;
}
@property(nonatomic,retain)CLLocationManager *locationManager;
@property(nonatomic,readwrite)float longitude;
@property(nonatomic,readwrite)float latitude;
@property(nonatomic,readwrite)float altitude;

+(NYLocationManager *) getInstance;

-(void)startUpdatingLocation;
-(void)stopUpdatingLocation;
-(double)getDistanceFromUserLocationToCordinatesLatitude:(float)lat Longitude:(float)lon;

@end




//### implementation file: 

@implementation NYLocationManager

@synthesize locationManager;
@synthesize latitude;
@synthesize longitude;
@synthesize altitude;

+ (id)getInstance
{
    static NYLocationManager *Instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Instance = [[self alloc] init];
    });
    [Instance startUpdatingLocation];
    return Instance;
}

- (id)init
{
    if (self = [super init])
    {

        latitude    =0.0;
        longitude   =0.0;
        altitude    =0.0;
        if([[NSUserDefaults standardUserDefaults] objectForKey:@"locationLongitude"] != nil)
        {
            NSUserDefaults *savedLocation=[NSUserDefaults standardUserDefaults];
            latitude    =[[savedLocation objectForKey:@"locationLatitude"] floatValue];
            longitude   =[[savedLocation objectForKey:@"locationLongitude"] floatValue];
            altitude    =[[savedLocation objectForKey:@"locationAltitude"] floatValue];
        }
        locationManager = [[CLLocationManager alloc] init];
        locationManager.distanceFilter  = kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
        locationManager.delegate        = self;
        if ([CLLocationManager locationServicesEnabled])
        {
            [locationManager startUpdatingLocation];
        } else
        {
            [NYMessageToUser  alertUserWithTitle:@"Location Services is Disabled!!!" withMessage:@"This app is designed to share images with location, Please enable location for this app and relucnh the app"];
        }
    }
    return self;
}

- (void)dealloc
{
    // Should never be called, but just here for clarity really.
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *loc =[locations lastObject];
    self.longitude  =loc.coordinate.longitude;
    self.latitude   =loc.coordinate.latitude;
    self.altitude   =loc.altitude;
    NSUserDefaults *savedLocation=[NSUserDefaults standardUserDefaults];
    [savedLocation setObject: [NSString stringWithFormat:@"%f", self.longitude] forKey:@"locationLongitude"];
    [savedLocation setObject: [NSString stringWithFormat:@"%f", self.latitude] forKey:@"locationLatitude"];
    [savedLocation setObject: [NSString stringWithFormat:@"%f", self.altitude ] forKey:@"locationAltitude"];
    [savedLocation synchronize];

    [locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    [locationManager stopUpdatingLocation];
    [NYMessageToUser alertUserWithTitle:@"Location Error!!!" withMessage:@"This app is designed to use with valid location, Please enable location for this app and relucnh the app"];
}
-(void)startUpdatingLocation
{
    if ([CLLocationManager locationServicesEnabled])
    {
        [locationManager startUpdatingLocation];
    } else
    {
        [NYMessageToUser alertUserWithTitle:@"Location Services is Disabled!!!" withMessage:@"This app is designed to share images with location, Please enable location for this app and relucnh the app"];
    }
}

-(void)stopUpdatingLocation
{
    [locationManager stopUpdatingLocation];
}
-(double)getDistanceFromUserLocationToCordinatesLatitude:(float)lat Longitude:(float)lon
{
    CLLocation *locA = [[CLLocation alloc] initWithLatitude:self.latitude longitude:self.longitude];

    CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat longitude:lon];

    CLLocationDistance distance = [locA distanceFromLocation:locB];
    return distance;
}
@end 




//### How to use



NYLocationManager *loc  =[NYLocationManager getInstance];
 NSLog(@"longitude: %f, latitude: %f, altitude: %f",loc.longitude,loc.latitude,loc.altitude);