左上と右下の 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 は、私を非常に南に置き、本来あるべき場所よりも少し東に置きます。