Search code examples
objective-ccllocation

How do I obtain the values associated with CLLocationManager - i.e. CLLocationSpeed, CLLocationDirection, CLLocationAccuracy, CLLocationCoordinates?


I have the following code in the .m file.

The code is to perform the following tasks:

  1. initialize the CLLocationManager class
  2. obtain the properties (Speed, Accuracy, Coordinates)
  3. convert these values to NSString
  4. log these values to the screen via use of NSLog
  5. set the values in the designated labels and then finally
  6. loop thru the code contained in method "locationUpdate".

The code provides an initial value for all these properties. However, the values are the same for every property (i.e. 2.7188880).

In addition, the code returns 0 for each property as I loop thru the method "locationUpdate".

Can anyone tell me the reason the code isn't returning values for the properties?

- (void)viewDidLoad {
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];

    [locationManager setDelegate:self];

    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];

    [locationManager startUpdatingLocation];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval: 2.0
                                                      target: self
                                                    selector:@selector(locationUpdate)
                                                    userInfo: nil repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];


    NSString *spd = [[NSString alloc] initWithFormat:@"%f", speed];

    NSString *crd = [[NSString alloc] initWithFormat:@"%f", coordinates];

    NSString *drc = [[NSString alloc] initWithFormat:@"%f", direction];

    NSString *acc = [[NSString alloc] initWithFormat:@"%f", accuracy];

    NSString *desc = [locationManager description];

    NSLog(@"Speed: %@", spd);
    NSLog(@"Coordinates: %@", crd);
    NSLog(@"Direction: %@", drc);
    NSLog(@"Accuracy: %@", acc);
    NSLog(@"Description: %@", desc);

    Speedometer.text = spd;
    CoordinatesLabel.text = crd;
    DirectionLabel.text = drc;
    AccuracyLabel.text = acc;
    DescriptionLabel.text = desc; 
}


- (void)locationUpdate {

    NSString *desc = [locationManager description];

    NSString *spd = [NSString stringWithFormat:@"%g", speed];
    NSString *crd = [NSString stringWithFormat:@"%g", coordinates];

    NSString *drc = [NSString stringWithFormat:@"%g", direction];

    NSString *acc = [NSString stringWithFormat:@"%g", accuracy];

    NSLog(@"Speed: %@", spd);
    NSLog(@"Coordinates: %@", crd);
    NSLog(@"Direction: %@", drc);
    NSLog(@"Accuracy: %@", acc);

    Speedometer.text = spd;
    CoordinatesLabel.text = crd;
    DirectionLabel.text = drc;
    AccuracyLabel.text = acc;
    DescriptionLabel.text = desc; 
}

Solution

  • You are not supposed to setup a timer. You simply implement the location manager delegate methods. When there is an update, you will be told.

    Your biggest issue is your use of the variables speed, coordinates, direction, and accuracy. You never set them anywhere (at least with any code you posted).

    Implement the locationManager:didUpdateLocations: and/or locationManager:didUpdateToLocation:fromLocation: methods. These will be called with updated location information. Use these to update your speed and other ivars.

    Edit: One other suggestion. Don't use string formats to format numbers to be displayed to the user. Use NSNumberFormatter instead. This way the numbers are formatted properly based on the user's locale.