0

I am trying to calculate distance from start using Core Location framework, but when i put the Application on an iPhone device, the data is not correct. Distance keeps on fluctuating and showing random data. Kindly help me out. Also, Altitude is showing zero.

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

    //Altitude
    if(startingPoint==nil)
        self.startingPoint=newLocation;

    NSString *currentAltitude = [[NSString alloc] initWithFormat:@"%g",                                                          
                                 newLocation.altitude];

    heightMesurement.text=currentAltitude;
    [currentAltitude release];

    //Distance

    if(startingPoint==nil)
        self.startingPoint=newLocation;
    //if(newLocation.horizontalAccuracy <= 100.0f) { 
    //    [locationManager stopUpdatingLocation]; 
    //}

    //test start.......................................................

    //startlocation
    NSString *sp = [[NSString alloc] 
                            initWithFormat:@"%f",startingPoint];
    NSLog(@"\nStarting point=%@",startingPoint);
    ssp.text=sp;

    //endlocation

    NSString *ep = [[NSString alloc] 
                    initWithFormat:@"%f",newLocation]; 
    NSLog(@"\nStarting point=%@",newLocation);
    eep.text=ep;
    //test end............................................................


    CLLocationDistance mydistance=[newLocation distanceFromLocation:startingPoint];

    NSString *tripString = [[NSString alloc] 
                            initWithFormat:@"%f",mydistance]; 
    distLabel.text=tripString;

    [tripString release];
    //test........................

    [sp release];
    [ep release];

}//Location Manager ends..


//Time interval of 3 sec....
-(void)locationUpdate:(NSTimer*)timer{

    if(timer != nil) [timer invalidate];

    [self.locationManager startUpdatingLocation];
}
Nate
  • 31,017
  • 13
  • 83
  • 207

1 Answers1

1

As to why your altitude may be zero, please see this answer to a similar question.

This may just be a problem with your NSLog statements, but both the starting and ending points are printed out with NSLog statements that say

NSLog(@"\nStarting point=%@",

The way you seem to have scheduled a 3-second timer is not really the way iOS wants you to use CLLocationManager. The preferred way is to tell CLLocationManager what your location criterion are, and then just start it updating. You don't actually need to keep telling it to start updating every 3 seconds. You can just do it once, and then if you ever decide you don't need any more updates, then call

[locationManager stopUpdatingLocation];

If the OS has no new location information, it probably doesn't make sense to keep asking. It'll tell you when it has new location information, via locationManager:didUpdateToLocation:fromLocation. So, I would recommend starting the process more like this:

    CLLocationManager* locationManager = [[CLLocationManager alloc] init];
    if ([locationManager locationServicesEnabled]) {
        Reachability* netStatus = [Reachability sharedReachability];
        if (([netStatus internetConnectionStatus] != NotReachable) || ([netStatus localWiFiConnectionStatus] != NotReachable)) {
            locationManager.delegate = self;
            locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
            locationManager.distanceFilter = 100.0;  // 100 m, or whatever you want
            [locationManager startUpdatingLocation];
        } else {
            // TODO: error handling
        }
    }
    self.locMgr = locationManager;
    [locationManager release];

The location manager will often deliver you multiple results, with increasing accuracy as it hones in on your location. If you're continually restarting it, I'm wondering if that's causing it problems.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207
  • yes, @vikash, `viewDidLoad` would probably be an appropriate place to init the `CLLocationManager` and start it updating. depending on how your app is setup, it also might be better to put that code into `viewDidAppear:`. that way, whenever the user navigates back to the view that needs this code, the location starts updating again. you might then choose to call `[locationManager stopUpdatingLocation]` in `viewDidDisappear:`. but, that depends on whether or not you have multiple views in your app, how long users will spend on each view, and how efficient you want to be. – Nate May 16 '12 at 08:55
  • just to clarify, you could probably `init` the location manager once in `viewDidLoad`, and then simply call start/stop updating location in the `viewDidAppear:` and `viewDidDisappear:` implementations. – Nate May 16 '12 at 08:57
  • can u provide the souce code as i am getting error while using above code in viewDidload – vikash kumar May 16 '12 at 09:12
  • @vikash, an error on which line? do you have the `Reachability` class? you can find it in the Apple sample code on [developer.apple.com](http://developer.apple.com/iphone). or you can just remove the use of `Reachability`. it's just error handling for when the network isn't available. – Nate May 16 '12 at 10:38