计算两个地理位置之间的距离


76

请阐明这种情况

现在,我有两个数组,它们具有附近地点的经度和纬度,并且还具有用户位置latiude和longiude现在,我想计算用户位置和附近地点之间的距离,并希望在listview中显示它们。

我知道有一种方法可以计算距离

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

现在的问题是,在这种方法中如何通过这两个具有附近纬度和经度的数组,并获得距离数组。


请查看此链接: stackoverflow.com/questions/5739734/… 希望对您有所帮助。
Dinesh Sharma

感谢您的答复,但我想使用上述方法计算距离。
阳光明媚,时间

请澄清一下,从技术上讲,您只是给我们提供了方法签名,该信息不足以知道在哪里可以找到方法文档。尝试添加有关包含类的信息
Marmoy

停止使用distanceTo因为它retourns错误的价值观: stackoverflow.com/questions/30664031/...
Karamsa

@Karamsa,但在您的链接中描述它正常工作。
CoolMind

Answers:


187

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

查看distanceTo或distanceBetween。您可以根据经纬度创建位置对象:

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

这会给两个地方之间的距离带来距离吗?
Harsha MV

2
不,它返回位移(两点之间的最短距离)
Zeeshan Chaudhry13年

2
float pk = (float) (180/3.14169);-小拼写错误,不是Pi ...应为3.141 5 9或Math.PI使用(也不Math推荐使用class FloatMath)。57个投票没有人注意到?:D
snachmsm

是否有人确认是否locationA.distanceTo(locationB)考虑到海拔高度?
秘密松鼠

2
第二个代码块是做什么用的?你为什么不只用Location. distanceBetween()呢?
spaaarky21 2016年

16

尝试此代码。这里我们有两个经度和纬度值,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);

这里的“距离”是位置A与位置B之间的距离(以米为单位)


3

只有一个用户Location,因此您可以迭代附近的地点列表,可以调用该distanceTo()函数来获取距离,并且可以根据需要存储在数组中。

据我了解,distanceBetween()在遥远的地方,它的输出是WGS84椭球。


您能建议哪一个提供准确的距离吗?
Paresh Mayani

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

    }

1

distanceTo将为您提供两个给定位置ej target.distanceTo(destination)之间的距离(以米为单位)。

distanceBetween也可以为您提供距离,但会将距离存储在float(results [0])数组中。文档说如果结果的长度为2或更大,则初始方位将存储在results [1]中。如果结果的长度为3或更大,则最终方位角将存储在results [2]中

希望这会有所帮助

我用distanceTo来获得从A点到B点的距离,我认为这是要走的路。


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;

    }

0

我想自己实现这一点,最终阅读了有关大圆距离公式的维基百科页面,因为没有足够的代码可读性可供我用作基础。

C#示例

    /// <summary>
    /// Calculates the distance between two locations using the Great Circle Distance algorithm
    /// <see cref="https://en.wikipedia.org/wiki/Great-circle_distance"/>
    /// </summary>
    /// <param name="first"></param>
    /// <param name="second"></param>
    /// <returns></returns>
    private static double DistanceBetween(GeoLocation first, GeoLocation second)
    {
        double longitudeDifferenceInRadians = Math.Abs(ToRadians(first.Longitude) - ToRadians(second.Longitude));

        double centralAngleBetweenLocationsInRadians = Math.Acos(
            Math.Sin(ToRadians(first.Latitude)) * Math.Sin(ToRadians(second.Latitude)) +
            Math.Cos(ToRadians(first.Latitude)) * Math.Cos(ToRadians(second.Latitude)) *
            Math.Cos(longitudeDifferenceInRadians));

        const double earthRadiusInMeters = 6357 * 1000;

        return earthRadiusInMeters * centralAngleBetweenLocationsInRadians;
    }

    private static double ToRadians(double degrees)
    {
        return degrees * Math.PI / 180;
    }
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.