我正在开发一个应用程序,该应用程序计算该人行进的距离。我正在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
我试着改进这个问题,但是我对可怕的代码格式无能为力。 @Siddharth:如果这是真实的代码,请在格式上努力*努力*。正确缩进代码并删除所有冗余空行。没有人可以阅读,甚至很少有人会尝试。 – 2010-06-25 14:34:35
谢谢Rudolph ..我会尝试改进,但现在你能解释我的距离计算逻辑有什么问题吗?实际上我真正的代码缩进是好的,但是当我在这里粘贴我的代码时,我不知道y缩进出错... – Siddharth 2010-06-25 14:39:54
I我已经习惯了代码格式化 - Siddarth也许可以进一步改进它:查看我在代码块中使用标记的方式。 – Andiih 2010-06-25 16:05:28