当您在java中知道经度和纬度时,计算以米为单位的距离 [英] Calculate distance in meters when you know longitude and latitude in java
问题描述
可能重复:
使用Java中的纬度/经度值
< h3>重复:
- 使用纬度/经度Java中的值
- 如何计算两个纬度经度点之间的距离?
- Working with latitude/longitude values in Java
- How do I calculate distance between two latitude longitude points?
- 度数:分钟:秒(49°30'00N ,123°30'00W)
- 度:十进制分钟(49°30.0', - 123°30.0'),(49d30.0m,-123d30.0')
- 十进制度(49.5000°, - 123.5000°),一般有4-6个十进制数。
Duplicate:
我需要计算两个坐标给出的两点之间的距离。我正在研究的项目是一个Java项目,所以Java代码会很棒,但也可以给出伪代码,然后我可以自己实现:)
I need to calculate the distance between two points given by two coordinates. The project I am working on is a Java-project, so Java-code will be great, but pseudo-code can also be given, then I can implement it myself :)
您可能知道,有三种表示坐标的方法:
As you probably know, there are three ways to represent coordinates:
这是我的坐标的第三种方式,所以这个值的代码将是首选:)
It's the third way my coordinates are given in, so the code for this values will be preferred :)
推荐答案
基于< a href =https://stackoverflow.com/questions/120283/>关于stackoverflow的另一个问题,我得到了这个代码..这计算结果以米为单位,而不是以英里为单位:)
Based on another question on stackoverflow, I got this code.. This calculates the result in meters, not in miles :)
public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
double earthRadius = 6371000; //meters
double dLat = Math.toRadians(lat2-lat1);
double dLng = 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(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
float dist = (float) (earthRadius * c);
return dist;
}
这篇关于当您在java中知道经度和纬度时,计算以米为单位的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!