views:

114

answers:

1

Hi All,

I am developing an application that computes the distance travelled by the person. I am testing it on a iPad (Device-3.2). My iPad is using WiFi to get the current location. The results are highly inaccurate even though i have filtered the values. I don't know if GPS will give me accurate results. I am pasting the entire code below. Please verify the code and in case of errors please let me know. It would b very helpful if some one test the code on iPhone(3g) or iPad(3g). If not possible then just check the logic.....also i want to compute the calories burnt ..is there any formula to do so..? I have made simple view based project.....and used a distance label in nib file to set distance value but distance is updating at a very rapid rate....please correct it.




    //  iPacometerViewController.h
    @interface iPacometerViewController : UIViewController {
    CLLocationManager *locationManager;
    CLLocation *oldLocat;
    CLLocation *newLocat;
    IBOutlet UILabel *distanceLabel;
    }

    @property(nonatomic,assign)IBOutlet UILabel *distanceLabel;
    @property(nonatomic,retain)CLLocationManager *locationManager;
    @property(nonatomic,retain)CLLocation *oldLocat;
    @property(nonatomic,retain)CLLocation *newLocat;

-(void)computeDistanceFrom:(CLLocation *)oldL tO:(CLLocation *)newL;

@end

//  iPacometerViewController.m

#import "iPacometerviewController.h"

@implementation iPacometerViewController

static double distance = 0.0;
@synthesize locationManager;
@synthesize oldLocat;
@synthesize newLocat;
@synthesize distanceLabel;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
       [super viewDidLoad];

    //initializing location manager
    locationManager =[[CLLocationManager alloc]init];
    locationManager.delegate = self;
    locationManager.distanceFilter = 150.0f;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];
    oldLocat = [[CLLocation alloc]init];
    newLocat = [[CLLocation alloc]init];
}


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

    if (newLocation.horizontalAccuracy  60.0) return;   // data is too long ago, don't use it

    NSLog(@"oldd %@",oldLocation);
    self.oldLocat = oldLocation;
    self.newLocat = newLocation;
    if(oldLocat!=nil)
    {
    [self computeDistanceFrom:oldLocat tO:newLocat];
    }
}


-(void)computeDistanceFrom:(CLLocation *)oldL tO:(CLLocation *)newL
         {
    NSLog(@"oldd %@",oldL);
    NSLog(@"new %@",newL);

    CLLocationDistance currentDistance = [oldL distanceFromLocation:newL];
    NSLog(@"you have travel=%f",currentDistance);   
        distance = distance + currentDistance;
    double distanceInKm = distance/1000;
    NSString *distanceLabelValue = [NSString stringWithFormat:@"%1.2f Kms",distanceInKm];
    distanceLabel.text = distanceLabelValue;
}   

- (void)didReceiveMemoryWarning {
     // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
     // Release any cached data, images, etc that aren't in use.
}



- (void)dealloc {
        //[mapView release];
    [oldloct release];
    [newLocat release];
    [locationManager release];
    [super dealloc];
}
@end

A: 

your logic is fine. Its the iPhone's logic that is bad.

There are two main issues that I see when writing this sort of code.

1) What I call poisoned points. These are cached points that the iPhone reports erroneously. You can check for them by dropping any points where the timestamp is the same as or earlier than the latest point. It may also be worth recording a frequency of visits to a point. The poisoned points tend to be visited time and time again (maybe 5 times) each time as a jump from your real track. If you can spot them, you can rule them out.

2) Accuracy - especially height changes. Look at the horizontal and vertical accuracy figures returned with each point. And look at the height. If they are inaccurate, then the velocity and therefore distance traveled will be too. One cause of bad distances is if one end of your pair has an altitude, and the other does not: the "not" gets classed as zero - so if you are 200m above the sea level at the time you have just travelled 200m without moving!

To improve accuracy, you may be better off writing your own great circle distance algorithm (or even simple Pythagoras given the distances are small) which ignores height, or just doing some better filtering on the points you use.

I'm struggling to find a reliable way of measuring location, distance & speed with the iPhone (3GS). and iPad or Touch is just going to be worse I guess.

If you can drop me a mail at andrew dot hawken at fiftyeggs dot co dot uk I will send you my raw iPhone logging software. It would be great if you can run it for a couple of trips and let me have the results - I'm trying to solve the same problem, but only have GPS datasets to work on.

Andiih
Thx Andiih.....but what should i do now....i have seen several apps in APP store which computes distance ,speed.....how do they do it.....??....i am badly stuck at this point..it would be very helpful if you can provide some piece of code that solve issues mentioned by you..THX
Siddharth
Andiih...........i am waiting for your reply ...please help me out...!
Siddharth
please provide your valuable feedback
Siddharth
you could utilise the techniques I suggested above to filter your data, you could use great circle or pythag to calc distance or try smoothing the results using a filter, or just sense checking speed against users mode of transport.
Andiih
@Andiih so you mean to say that instead of using distanceFromLocation: i should use great circle formula,and regarding filters.. i have already filtered the values how can i get more smoother results.please provide some code for it
Siddharth
what i think Andiih that getDistanceFrom method might be using great circle formula to compute distance.
Siddharth
I think it does use greatcircle, but does it account for changes in altitude ? I'm suggesting you can tweak the mechanism. I don't yet have improved code to share, but you can take a look here http://troybrant.net/blog/2010/02/detecting-bad-corelocation-data/ for some clues.
Andiih
@Andiih....initially i have used the ref suggested by you but that also dint worked...so can we conclude that we cant get accurate results using CLLocationManager..no matter how hard we try..because i have tried almost everything but dint get accurate reults..i don know what concept are used by similar apps present in AppStore.
Siddharth