Advertisement
thibthibaut

Coordinates

Nov 24th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.13 KB | None | 0 0
  1. /** Coordinates Class
  2.  * v 1.0
  3.  * @author Thibaut Vercueil
  4.  *
  5.  */
  6.  
  7.  
  8. public class Coordinates {
  9.     private double lat;
  10.     private double lng;
  11.    
  12.     /**
  13.      * Constructor
  14.      * @param lat
  15.      * @param lng
  16.      */
  17.     public Coordinates(double lat, double lng){
  18.         this.lat = lat;
  19.         this.lng = lng;
  20.     }
  21.    
  22.     /**
  23.      * Getter
  24.      * @return current latitude
  25.      */
  26.     public double getLat(){
  27.         return this.lat;
  28.     }
  29.    
  30.     /**
  31.      * Getter
  32.      * @return current longitude
  33.      */
  34.     public double getLng(){
  35.         return this.lng;
  36.     }
  37.    
  38.     /**
  39.      * Setter
  40.      * @param l latitude
  41.      */
  42.     public void setLat(double l){
  43.         this.lat = l;
  44.     }
  45.  
  46.    
  47.     /**
  48.      * Setter
  49.      * @param l longitude
  50.      */
  51.     public void setLng(double l){
  52.         this.lng = l;
  53.     }
  54.    
  55.     public double getDistance(Coordinates C){
  56.         //Converting into Radian
  57.         double phi1 = (this.lat * Math.PI) / 180;
  58.         double lambda1 = (this.lng * Math.PI) / 180;
  59.        
  60.         double phi2 = (C.getLat() * Math.PI) / 180;
  61.         double lambda2 = (C.getLng() * Math.PI) / 180;
  62.        
  63.         //Applying the magic formula
  64.         return Math.acos(Math.sin(phi1)*Math.sin(phi2) + Math.cos(phi1)*Math.cos(phi2)*Math.cos(lambda2 - lambda1))*6371000;
  65.        
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement