ObjC で複数のジオポイント (経度、緯度で指定) の中間を計算したい
- (CLLocationCoordinate2D)middleOfPoints:(NSOrderedSet *)points
{
int i = 0; i = points.count;
double x,y,z = 0;
for (BuildingPoint *point in points)
{
// Iterate through Set
x += cos(point.latitude.doubleValue*(M_PI/180)) * cos(point.longitude.doubleValue*(M_PI/180));
y += cos(point.latitude.doubleValue*(M_PI/180)) * sin(point.longitude.doubleValue*(M_PI/180));
z += sin(point.latitude.doubleValue*(M_PI/180));
}
x = x/i;
y = y/i;
z = z/i;
double longitude = 180/M_PI * atan2(y,x);
double t = sqrt(x * x + y * y);
double latitude = 180/M_PI * atan2(z,t);
return CLLocationCoordinate2DMake(latitude, longitude);
}
しかし、何らかの理由で、同じ入力セットでメソッドを 5 回呼び出すと、異なる値が得られます。何か案は ?