0

所以我是 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

4 回答 4

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

感谢您指出我是多么的笨拙。弄清楚了。多谢你们!

于 2013-03-28T21:58:56.333 回答
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];


  } 
于 2013-03-29T05:39:50.433 回答
0

不要使用locationManager.location.coordinate.latitude,而是保留类型的实例变量CLLocationCoordinate2D。你可以称它为currentLocation. 然后当你在委托方法中得到一个值时locationManager:didUpdateLocations:,设置它的值currentLocation

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

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

于 2013-03-28T21:16:49.600 回答
0

我发现将 location 用作单例并将值保存到 default user 是很有趣的。我是年轻的程序员,正在尝试用 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);
于 2014-05-19T13:19:49.270 回答