2011-02-03 8 views
3

2つのジオポイント(2つの緯度/経度のペア)を指定した正確な距離(メートル)を取得するにはどうすればよいですか?2つのジオポイント間の距離ですか?

重複の可能性:

Distance Between Two GEO Locations

Calculating the distance of geo locations

Android calculate distance between two locations

How to find distance from the latitude and longitude of two locations?

+1

質問を解析できません。句読点を追加できますか? – mik01aj

答えて

3

あなたはこのスニペットを使用できる2点の座標からの距離を取得したい場合:

#include <math.h> 
#define DEG2RAD(degrees) (degrees * 0.01745327) 
#define RADIUS_OF_EARTH 6378.1 

+ (float) getDistanceFromStartCoords:(CLLocationCoordinate2D)start andEndCoords:(CLLocationCoordinate2D)end 
{ 
    float dist = acos((cos(DEG2RAD(start.latitude))* 
       cos(DEG2RAD(end.latitude))* 
       cos((-1*DEG2RAD(end.longitude))- 
        (-1*DEG2RAD(start.longitude)))) + 
       (sin(DEG2RAD(start.latitude))* 
       sin(DEG2RAD(end.latitude)))) * 
      RADIUS_OF_EARTH; 

    return dist; 
} 
+0

世界は球形ではありません。ジオイドに対するあなたの位置の計算は、最大21kmの誤差があります。 –

+1

@グラハム正しい...しかし、Manish Jainは距離の2メートルでポイントをチェックしません! 2メートルで、世界のヒューズは変わらない! – elp

+0

CLLocationCoordinate2DではなくCLLocation変数の私の距離 –

3

2メートルの解像度を与える距離測定はiPhoneにはありません。次の2つの場所の間メートルの変位を得るために、コア場所の-[CLLocation distanceFromLocation: otherLocation]メソッドを使用しますが、心に留めすることができますどこにも私はAppleがそれらの座標のために使用されるものジオード、それはです本当にいるかどうかを説明しない見てきたことを

  • 位置の異なる計算のための同じ地点
  • 彼らが使用するモデルは、フィールドサイズの領域内の人間サイズのオブジェクト間の距離を計算するためにかなりぼんやりとしている高度を考慮に入れません。ロンドンとモスクワ間の距離を計算するのは問題ありません。誤差は小さいです。
  • お使いのデバイスは、動き検出と組み合わせて、本当に、高精度の位置データを使用して、接続しない状態では、完全にデバイスがwithin tens of metresにある場合、あなただけ言うことができる動き検出を使用せずにバッテリー
  • を吸うために起こっています。
2

これは、上記の溶液に「improvment」です。標高情報が追加されます。リンゴが返す標高はメートルであるようです。飛行または軌道には適していませんが、他の人のすぐ上の15階、近くの山などに誰かがいる場合はうまくいきます。広範囲にテストされていません。それはあなたが20km以上離れて何かのために高度を気にしないと仮定します。その後、相手に近づくにつれて高度補正を行います。したがって、互いに20m離れた2人の人にとって、100m高いと、約102mの距離になります。最後に、私は戻ってくるためにkmに切り替える。また、元のコードでは、ナンバーが見つかりました。

#define DEG2RAD(degrees) (degrees * 0.01745329251) 
#define RADIUS_OF_EARTH 6371000.0 
// km 
+ (double)getDistanceFromStartCoords:(CLLocationCoordinate2D)start altStart:(double)altStart andEndCoords:(CLLocationCoordinate2D)end altEnd:(double)altEnd; 
{ 
    double argument = (cos(DEG2RAD(start.latitude))* 
       cos(DEG2RAD(end.latitude))* 
       cos((-1*DEG2RAD(end.longitude))- 
        (-1*DEG2RAD(start.longitude)))) + 
       (sin(DEG2RAD(start.latitude))* 
       sin(DEG2RAD(end.latitude))); 

    double dist = 0.0; 
    if (argument < 1.0 && argument > -1.0) // acos will return nan for very small (0) distance 
     dist = acos(argument)*RADIUS_OF_EARTH; 
// else 
//  NSLog(@"found bug, %f", acos(argument)); 


    // Altitude hack. 
    // blend in an altitude correction (blend for smoothness) 
    // add in altitude difference 
    double altDiff = fabs(altStart - altEnd); // altdiff 
    double factor = 1.0 - dist/20000.0; 
    if (factor < 0.0) 
     factor = 0.0; 

    dist += sqrt(dist*dist + factor*altDiff*altDiff); 

    //NSLog(@"distance found, %f", dist); 
    return dist/1000.0; // return km 
} 
関連する問題