2010-06-25 49 views
1

我正在开发一个应用程序,该应用程序计算该人行进的距离。我正在iPad上测试它(Device-3.2)。我的iPad使用WiFi来获取当前位置。即使我已经过滤了这些值,结果也是非常不准确的。我不知道GPS是否会给我准确的结果。我正在粘贴下面的整个代码。请验证代码,如有错误,请告诉我。如果有人在iPhone(3G)或iPad(3G)上测试代码,它会非常有帮助。如果不可能,那么只是检查逻辑.....我也想计算卡路里燃烧..有任何公式可以这么做..?我做了一个简单的基于视图的项目.....并使用nib文件中的距离标签来设置距离值,但距离以非常快的速度更新....请更正它。Corelocation框架不会产生准确的距离

 



    // 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 
 
+0

我试着改进这个问题,但是我对可怕的代码格式无能为力。 @Siddharth:如果这是真实的代码,请在格式上努力*努力*。正确缩进代码并删除所有冗余空行。没有人可以阅读,甚至很少有人会尝试。 – 2010-06-25 14:34:35

+0

谢谢Rudolph ..我会尝试改进,但现在你能解释我的距离计算逻辑有什么问题吗?实际上我真正的代码缩进是好的,但是当我在这里粘贴我的代码时,我不知道y缩进出错... – Siddharth 2010-06-25 14:39:54

+0

I我已经习惯了代码格式化 - Siddarth也许可以进一步改进它:查看我在代码块中使用标记的方式。 – Andiih 2010-06-25 16:05:28

回答

0

你的逻辑很好。它的iPhone的逻辑很糟糕。

编写这类代码时,我看到两个主要问题。

1)我称之为中毒点。这些是iPhone错误报告的缓存点。您可以通过删除时间戳等于或早于最新点的任何点来检查它们。记录一个访问点的频率也是值得的。中毒点往往会一次又一次(可能是5次)作为您真实赛道的跳跃而被访问。如果你能发现它们,你可以排除它们。

2)准确性 - 特别是高度的变化。查看每个点返回的水平和垂直精度数字。并看看高度。如果它们不准确,那么速度以及因此行进的距离也将是。不好的距离的一个原因是如果你的一对的一端有一个高度,另一个没有:“不”被归类为零 - 所以如果你刚刚在200米的高度上没有移动!

为了提高准确性,你可以更好地编写自己的大圆距离算法(或者考虑到距离很小的简单的毕达哥拉斯算法),忽略高度,或者只是对你使用的点进行更好的过滤。

我正在努力寻找一种可靠的方式来测量位置,距离&速度与iPhone(3GS)。而iPad或Touch会变得更糟,我想。

如果你可以给我发一封邮件在andrew dot hawken at fiftyeggs dot co dot uk我会给你发送我原始的iPhone日志软件。如果您可以运行它几次旅行并让我获得结果,那将是非常好的 - 我试图解决同样的问题,但只能使用GPS数据集。

+0

Thx Andiih .....但我现在应该做什么....我已经看到了几个应用程序在APP商店计算距离,速度.....他们怎么样做到这一点..... ?? ....我在这一点严重卡住..如果你可以提供一些解决你所提到的问题的代码将非常有帮助..THX – Siddharth 2010-06-25 16:43:57

+0

Andiih .... .......我在等待你的回复......请帮助我......! – Siddharth 2010-06-27 06:06:39

+0

请提供您的宝贵意见 – Siddharth 2010-06-27 16:44:23