Last active
January 6, 2021 00:37
-
-
Save danielgomezrico/992c2f35d04a42a99ea5 to your computer and use it in GitHub Desktop.
Revisions
-
danielgomezrico revised this gist
Jun 21, 2016 . 1 changed file with 18 additions and 9 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,30 +1,39 @@ import static java.lang.Math.acos; import static java.lang.Math.cos; import static java.lang.Math.sin; /** * Calculate distance between coordinates. */ public class DistanceCalculator { static double PI_RAD = Math.PI / 180.0; /** * Use Great Circle distance formula to calculate distance between 2 coordinates in meters. */ public double greatCircleInFeet(LatLng latLng1, LatLng latLng2) { return greatCircleInKilometers(latLng1.latitude, latLng1.longitude, latLng2.latitude, latLng2.longitude) * 3280.84; } /** * Use Great Circle distance formula to calculate distance between 2 coordinates in meters. */ public double greatCircleInMeters(LatLng latLng1, LatLng latLng2) { return greatCircleInKilometers(latLng1.latitude, latLng1.longitude, latLng2.latitude, latLng2.longitude) * 1000; } /** * Use Great Circle distance formula to calculate distance between 2 coordinates in kilometers. * https://software.intel.com/en-us/blogs/2012/11/25/calculating-geographic-distances-in-location-aware-apps */ public double greatCircleInKilometers(double lat1, double long1, double lat2, double long2) { double phi1 = lat1 * PI_RAD; double phi2 = lat2 * PI_RAD; double lam1 = long1 * PI_RAD; double lam2 = long2 * PI_RAD; return 6371.01 * acos(sin(phi1) * sin(phi2) + cos(phi1) * cos(phi2) * cos(lam2 - lam1)); } } -
danielgomezrico created this gist
Nov 19, 2015 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ /** * Calculate distance between coordinates. */ public class DistanceCalculator { static double PI_RAD = Math.PI / 180.0; /** * Use Great Circle distance formula to calculate distance between 2 coordinates in meters. */ public double greatCircleInMeters(double lat1, double long1, double lat2, double long2) { return greatCircleInKilometers(lat1, long1, lat2, long2) * 1000; } /** * Use Great Circle distance formula to calculate distance between 2 coordinates in kilometers. * https://software.intel.com/en-us/blogs/2012/11/25/calculating-geographic-distances-in-location-aware-apps */ public double greatCircleInKilometers(double lat1, double long1, double lat2, double long2) { double phi1 = lat1 * PI_RAD; double phi2 = lat2 * PI_RAD; double lam1 = long1 * PI_RAD; double lam2 = long2 * PI_RAD; return 6371.01 * Math.acos( Math.sin(phi1) * Math.sin(phi2) + Math.cos(phi1) * Math.cos(phi2) * Math.cos(lam2 - lam1)); } }