以米为单位计算两个位置之间的距离

时间:2017-03-28 21:33:10

标签: android gps android-location android-gps

我有两个位置,我想以米为单位计算距离。我写了一些代码,但它没有完美运作。

private void getDistancBetweenTwoPoints(double lat1,double lon1,double lat2,double lon2)
{
    Location loc1 = new Location("");
    loc1.setLatitude(lat1);
    loc1.setLongitude(lon1);

    Location loc2 = new Location("");
    loc2.setLatitude(lat2);
    loc2.setLongitude(lon2);

    int R = 6371; // km

    double dLat = deg2rad(lat2-lat1);
    double dLon = deg2rad(lon2-lon1);
    double  a =
            Math.sin(dLat/2) * Math.sin(dLat/2) +
                    Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
                            Math.sin(dLon/2) * Math.sin(dLon/2)
            ;
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    double distanceInMeters = R * c;
    Log.e("distanceInMeters",distanceInMeters/10000+"mm");
}

public double  deg2rad(double deg) {
    return deg * (Math.PI/180);
}

如何计算米的距离?我的目标是,如果米> 200做点什么。我怎样才能解决我的问题?

3 个答案:

答案 0 :(得分:8)

没有必要为此重新发明轮子。

您可以使用Location.distanceBetween()方法。

来自the documentation

  

计算两个位置之间的近似距离

这是一个简单的例子:

private float getDistancBetweenTwoPoints(double lat1,double lon1,double lat2,double lon2) {

    float[] distance = new float[2];

    Location.distanceBetween( lat1, lon1,
            lat2, lon2, distance);

    return distance[0];
}

如果结果数组不仅仅填充了索引零,则其他索引每个都包含一个方位(初始和最终)。

方位是指定指南针方向的数字。

来自http://www.geomidpoint.com/destination/help.html

  

方位(或方位角)是指南针方向   起始点,并且必须在0到360的范围内.0代表   北,90是东,180是南,270是西。

答案 1 :(得分:0)

使用此方法计算两个纬度/经度和点之间的距离。他们的高度。

/**
 * Calculate distance between two points in latitude and longitude taking
 * into account height difference using the Haversine method as its base.
 *
 * Elevation should be in meters. If you are not interested in elevation, pass 0.
 *
 * @return Distance in meters
 */
private double distanceBetween(double lat1, double lat2, double lon1,
                               double lon2, double el1, double el2) {

    final int R = 6371; // Radius of the earth

    double latDistance = Math.toRadians(lat2 - lat1);
    double lonDistance = Math.toRadians(lon2 - lon1);
    double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
            + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
            * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double distance = R * c * 1000; // convert to meters

    double height = el1 - el2;

    distance = Math.pow(distance, 2) + Math.pow(height, 2);

    return Math.sqrt(distance);
}

答案 2 :(得分:0)

您可以使用以下代码计算两个位置的距离:

private double distanceBetween(double lat1, double lon1, double lat2, double lon2) {
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1))
            * Math.sin(deg2rad(lat2))
            + Math.cos(deg2rad(lat1))
            * Math.cos(deg2rad(lat2))
            * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = dist * 180.0 / Math.PI;
    dist = dist * 60 * 1.1515*1000;
    return (dist);
}

private double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}