我正在尝试创建一个基于位置的应用程序,我一直在尝试调试它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;
}
其中currentPoint是我创建的Pointxy类中的一个对象,它只不过是一个具有两个属性lat和longit两个浮点数的对象。
然后我创建了另一个名为Shape的类,它有一个NSMutableArray(ptArray)和一个名为area的属性,它是一个long double。
然后我在ptArray中存储了一堆尖头对象,然后在以下Shape方法中使用它们:
-(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,我不应该看到为什么?我想这可能与我的变量精确度有关系吗? 非常感谢你
答案 0 :(得分:0)
我刚刚弄清楚了我的错误。我在调用[ptArray addObject:currentPoint];
,其中currentPoint
是pointxy
个实例。
之后的事情就是我为currentPoint.lat
和longit
分配了一个新值,但是因为当我更改值时addObject
方法通过引用传递了Object,所以更改了值数组对象,然后我又添加了currentPoint
,但由于它是通过引用传递的,我现在在我的数组中有一组相等的点,并且这些点内的图中的区域确实为零! / p>