Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 计算两个地理位置之间的距离_Android_Location_Distance_Latitude Longitude - Fatal编程技术网

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

Android 计算两个地理位置之间的距离,android,location,distance,latitude-longitude,Android,Location,Distance,Latitude Longitude,请解释一下这种情况 现在我有两个数组,有附近位置的纬度和经度,还有用户位置的纬度和经度,现在我想计算用户位置和附近位置之间的距离,并在listview中显示它们 我知道有一种计算距离的方法 public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results); 现在的问题是如何在这个方法中

请解释一下这种情况

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

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

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

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

只有一个用户位置,因此您可以迭代附近位置的列表。您可以调用
distanceTo()
函数来获得距离,如果您愿意,可以存储在数组中

据我所知,
distanceBetween()
用于遥远的地方,它的输出是一个WGS84椭球体

查看距离

返回此位置与目标之间的近似距离(以米为单位) 给定的位置。使用WGS84椭球体定义距离

或距离

计算两个位置之间的近似距离(以米为单位),以及 (可选)之间最短路径的初始方位角和最终方位角 他们。使用WGS84椭球定义距离和方位

可以从纬度和经度创建位置对象:

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


distanceTo将给出两个给定位置ej target.distanceTo(目的地)之间的距离,单位为米

distanceBetween也会为您提供距离,但它会将距离存储在浮点数组中(结果[0])。文档表示,如果结果的长度为2或更大,则初始方位将存储在结果[1]中。如果结果长度为3或更大,则最终轴承存储在结果[2]中

希望这有帮助


我已经使用distanceTo获得了从A点到B点的距离,我认为这是正确的方法。

试试下面的代码。这里我们有两个经度和纬度值以及所选的_位置。distance to(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”是位置A和位置B之间的距离(单位为

私人静态双里程里程=1.609344;
专用静态双密耳=0.8684;
/// 
///计算两个纬度和经度点之间的距离。
///大链接-http://www.movable-type.co.uk/scripts/latlong.html
/// 
///第一坐标。
///第二坐标。
///以长度为单位设置返回值。
公共静态双距离(坐标坐标坐标1、坐标坐标坐标2、单位长度单位长度)
{
双θ=coordinate1.getLongitude()-coordinate2.getLongitude();
双距离=Math.sin(ToRadian(coordinate1.getLatitude())*Math.sin(ToRadian(coordinate2.getLatitude()))+
Math.cos(ToRadian(coordinate1.getLatitude())*Math.cos(ToRadian(coordinate2.getLatitude()))*
cos(ToRadian(theta));
距离=数学acos(距离);
距离=到度(距离);
距离=距离*60*1.1515;
如果(单位长度==单位长度.km)
距离=距离*英里/公里;
else if(unitsOfLength==unitsOfLength.NauticalMiles)
距离=距离*u milesto;
返回(距离);
}

我想自己实现这一点,我最终阅读了上的Wikipedia页面,因为没有任何代码可读性足以作为我的基础

C#示例

//
///使用大圆距离算法计算两个位置之间的距离
/// 
/// 
/// 
/// 
/// 
专用静态双距离(地理位置第一,地理位置第二)
{
double longitudeDifferenceInRadians=Math.Abs(环面(第一个经度)-环面(第二个经度));
双中心LangleBetween位置sinradians=Math.Acos(
Math.Sin(环面(第一纬度))*Math.Sin(环面(第二纬度))+
Math.Cos(环面(第一纬度))*Math.Cos(环面(第二纬度))*
Cos(纵向微分半径);
常数双接地半径计=6357*1000;
返回接地半径米*位置之间的中心线弧度;
}
专用静态双toradian(双度)
{
返回度*Math.PI/180;
}

查看此链接:希望这能对您有所帮助。感谢您的回复,但我想使用上述方法计算距离。请澄清,从技术上讲,您只给了我们一个方法签名,这不足以知道在哪里可以找到方法文档。尝试使用distanceTo添加包含类Stop的信息,因为它返回了错误的值:@Karamsa,但在您的链接中描述它工作正常。您能建议哪一个提供了确切的距离吗?这是否提供了两个地方之间的公路距离?不返回位移(两点之间的最短距离)
float pk=(float)(180/3.14169);
-小的拼写错误,这不是Pi…它应该是3.14159或
Math。使用Pi
(也使用
Math
类而不是弃用
FloatMath
)。57次投票,没有人注意到?:DAnyone确认是否
位置a.distanceTo(位置b)
考虑了高度?第二个代码块是什么?为什么不使用
Location.distanceBetween()
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);
    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);

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

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