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

waxmsbnn  于 2023-04-04  发布在  Android
关注(0)|答案(8)|浏览(207)

请给我解释一下这个情况
现在我有两个数组,有附近地方的纬度和经度,也有用户位置latiude和longiude,现在我想计算用户位置和附近地方之间的距离,并希望在listview中显示它们。
我知道有一种计算距离的方法

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

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

kkbh8khc

kkbh8khc1#

http://developer.android.com/reference/android/location/Location.html
眺望远方
返回此位置与给定位置之间的近似距离(以米为单位)。距离使用WGS84椭球定义。
或距离
计算两个位置之间的近似距离(以米为单位),还可以选择计算两个位置之间最短路径的初始方位角和最终方位角。距离和方位角使用WGS84椭球定义。
您可以从纬度和经度创建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;
}
v1l68za4

v1l68za42#

这里我们有两个经度和纬度值,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);

这里“distance”是locationA和locationB之间的距离(单位:Meters

oknwwptz

oknwwptz3#

只有一个用户Location,所以可以迭代附近地点的List可以调用distanceTo()函数来获取距离,如果喜欢可以存储在数组中。
据我所知,distanceBetween()是用于遥远的地方,它的输出是一个WGS84椭球。

8xiog9wr

8xiog9wr4#

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

    }
z9ju0rcb

z9ju0rcb5#

distanceTo将为您提供两个给定位置ej target之间的距离(以米为单位)。distanceTo(destination)。
distanceBetween也给予你距离,但它会将距离存储在一个float数组(results[0])中。文档说如果results的长度为2或更大,则初始方位存储在results[1]中。如果results的长度为3或更大,则最终方位存储在results[2]中
希望这能有所帮助
我已经用distanceTo来计算从A点到B点的距离,我认为这是正确的方法。

f0ofjuux

f0ofjuux6#

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;

    }
byqmnocz

byqmnocz7#

我想自己实现这一点,我最终阅读了Great-circle distance formula上的Wikipedia页面,因为没有代码可读到足以让我作为基础使用。
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;
    }
piok6c0g

piok6c0g8#

简单地使用.distanceTo方法。例如:

private fun getDistance(locationA:Location, locationB:Location){

    val distance = locationA.distanceTo(locationB)
}

相关问题