0

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 回呼び出すと、異なる値が得られます。何か案は ?

4

1 に答える 1

0
double x,y,z = 0;

zゼロにのみ初期化されますがxy未定義の (ランダムな) 値があります。に変更します

double x = 0, y = 0, z = 0;
于 2012-09-25T18:24:18.613 に答える