4

左上と右下の 2 つの座標があります。地域の中心点を見つけたいです。現在、私はそれを計算するために次の方法を持っています。中心点がずれています。でメソッドを呼び出すと

[self.map setRegionTopLeft: CLLocationCoordinate2DMake(21.57524, -157.984514)
               bottomRight: CLLocationCoordinate2DMake(21.309766, -157.80766)
                  animated:YES];

米国ハワイ州のオアフ島を中心とする必要があります。ここでこの数学を見つけたので、何が起こっているのかわかりません。

コード A - これはかなり外れています。それは私を島の近くに置きません。

- (CLLocationCoordinate2D)centerPointFromRegionTopLeft:(CLLocationCoordinate2D)topLeft
                                           bottomRight:(CLLocationCoordinate2D)bottomRight
{
    CLLocationCoordinate2D centerPoint;

    centerPoint.longitude = (topLeft.longitude + bottomRight.longitude) / 2;
    if (fabs(bottomRight.longitude - topLeft.longitude) > 180)
    {
        if (centerPoint.longitude > 0)
        {
            centerPoint.longitude = centerPoint.longitude + 180;
        } else {
            centerPoint.longitude = centerPoint.longitude - 180;
        }
    }

    centerPoint.latitude = asin((sin(bottomRight.latitude) + sin(topLeft.latitude))/2);

    return centerPoint;
}

私ももともと、この方法を試しました。長方形の中心を考えたときに頭に浮かんだのはまさにそれです。中心のあるべき場所にかなり近づけば、島が見えますが、まだずれています。

コード B - 私が試した元のコード。これは私が期待したものにはるかに近いですが、まだオフです.

- (CLLocationCoordinate2D)centerPointFromRegionTopLeft:(CLLocationCoordinate2D)topLeft
                                           bottomRight:(CLLocationCoordinate2D)bottomRight
{
    CLLocationCoordinate2D centerPoint;

    centerPoint.latitude =  ((topLeft.latitude + bottomRight.latitude) / 2);
    centerPoint.longitude = ((topLeft.longitude + bottomRight.longitude) / 2);

    return centerPoint;
}

座標領域 (topLeft、bottomRight) が与えられた場合、中心座標を取得するにはどうすればよいですか? アイデアは、任意の 2 つの座標を指定して中心座標を取得できるようにすることです。

更新*コード B が機能します。topLeft と bottomRight が間違っていました。コード A は、私を非常に南に置き、本来あるべき場所よりも少し東に置きます。

4

1 に答える 1

0

You need the middle of L(longitude) and B(latitude). For B the problem is around the pole, but as you set it, you simply can't "put the cap on the pole", so, really no problems here.

Middle(B1,B2)=(B1+B2)/2.

But L is much worse. L can jump from -179 to -179. And another problem : the middle of (-179,+179) should be 180, and middle(-1,+1) should be 0. I.e., we should always choose middle along shorter way between opposite points, not around the whole Earth.

We should move the zero meridian so, that the difference between L1,L2 will be smaller, than 180, make normal middle of them and then return the zero meridian back.

  • Let L1
  • if L2-L1>180, let's choose L2 for the new zero meridian.
    • shift=L2
    • L2=L2-shift, L1=L1+360-shift. Now, notice, L1-L2<180!
    • LmShifted=(L1+L2)/2
    • Lm=LmShifted+shift.
    • If we'll take these formulas together, we'll have:
    • Lm=(L1-L2+360)/2+L2
  • if L2-L1<180, Lm=(L1+L2)/2

The problem is when L2-L1=180. In this case you have two opposite meridians, dividing the Earth in two, and for the role of the middle both "quarter" meridian, to the right and to the left, fit. It's up to you, what to choose.

于 2014-02-15T00:02:37.503 に答える