GPS距离计算
我想计算两个 GPS 位置之间的距离,每个位置都有一个纬度值和一个经度值。对于短距离结果,计算应该是准确的。例如。 < 300m。如果我使用 Google Earth(请参阅我的代码中的坐标),距离约为 136m。 如果我使用文章提供的解决方案: http://www.movable-type。 co.uk/scripts/latlong.html(半正矢公式)结果与此相差甚远。
使用的代码:
public void GpsCalc(){
double d = getDistance(51.342299,4.371359, 51.342490,4.371997);
Log.e("GpsCalc", String.valueOf(d));
}
public static double getDistance(double lat1, double lng1, double lat2, double lng2){
double R = 6371; // earth’s radius (mean radius = 6,371km)
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lng2-lng1);
double a = 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);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dr1 = R * c;//in radians
Log.e("getDistance-dr1", String.valueOf(dr1));
return dr1;
}
我确信它应该是一些小的变化,但我看不到它。
I want to calculate the distance between two GPS locations, each with a latitute value and a longitude value. The calculations should be accurate for short-distance results. eg. < 300m. If I use Google Earth (see coord in my code) , the distance is ~136m.
If I use the solution provided by article: http://www.movable-type.co.uk/scripts/latlong.html (the haversine formula) the result is nothing near that.
used code:
public void GpsCalc(){
double d = getDistance(51.342299,4.371359, 51.342490,4.371997);
Log.e("GpsCalc", String.valueOf(d));
}
public static double getDistance(double lat1, double lng1, double lat2, double lng2){
double R = 6371; // earth’s radius (mean radius = 6,371km)
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lng2-lng1);
double a = 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);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dr1 = R * c;//in radians
Log.e("getDistance-dr1", String.valueOf(dr1));
return dr1;
}
I'm sure it should be some minor change, but i can't see it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对该页面上的半正矢公式也遇到了麻烦。我知道这并不完全是你问题的答案,但我在余弦定律公式上取得了更大的成功,它给出了与谷歌地球相同的结果。如果它有帮助,它看起来像这样:
编辑:
我在 Google 地图和 Google 地球以及我的代码中尝试了您的坐标,我得到了 49m 的坐标。也许从来就没有问题?
I have also had trouble with the haversine formula on that page. I know it's not precisely an answer to your question, but I had more success with the law of cosines formula, which gives the same results as Google Earth. In case it helps, it looked like this:
EDIT:
I tried your coordinates in Google Maps and Google Earth and in my code, and I'm getting 49m for all of them. Maybe there was never a problem?
有点晚了,但还有两个选择
使用 Apple Corelocation(49.2733 米)
手动计算(49.1393米)
包含数学库
也许我更喜欢第二种方法(手动计算)。
A bit late, but two more options
Using Apple Corelocation (49.2733 meters)
Manual calculation (49.1393 meters)
Include Math library
Perhaps I prefer the second method (Manual calculation).