Last active
November 4, 2015 13:56
-
-
Save FagnerMartinsBrack/ae0163b6fac48d65a928 to your computer and use it in GitHub Desktop.
EarthLocation class
This file contains 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
/** | |
* @author Fagner Brack | |
*/ | |
public class EarthLocation { | |
private double latitude; | |
private double longitude; | |
private final int EARTH_RADIUS_KM = 6371; | |
public EarthLocation( double latitude, double longitude ) { | |
this.latitude = latitude; | |
this.longitude = longitude; | |
} | |
public double getLatitude() { | |
return latitude; | |
} | |
public double getLongitude() { | |
return longitude; | |
} | |
/** | |
* Recupera a distância em KM entre uma coordenada e outra considerando a circunferência | |
* da Terra.<br> | |
* O resultado é arredondado para baixo. | |
*/ | |
public int getDistanceOf( EarthLocation target ) { | |
double cosine = Math.cos( ( 90 - latitude ) * Math.PI / 180 ); | |
double targetCosine = Math.cos( ( 90 - target.getLatitude() ) * Math.PI / 180 ); | |
double sine = Math.sin( ( 90 - latitude ) * Math.PI / 180 ); | |
double targetSine = Math.sin( ( 90 - target.getLatitude() ) * Math.PI / 180 ); | |
double thisLongitude = ( 360 + longitude ) * Math.PI / 180; | |
double targetLongitude = ( 360 + target.getLongitude() ) * Math.PI / 180; | |
double longitudeAngle = Math.cos( Math.abs( thisLongitude - targetLongitude ) ); | |
double radius = Math.acos( cosine * targetCosine + sine * targetSine * longitudeAngle ); | |
double km = radius * EARTH_RADIUS_KM; | |
return ( int )km; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment