2010-06-25 51 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