iOs (CoreLocation) - locationManager:didUpdateLocations: not working as expected

会有一股神秘感。 提交于 2019-12-23 16:42:08

问题


I wrote a simple iPhone app that retrieves location information and simply display them. I want to increment the distanceTraveled variable every time the position changes. My problem is that in the method locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations, locations.count is always equal to one. I thought it would add an element to the array every time the method is called, but it seems not like that...

I am running the application in the simulator (I don't have a device now).

This is my code:

// BIDViewController.h

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

@interface BIDViewController : UIViewController <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *startingPoint;

@property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracyLabel;
@property (strong, nonatomic) IBOutlet UILabel *altitudeLabel;
@property (strong, nonatomic) IBOutlet UILabel *verticalAccuracyLabel;
@property (strong, nonatomic) IBOutlet UILabel *distanceTraveledLabel;

@end



// BIDViewController.m :

#import "BIDViewController.h"

@interface BIDViewController ()

@property (strong, nonatomic)CLLocation *currentLocation;
@property (strong, nonatomic)CLLocation *lastLocation;
@property CLLocationDistance distanceTraveled;

@end

@implementation BIDViewController

@synthesize latitudeLabel;
@synthesize locationManager;
@synthesize longitudeLabel;
@synthesize startingPoint;
@synthesize altitudeLabel;
@synthesize distanceTraveledLabel;
@synthesize horizontalAccuracyLabel;
@synthesize verticalAccuracyLabel;
@synthesize currentLocation;
@synthesize lastLocation;
@synthesize distanceTraveled;

- (void)viewDidLoad
{
    [super viewDidLoad];

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    //locationManager.distanceFilter = 10.0f;

    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    if (startingPoint == nil) {
        startingPoint = [locations lastObject];
        NSLog(@"first time -- locations.count = %d", locations.count);
        distanceTraveled = 0;
        distanceTraveledLabel.text = @"0m";
    }
    else if (locations.count > 1) {
        NSLog(@"into the else if -- locations.count = %d", locations.count);
        lastLocation = locations[locations.count -2];
        distanceTraveled = distanceTraveled + [currentLocation distanceFromLocation:lastLocation];
        distanceTraveledLabel.text = [NSString stringWithFormat:@"%gm", distanceTraveled];
    }

    NSLog(@"locations.count = %d", locations.count);

    currentLocation = [locations lastObject];

    latitudeLabel.text =
        [NSString stringWithFormat:@"%g\u00B0", currentLocation.coordinate.latitude];
    longitudeLabel.text =
        [NSString stringWithFormat:@"%g\u00B0", currentLocation.coordinate.longitude];
    horizontalAccuracyLabel.text =
        [NSString stringWithFormat:@"%gm", currentLocation.horizontalAccuracy];
   verticalAccuracyLabel.text =
        [NSString stringWithFormat:@"%gm", currentLocation.verticalAccuracy];
    altitudeLabel.text =
        [NSString stringWithFormat:@"%gm", currentLocation.altitude];

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSString *errorString = [[NSString alloc] init];

    switch (error.code) {
        case kCLErrorLocationUnknown:
            errorString = @"Location unknown";
            break;

        case kCLErrorDenied:
            errorString = @"Access denied";
            break;

        case kCLErrorNetwork:
            errorString = @"No network coverage";
            break;

        case kCLErrorDeferredAccuracyTooLow:
            errorString = @"Accuracy is too low to display";
            break;

        default:
            break;
    }

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error getting location"
                                                    message:errorString
                                                   delegate:nil
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

@end

This is what the logger looks like:

2012-11-21 11:28:34.882 WhereAmI[652:11c03] first time -- locations.count = 1
2012-11-21 11:28:34.897 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:35.835 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:36.835 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:37.836 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:38.836 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:39.837 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:40.837 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:41.838 WhereAmI[652:11c03] locations.count = 1
2012-11-21 11:28:42.839 WhereAmI[652:11c03] locations.count = 1

and so on...

Any advice? Thanks everyone and, as always, sorry for the bad english!


回答1:


Simulator will not return more than one location since it uses the location of your computer, so you get the precision of your Wi-Fi and that's it unless you change to another Wi-Fi network somewhere else while running it should never return a more accuracy/different location.

If you're testing on an actual device that moves, or if the device first gives you the cell tower accuracy, then Wi-Fi, then GPS accuracy you will receive more than one location in the array.

Hope this clears it up.



来源:https://stackoverflow.com/questions/13491594/ios-corelocation-locationmanagerdidupdatelocations-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!