Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GeoLocation
- {
- public static final double RADIUS = 3963.1676;
- public static final double KM_PER_MILE = 1.60934;
- private double latitude;
- private double longitude;
- public GeoLocation(double theLatitude, double theLongitude)
- {
- latitude = theLatitude;
- longitude = theLongitude;
- }
- public double getLatitude()
- {
- return latitude;
- }
- public double getLongitude()
- {
- return longitude;
- }
- public String toString()
- {
- return "latitude: " + latitude + ", longitude: " + longitude;
- }
- public double distanceFrom(GeoLocation other)
- {
- double lat1 = Math.toRadians(latitude);
- double long1 = Math.toRadians(longitude);
- double lat2 = Math.toRadians(other.latitude);
- double long2 = Math.toRadians(other.longitude);
- double theCos = Math.sin(lat1) * Math.sin(lat2) +
- Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2);
- double arcLength = Math.acos(theCos);
- double result = arcLength * RADIUS;
- return result;
- }
- public double distanceFromInKilometers(GeoLocation other)
- {
- double lat1 = Math.toRadians(latitude);
- double long1 = Math.toRadians(longitude);
- double lat2 = Math.toRadians(other.latitude);
- double long2 = Math.toRadians(other.longitude);
- double theCos = Math.sin(lat1) * Math.sin(lat2) +
- Math.cos(lat1) * Math.cos(lat2) * Math.cos(long1 - long2);
- double arcLength = Math.acos(theCos);
- double result = arcLength * RADIUS;
- double result2 = result * KM_PER_MILE;
- return result2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement