View Javadoc
1   package org.kuali.mobility.bus.service.util;
2   
3   import org.kuali.mobility.bus.entity.BusStop;
4   
5   public class BusStopDistanceUtil
6   {	
7   	private static double earthRadius = 6371; // km
8   
9   	public BusStopDistanceUtil(){
10  		
11  	}
12  
13  	/*calculate each busstop distance based on lat,lon */
14  	public double calculateDistance(BusStop s,double lat1,double lon1) {
15  			double pk = 180/3.14159;
16  	
17  		    double a1 = Double.parseDouble(s.getLatitude()) / pk;
18  		    double a2 = Double.parseDouble(s.getLongitude()) / pk;
19  		    double b1 = lat1 / pk;
20  		    double b2 = lon1 / pk;
21  	
22  		    double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2);
23  		    double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2);
24  		    double t3 = Math.sin(a1) * Math.sin(b1);
25  		    double tt = Math.acos(t1 + t2 + t3);
26  	
27  		    return 6366000*tt;
28  		    /*
29  		    double lat2 = Double.parseDouble(s.getLatitude());
30  			double lon2 = Double.parseDouble(s.getLongitude());
31  			double dlon = Math.toRadians(lon2 - lon1);
32  			double dlat = Math.toRadians(lat2 - lat1);
33  			double a = (Math.sin(dlat / 2)) * (Math.sin(dlat / 2))
34  					+ (Math.sin(dlon / 2)) * Math.sin(dlon / 2)
35  					* Math.cos(lat1) * Math.cos(lat2);
36  			double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
37  			double dist = earthRadius * c;
38  		    return dist;
39  		    */
40  	}
41  
42  }