2011-11-08 15 views
49

との間の距離を計算する今私は2つのアレイは、近くの場所の緯度と経度を有し、また、現在のユーザ位置latiudeとlongiudeを有する有し、この状況2つの地理的位置

に光を当ててください。私はとの間の距離を計算しますユーザーの所在地や近くの場所をリストビューに表示したい

私は

public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results); 

との距離を計算するための方法は、今の問題は何があるされていることを知っているが、この方法では近くの緯度とlongitueを持つこれら二つの配列を渡しと距離の配列を取得する方法です。

+0

(メートル単位)locationA & locationBの間の距離であります/計算の方法 - 2点間距離 - アンドロイドアプリ/ 5739789#3739789 Hoこれはあなたを助けるでしょう。 –

+0

返事ありがとう、私は上記の方法を使用して距離を計算したいと思います。 –

+0

技術的には、メソッドの文書がどこにあるのかを知るための情報ではないメソッドシグネチャしか与えられていないことを明確にしてください。含まれているクラスに関する情報を追加してみてください – Marmoy

答えて

140

http://developer.android.com/reference/android/location/Location.html

はdistanceToまたは間の距離を調べてください。あなたは、緯度と経度から位置オブジェクトを作成できます。

Location locationA = new Location("point A"); 

locationA.setLatitude(latA); 
locationA.setLongitude(lngA); 

Location locationB = new Location("point B"); 

locationB.setLatitude(latB); 
locationB.setLongitude(lngB); 

float distance = locationA.distanceTo(locationB); 

又は

private double meterDistanceBetweenPoints(float lat_a, float lng_a, float lat_b, float lng_b) { 
    float pk = (float) (180.f/Math.PI); 

    float a1 = lat_a/pk; 
    float a2 = lng_a/pk; 
    float b1 = lat_b/pk; 
    float b2 = lng_b/pk; 

    double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2); 
    double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2); 
    double t3 = Math.sin(a1) * Math.sin(b1); 
    double tt = Math.acos(t1 + t2 + t3); 

    return 6366000 * tt; 
} 
+0

これは道路によって2つの場所の間の距離を与えますか? –

+1

いいえ、ディスプレースメント(2点間の最短距離) –

+1

'float pk =(float)(180/3.14169);' - 小さなスペルミス、これはPiではありません。3.141 ** 5 ** 9または'Math.PI'が使われました(代わりに' Math'クラスは 'FloatMath'を廃止しました)。 57 upvotesと誰も気づいた? :D – snachmsm

3

場所が1つしかないので、繰り返し行うことができます。近くの場所のリストはdistanceTo()関数を呼び出して呼び出すことができます。望むなら配列に格納できます。

私が理解しているところでは、distanceBetween()は離れた場所にあり、出力はWGS84楕円です。

+0

正確な距離を教えてください? –

0

distanceToはあなたに与えられた二つの位置EJ target.distanceTo(宛先)との間のメートルでの距離を与えます。

distanceBetweenも距離を与えますが、距離をfloatの配列に格納します(結果[0])。結果が長さ2以上の場合、初期ベアリングは結果[1]に格納されます。結果は、長さ3以上を持っている場合は、最終軸受が結果に格納され、これは私はそれは方法だと思い、私はBに、点Aからの距離を取得するためにdistanceToを使用しました

を助けること[2]

希望行く。

10

このコードをお試しください。ここでは経度と緯度の2つの値があり、selected_location.distanceTo(near_locations)関数はこれらの場所間の距離をメートルで返します。 http://stackoverflow.com/questions/5739734:

ここ
Location selected_location=new Location("locationA"); 
      selected_location.setLatitude(17.372102); 
      selected_location.setLongitude(78.484196); 
Location near_locations=new Location("locationB"); 
      near_locations.setLatitude(17.375775); 
      near_locations.setLongitude(78.469218); 
double distance=selected_location.distanceTo(near_locations); 

"距離" は、このリンクをチェックアウト

+4

メトリックに言及するのに最適です.. !!! –

3
private static Double _MilesToKilometers = 1.609344; 
    private static Double _MilesToNautical = 0.8684; 


    /// <summary> 
    /// Calculates the distance between two points of latitude and longitude. 
    /// Great Link - http://www.movable-type.co.uk/scripts/latlong.html 
    /// </summary> 
    /// <param name="coordinate1">First coordinate.</param> 
    /// <param name="coordinate2">Second coordinate.</param> 
    /// <param name="unitsOfLength">Sets the return value unit of length.</param> 
    public static Double Distance(Coordinate coordinate1, Coordinate coordinate2, UnitsOfLength unitsOfLength) 
    { 

     double theta = coordinate1.getLongitude() - coordinate2.getLongitude(); 
     double distance = Math.sin(ToRadian(coordinate1.getLatitude())) * Math.sin(ToRadian(coordinate2.getLatitude())) + 
         Math.cos(ToRadian(coordinate1.getLatitude())) * Math.cos(ToRadian(coordinate2.getLatitude())) * 
         Math.cos(ToRadian(theta)); 

     distance = Math.acos(distance); 
     distance = ToDegree(distance); 
     distance = distance * 60 * 1.1515; 

     if (unitsOfLength == UnitsOfLength.Kilometer) 
      distance = distance * _MilesToKilometers; 
     else if (unitsOfLength == UnitsOfLength.NauticalMiles) 
      distance = distance * _MilesToNautical; 

     return (distance); 

    } 
0
public double distance(Double latitude, Double longitude, double e, double f) { 
     double d2r = Math.PI/180; 

     double dlong = (longitude - f) * d2r; 
     double dlat = (latitude - e) * d2r; 
     double a = Math.pow(Math.sin(dlat/2.0), 2) + Math.cos(e * d2r) 
       * Math.cos(latitude * d2r) * Math.pow(Math.sin(dlong/2.0), 2) 
     double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 
     double d = 6367 * c; 
       return d; 

    } 
関連する問題