Search code examples
ioscore-locationcllocationmanagercllocationcllocationdistance

Initial starting location won't always start at ZERO


When I first start location updating the value will not be 0 always. Sometimes it is but sometimes I get values slightly above 0. I will get a value such as 2. So of coarse I hit the stop and start again then ill get a smaller value and continue doing so until I get zero. Even though my desired distance filter is 250 meters ill get values at the start below this! Why is this happening and how do I deal with this.The following is especially a problem when starting this indoors(giving me the idea that this is some G-P-S initial establishing problem). I can deal with this with a simple alert view to confirm the user is outdoors before starting but this occurs sometimes when outside so I want this issue solved. Also when it does start at 0 and I start driving immediately I get inaccurate values. Seems like I have to wait like 30 seconds before the starting to drive before I get accurate readings.

MY GUESS FOR A SOLUTION: NEED TO CONFIRM SOME HOW I HAVE A SOLID G-P-S CONNECTION BEFORE THE LOCATION UPDATING BEGINS.

Don't see anything in the CLLocationManager class reference that can be of any help to the above solution.

CLLocationManager class reference

Heres an example of what I mean with the starting and stopping when it doesn't start at 0.

Trigger Start - > Location = 1.1 km
Trigger Stop
Trigger Start - > Location = .98 km
Trigger Stop
Trigger Start - > Location = .55 km
Trigger Stop
Trigger Start - > Location = .02 km
Trigger Stop
Trigger Start - > Location = 0 km (finally where it should start from)

Here is my code:

@interface DriveLog (){

    CLLocationDistance totalDistanceBetween;
    NSString *tripString;
    Settings *settingsVC;
    CGFloat km;

}
- (void)viewDidLoad{

    totalDistanceBetween = 0;

    locationManager = [[CLLocationManager alloc] init];
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    locationManager.delegate = self;
    locationManager.distanceFilter = 250;
    startLocation = nil;

    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (IBAction)start:(id)sender {

    [locationManager startUpdatingLocation];
}


- (IBAction)stop:(id)sender {

    totalDistanceBetween = 0;
    startLocation = nil;
    [locationManager stopUpdatingLocation];

}

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

    if (startLocation == nil){
        totalDistanceBetween = 0;
        self.startLocation = newLocation;
    }
    CLLocationDistance distanceBetween = [newLocation distanceFromLocation:startLocation ];
    self.startLocation = newLocation;
    totalDistanceBetween += distanceBetween;

    if(kmOrMile == 0){
        km = totalDistanceBetween * 0.001;

    }else{
        km = totalDistanceBetween * 0.000621371192237334;
    }

    tripString = [[NSString alloc]
                            initWithFormat:@"%.02f%@",
                            km, kmOrMileString];

    distance.text = tripString;
}

Solution

  • The problem was with my device. My device was in rough shape and has had underwater experiences I guess making the hardware inside a little defected. Just a reminder to everyone to always try on another device. Could always be your phone even when you least expect it.