两个地点之间的距离是不正确的? [英] distance between two locations is not right?

查看:204
本文介绍了两个地点之间的距离是不正确的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到使用此两个位置:

i get two locations using this :

LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

然后我计算出它们之间的距离,但是距离是不正确的。

then i compute the distance between them , but the distance is not true

public static float calculateDistance(double e, double f, double g, double h)
{
    float dLat = (float) Math.toRadians(g - e);
    float dLon = (float) Math.toRadians(h - f);
    float a =
            (float) (Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(e))
                    * Math.cos(Math.toRadians(g)) * Math.sin(dLon / 2) * Math.sin(dLon / 2));
    float c = (float) (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)));
    float d = 6371 * c;
    return d;
}

(32.2163799,35.0420986)
(31.9210915,35.2037014)

(32.2163799,35.0420986) (31.9210915,35.2037014)

结果36.193707公里
但实际比这更多,大约85公里

the result 36.193707 km but the actual is more than that , approximately 85 km

推荐答案

您可以使用非标准的功能是:

You can use standart function for this:

Location location1 = new Location("<provider name");
location1.setLatitude(lat1);
location1.setLongitude(lng1);

Location location2 = new Location("<provider name");
location2.setLatitude(lat2);
location2.setLongitude(lng2);

float distance = location1.distanceTo(location2);

更多信息:<一href=\"http://developer.android.com/reference/android/location/Location.html#distanceTo%28android.location.Location%29\" rel=\"nofollow\">http://developer.android.com/reference/android/location/Location.html#distanceTo%28android.location.Location%29

或函数之间的距离:
<一href=\"http://developer.android.com/reference/android/location/Location.html#distanceBetween%28double,%20double,%20double,%20double,%20float[]%29\" rel=\"nofollow\">http://developer.android.com/reference/android/location/Location.html#distanceBetween%28double,%20double,%20double,%20double,%20float[]%29

这篇关于两个地点之间的距离是不正确的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆