我正在開發一個應用程序,該應用程序計算該人行進的距離。我正在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