web-dev-qa-db-ja.com

2つの地理的位置間の距離の計算

この状況に光を当ててください

現在、近くの場所の緯度と経度を持つ2つの配列があり、ユーザーの場所の緯度と経度もあります。ユーザーの場所と近くの場所の間の距離を計算し、リストビューで表示したいです。

私は距離を計算する方法があることを知っています

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

ここで問題となるのは、このメソッドで緯度と経度が近くにあるこれらの2つの配列を渡し、距離の配列を取得する方法です。

62
john smith

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

DistanceToまたはdistanceBetweenを調べます。緯度と経度からLocationオブジェクトを作成できます。

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;
}
166
Karthi

このコードを試してください。ここでは、2つの経度と緯度の値があり、selected_location.distanceTo(near_locations)関数はそれらの場所間の距離をメートル単位で返します。

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);

ここで、「距離」は、locationAとlocationBの間の距離(メートル単位)です。

14
sandeepmaaram
    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);

    }
3
user2871325

ユーザーの場所は1つだけなので、近くの場所のリストを反復処理してdistanceTo()関数を呼び出して距離を取得できます。必要に応じて配列に格納できます。

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

3
Reno

distanceToは、指定された2つの場所ej target.distanceTo(destination)の間の距離をメートル単位で示します。

distanceBetweenは距離も提供しますが、float(results [0])の配列に距離を格納します。ドキュメントには、結果の長さが2以上の場合、初期方位はresults [1]に保存されます。結果の長さが3以上の場合、最終方位は結果に保存されます[2]

これが役立つことを願って

私はdistanceToを使用して、ポイントAからBまでの距離を取得しました。

1
eOnOe
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;

    }
0
Vinay