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

3

我有两个位置,想要计算它们之间的距离(单位为米)。我写了一些代码,但它并不完美地工作。

private void getDistanceBetweenTwoPoints(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个回答

9
不需要重新发明轮子。
您可以使用“Location.distanceBetween()”方法。
根据文档
计算两个位置之间的大致距离(以米为单位)。
这里是一个简单的例子:
private float getDistanceBetweenTwoPoints(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表示西。


它对我有用。请解释一下distance[1]是什么? - Vardhman Patil
@ANDY_VAR,您可以在文档链接中查看解释:计算出的距离存储在results[0]中。如果results长度大于等于2,则初始方位角存储在results[1]中。如果results长度大于等于3,则最终方位角存储在results[2]中。 - Daniel Nugent
谢谢您的回复。但我想知道它的实际意义。我们在什么情况下会得到两个距离?此外,我想知道在这种情况下什么是方位角。 - Vardhman Patil
1
@ANDY_VAR 我刚刚更新了答案并添加了额外的信息。这里还有更多信息,特别是关于初始和最终方位角:https://www.reddit.com/r/answers/comments/gyfba/can_somebody_explain_to_me_the_difference_between/ - Daniel Nugent
请更正方法名称中的拼写错误。正确的应该是getDistanceBetweenTwoPoints。 - Emad Razavi

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接