2013-03-28 63 views
0

所以我是iOS开发新手,我只是试图让我的当前GPS坐标更新一个标签。我没有编译问题,但我的坐标是0.00000, 0.00000iOS CoreLocation没有获得GPS坐标

这里是我的.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 
+0

在你叫什么点'startUpdatingLocation'什么如何实现委托方法? –

回答

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

感谢您指出如何nooby我。弄清楚了。多谢你们!

0

使用locationManager.location.coordinate.latitude的相反,保持CLLocationCoordinate2D类型的实例变量。你可以称它为currentLocation。然后,当您在代理方法locationManager:didUpdateLocations:中获得值时,请将值设置为currentLocation

您必须致电[locationManager startUpdatingLocation]并设置它的委托(以及实现该委托方法)。

您现在使用位置管理器的方式是错误的,我认为您应该遵循一个教程来获取基础知识。

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


    } 
+0

不应该在didUpdateLocation中调用updateLabel? – Will

0

我发现这是很有趣的使用位置作为单身人士,并将值保存到默认用户。我是年轻的程序员,并试图编写所有的代码。我用这个如下(此代码仍然需要进行重构和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);