我試圖做一個基於位置的應用程序,我一直試圖調試它3天,仍然無法弄清楚什麼是錯的......在我的視圖控制器中,我正在通過一些位置點(緯度和經度)所代表的方法:基於位置的應用程序調試
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
currentPoint.lat=newLocation.coordinate.latitude*M_PI/180;
currentPoint.longit=newLocation.coordinate.longitude*M_PI/180;
}
其中當前點是從Pointxy我已創建的類的對象,則只不過是具有兩個屬性lat和longit兩個浮筒的對象更多。
然後我創建了另一個叫做Shape的類,它有一個NSMutableArray(ptArray)和一個叫做area的屬性,它是一個long double。
我然後存儲一串在ptArray尖尖對象,然後預成型件在下面形狀的方法使用它們:
-(void)CalculateArea{
for (int i=0; i<[self NumberofPoints]-1; i++) {
Pointxy *pointi=[ptArray objectAtIndex:i];
Pointxy *pointiplus1=[ptArray objectAtIndex:i+1];
area-=6371.0*6371.0/2.0*(pointiplus1.longit-pointi.longit)*(2+sinf(pointiplus1.lat)+sinf(pointi.lat));
}
Pointxy *pointzero=[ptArray objectAtIndex:0];
Pointxy *pointlast=[ptArray objectAtIndex:[ptArray count]-1];
area-=6371.0*6371.0/2.0*(pointzero.longit-pointlast.longit)*(2+sinf(pointzero.lat)+sinf(pointlast.lat));
area=area*1000000.0;
}
我遇到的問題是,我然後嘗試使用此在的UILabel顯示區域代碼:
NSString *AreaString=[[NSString alloc]initWithFormat:@"%Lf",MyShape.area];
[AreaLabel setText:AreaString];
MyShape的地方是一個Shape對象,AreaLabel是的UILabel,我總是得到0.0000顯示,我應該不是任何人都可以看到,爲什麼?我想這可能是一個精確的問題,我的變量是嗎? 非常感謝你
座標以度爲單位,無需將其轉換爲弧度。您是否嘗試過使用NSLog打印座標?你有沒有使用已知的良好數據測試你的CalculateArea函數? – progrmr 2012-01-04 21:14:52
我沒有測試過,但我的來源是來自美國國家航空航天局的一篇論文:http://trs-new.jpl.nasa.gov/dspace/bitstream/2014/40409/1/07-03.pdf所以應該沒有錯誤它根本就沒有。無論如何,即使有同樣的錯誤,它怎麼會總是返回零?我將它轉換爲弧度以在sinf()函數中使用它...是的,我實際上有一個Label,它實時顯示存儲在數組中的座標,謝謝。 – Mppl 2012-01-04 22:26:24
@ user611295:美國國家航空航天局的論文可能沒有任何錯誤,但是它是你需要檢查和測試的算法的實現。另外,在Objective-C中,即使區域算法代碼是正確的,答案仍有很多方法可以達到零。如果你正在使用的一些對象沒有被alloc + init -ed,它們將是零,導致沒有操作。示例:如果MyShape爲零,則MyShape.area將返回零。如果ptArray爲零或爲空,您將得到零。等如progrmr建議,你需要添加日誌記錄,並進行單元測試。 – Anna 2012-01-05 03:05:40