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

时间:2021-07-26 16:53:39

please shed some light on this situation

请解释一下这种情况。

Right now i have two array having latitude and longitude of nearby places and also have the user location latiude and longiude now i want to calculate the distance between user location and nearby places and want to show them in listview.

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

I know that there is a method for calculating distance as

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

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

Now what is the problem is how to pass these two array having nearby latitude and longitue in this method and get the array of distances.

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

6 个解决方案

#1


144  

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

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

Look into distanceTo or distanceBetween. You can create a Location object from a latitude and longitude:

观察距离或距离。您可以从纬度和经度创建一个位置对象:

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

or

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

#2


11  

Try This Code. here we have two longitude and latitude values and selected_location.distanceTo(near_locations) function returns the distance between those places in meters.

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

here "distance" is distance between locationA & locationB (in Meters)

这里的“距离”是locationA和locationB之间的距离(以米为单位)

#3


3  

There is only one user Location, so you can iterate List of nearby places can call the distanceTo() function to get the distance, you can store in an array if you like.

这里只有一个用户位置,所以您可以在附近的位置进行迭代,可以调用distance to()函数来获得距离,如果您愿意,可以存储在数组中。

From what I understand, distanceBetween() is for far away places, it's output is a WGS84 ellipsoid.

根据我的理解,distanceBetween()是指遥远的地方,它的输出是WGS84椭圆体。

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

    }

#5


0  

distanceTo will give you the distance in meters between the two given location ej target.distanceTo(destination).

距离将会给你在两个给定位置ej目标之间的距离。距离(终点)。

distanceBetween give you the distance also but it will store the distance in a array of float( results[0]). the doc says If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2]

distanceBetween也会给你距离,但它会将距离存储在一个浮点数组中(结果[0])。doc说,如果结果长度大于或等于2,则初始轴承存储在结果[1]中。如果结果长度为3或更大,则最终轴承存储在结果[2]中

hope that this helps

希望这对您有所帮助

i've used distanceTo to get the distance from point A to B i think that is the way to go.

我用了distanceTo来表示从点A到点B的距离,我想这是一条路。

#6


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;

    }

#1


144  

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

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

Look into distanceTo or distanceBetween. You can create a Location object from a latitude and longitude:

观察距离或距离。您可以从纬度和经度创建一个位置对象:

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

or

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

#2


11  

Try This Code. here we have two longitude and latitude values and selected_location.distanceTo(near_locations) function returns the distance between those places in meters.

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

here "distance" is distance between locationA & locationB (in Meters)

这里的“距离”是locationA和locationB之间的距离(以米为单位)

#3


3  

There is only one user Location, so you can iterate List of nearby places can call the distanceTo() function to get the distance, you can store in an array if you like.

这里只有一个用户位置,所以您可以在附近的位置进行迭代,可以调用distance to()函数来获得距离,如果您愿意,可以存储在数组中。

From what I understand, distanceBetween() is for far away places, it's output is a WGS84 ellipsoid.

根据我的理解,distanceBetween()是指遥远的地方,它的输出是WGS84椭圆体。

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

    }

#5


0  

distanceTo will give you the distance in meters between the two given location ej target.distanceTo(destination).

距离将会给你在两个给定位置ej目标之间的距离。距离(终点)。

distanceBetween give you the distance also but it will store the distance in a array of float( results[0]). the doc says If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2]

distanceBetween也会给你距离,但它会将距离存储在一个浮点数组中(结果[0])。doc说,如果结果长度大于或等于2,则初始轴承存储在结果[1]中。如果结果长度为3或更大,则最终轴承存储在结果[2]中

hope that this helps

希望这对您有所帮助

i've used distanceTo to get the distance from point A to B i think that is the way to go.

我用了distanceTo来表示从点A到点B的距离,我想这是一条路。

#6


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;

    }