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

5 java android location

可能重复:
如何使用经度和纬度值计算两个位置之间的距离

在你们给我一些关于地球如何不是一个完美的球体的教育之前,当你靠近两极时它会发生变化,这似乎是我在网上找到的全部.在问我的父亲是一位多才多艺的地理学家和我的叔叔,他是一位为NASA工作的量子物理学家,而我是一名低级计算机程序员之前,我想先问问你们!

我只需要一个球场Km距离,因为乌鸦从手机飞到720平方公里面积的预先填充的位置列表,所以变化并不重要.

这是我现在,请不要丢弃任何东西.

mLatitude=-38.3533177
mLongitude=144.9127674
Run Code Online (Sandbox Code Playgroud)

这是十分钟前我离乌鸦飞的距离大约3公里

mLatitude=-38.3444385
mLongitude=144.9374762
Run Code Online (Sandbox Code Playgroud)

如何计算得到3公里?

我对Java很陌生,所以我不确定内置的Math函数是什么,我不知道计算是什么?

干杯,

麦克风.

anu*_*har 27

您可以使用类的distanceTo()方法Location来获取两个locatins之间的距离.


nos*_*a13 5

public class Calculator {
    private static final int earthRadius = 6371;
    public static float calculateDistance(float lat1, float lon1, float lat2, float lon2)
    {
        float dLat = (float) Math.toRadians(lat2 - lat1);
        float dLon = (float) Math.toRadians(lon2 - lon1);
        float a =
                (float) (Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
                        * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2));
        float c = (float) (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)));
        float d = earthRadius * c;
        return d;
    }
}
Run Code Online (Sandbox Code Playgroud)