问题描述
我已经使用 distanceTo()
两个位置之间
对象试过,但据我所知它不返回在所有情况下正确的价值观。相反,我写了一个自定义的方法,它返回什么,我认为是正确的值。
I've tried using distanceTo()
between two Location
objects, but AFAIK it isn't returning the correct values in all situations. Instead, I've written a custom method which returns what I think are the correct values.
float distanceBetween(Location l1, Location l2)
{
float lat1= (float)l1.getLatitude();
float lon1=(float)l1.getLongitude();
float lat2=(float)l2.getLatitude();
float lon2=(float)l2.getLongitude();
float R = 6371; // km
float dLat = (float)((lat2-lat1)*Math.PI/180);
float dLon = (float)((lon2-lon1)*Math.PI/180);
lat1 = (float)(lat1*Math.PI/180);
lat2 = (float)(lat2*Math.PI/180);
float a = (float)(Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2));
float c = (float)(2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)));
float d = R * c * 1000;
return d;
}
谁能告诉我有什么区别我和实施之间有什么 distanceTo()
正在恢复?
推荐答案
我是pretty肯定的是, Location.distanceTo()
正确设计。它采用了WGS84的做法,是高度precise准确。我最近使用 Location.distanceTo()
为一些测试用例,这是从来没有错的。
I'm pretty certain that Location.distanceTo()
is correctly designed. It uses the WGS84 approach which is highly precise and accurate. I recently used Location.distanceTo()
for a number of test cases and it was never wrong.
您还没有解释的为什么的你认为该方法不能正常工作。其可能的,你可能会得到错误的值,因为你已经不是一个位置
对象设置正确的经度和纬度值。此外,您还可以设置Android的源$ C $ C在IDE和检查的方法为自己。这是完全正确的。
You haven't explained why you think the method doesn't work properly. Its possible you may be getting wrong values because you haven't set the correct latitude and longitude values for a Location
object. Also, you can set up the source code of Android in your IDE and check the method for yourself. It is absolutely correct.
编辑:
在您发布的链接,有一个,简明概括了问题:
On that link you posted, there's an answer that concisely sums up the problem:
distanceTo()
正常工作。该错误是在你的身边,最有可能的
算法之一,例如如果没有GPS定位可用的,并且在电话
采用基于GSM小区的位置,这当然可以通过千米关闭。
有关您的应用程序大概要总结走过的距离,
只需要GPS定位,不要使用其他LocationProvider比GPS!
For your app that probably wants to sum up the distance travelled, take only GPS fixes, don't use another LocationProvider than GPS!
所以,故事的寓意是要确保你只使用GPS提供商距离计算。
So the moral of the story is to make sure that you use only the GPS provider for distance calculations.
编辑2:
谷歌建议使用。新的API被认为是更精确的。
Google recommends using the Google Play Services Location API over the android.location
API. The new API is supposed to be more accurate.
参考文献:
1 。
这篇关于如何Location类工作distanceTo()方法在内部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!