我正在创建一个项目,我正在使用Google direction API
来计算多个纬度和经度之间的距离。但这显示不正确。我想用Java
计算多个纬度和经度之间的距离。
这是我的java代码
private static double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private static double rad2deg(double rad) {
return (rad * 180 / Math.PI);
}
答案 0 :(得分:0)
要计算2点之间的距离,您需要使用Haversine formula。
在java中:
public class Haversine {
public static final double R = 6372.8; // In kilometers
// calculate distance with haversine formula
public static double haversine(double lat1, double lon1, double lat2, double lon2) {
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.asin(Math.sqrt(a));
return R * c;
}
// example how to use
public static void main(String[] args) {
System.out.println(haversine(36.12, -86.67, 33.94, -118.40));
}
}
答案 1 :(得分:0)
请尝试以下代码:
private static double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
} else if (unit == 'N') {
dist = dist * 0.8684;
}
return (dist);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private static double rad2deg(double rad) {
return (rad * 180 / Math.PI);
}
答案 2 :(得分:0)
这不完全是荷兰语,但我认为不难理解:
包nl.abz.testjaap;
public class Locatie {
private static final double DOORSNEDE_AARDE = 40000.0;
public final double latitude;
public final double longitude;
public Locatie(double latitude, double longitude) throws IllegalArgumentException {
if (latitude > 90 || latitude < -90) {
throw new IllegalArgumentException("latitude moet tussen - 90 en 90 liggen, was "
+ latitude);
}
if (longitude < -360 || longitude > 360) {
throw new IllegalArgumentException("Longitude moet tussen -360 en 360 liggen was "
+ longitude);
}
this.latitude = latitude;
this.longitude = longitude;
}
/**
* Calculate the distance in KM
*
* @param l2
* @return
*/
public double afstandTot(Locatie l2) {
double lon1 = longitude / 180 * Math.PI;
double lon2 = l2.longitude / 180 * Math.PI;
double lat1 = latitude / 180 * Math.PI;
double lat2 = l2.latitude / 180 * Math.PI;
double x1 = Math.cos(lon1) * Math.cos(lat1);
double y1 = Math.sin(lon1) * Math.cos(lat1);
double z1 = Math.sin(lat1);
double x2 = Math.cos(lon2) * Math.cos(lat2);
double y2 = Math.sin(lon2) * Math.cos(lat2);
double z2 = Math.sin(lat2);
double psi = Math.acos(x1 * x2 + y1 * y2 + z1 * z2);
double afstand = DOORSNEDE_AARDE / (2 * Math.PI) * psi;
return afstand;
}
}