Skip to content

Instantly share code, notes, and snippets.

@FagnerMartinsBrack
Last active November 4, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FagnerMartinsBrack/ae0163b6fac48d65a928 to your computer and use it in GitHub Desktop.
Save FagnerMartinsBrack/ae0163b6fac48d65a928 to your computer and use it in GitHub Desktop.
EarthLocation class
/**
* @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