Last active
August 29, 2015 14:02
-
-
Save rankun203/03d24ccac5582b5d762f to your computer and use it in GitHub Desktop.
The distance between "Chengdu Vocational Technical College" and "Royal Garden"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created on 14-6-15 | |
* | |
* output: | |
* 2.787433034459691 Miles | |
* 4.485938629409498 Kilometers | |
* 2.4206068471247955 Nautical Miles | |
* | |
* Original post by dommer on StackOverflow: http://stackoverflow.com/a/3694410/1904043 | |
* | |
* @author [email protected] | |
*/ | |
public class GeoTest { | |
private 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 double deg2rad(double deg) { | |
return (deg * Math.PI / 180.0); | |
} | |
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ | |
/*:: This function converts radians to decimal degrees :*/ | |
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/ | |
private double rad2deg(double rad) { | |
return (rad * 180.0 / Math.PI); | |
} | |
public static void main(String[] args) { | |
GeoTest geo = new GeoTest(); | |
System.out.println(geo.distance(30.6313222, 104.053067, 30.5914415,104.0601581, 'M') + " Miles\n"); | |
System.out.println(geo.distance(30.6313222, 104.053067, 30.5914415,104.0601581, 'K') + " Kilometers\n"); | |
System.out.println(geo.distance(30.6313222, 104.053067, 30.5914415,104.0601581, 'N') + " Nautical Miles\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment