added timeEstimate and distanceBetweenPoints functions
This commit is contained in:
		
							parent
							
								
									27d69cecd5
								
							
						
					
					
						commit
						3cfdcacfd6
					
				
					 1 changed files with 60 additions and 4 deletions
				
			
		
							
								
								
									
										64
									
								
								Route.java
									
									
									
									
									
								
							
							
						
						
									
										64
									
								
								Route.java
									
									
									
									
									
								
							|  | @ -1,11 +1,11 @@ | |||
| public class Route { | ||||
|     private double[] currentLocation; //Lat-Long coordinates for current location | ||||
|     private double[][][] routeSequences; //3D array of lat-long pairs + speed that form a route sequence, with multiple sequences stored | ||||
|     private double[][][] routeSequences; //3D array for routes. 1st dim selects route, 2nd dim selects route point, 3rd dim selects lat/long/speed/tolls (indices 0-2) | ||||
|     private int routeSelected; | ||||
|     private boolean tollsFlag; //stores user setting for whether to avoid tolls | ||||
|     private boolean hwyFlag; //stores user preference for whether to avoid major highways | ||||
|     public long timeEstimate; //time estimate in seconds | ||||
|     public float tollsAmount; //stores toll amount if toll roads are used | ||||
|     public double[] timeEstimate; //time estimate in seconds | ||||
|     public float[] tollsAmount; //stores toll amount if toll roads are used | ||||
|     public Destination currentDestination; //uses user-created class Destination | ||||
| 
 | ||||
|     private void updateCurrentLocation() { | ||||
|  | @ -21,11 +21,67 @@ public class Route { | |||
|         updateCurrentLocation(); | ||||
|         HelperFunctions apiCallToRouting = new HelperFunctions(); | ||||
|         routeSequences[0] = apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag); //route option 1 | ||||
|         timeEstimate[0] = estimateTime(routeSequences[0]); | ||||
|         estimateTolls(routeSequences[0]); | ||||
|         routeSequences[1] = apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag); //route option 2 | ||||
|         timeEstimate[1] = estimateTime(routeSequences[1]); | ||||
|         estimateTolls(routeSequences[1]); | ||||
|         routeSequences[2] = apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag); //route option 3 | ||||
|         timeEstimate[2] = estimateTime(routeSequences[1]); | ||||
|         estimateTolls(routeSequences[1]); | ||||
|     } | ||||
| 
 | ||||
|     public void selectRoute() { | ||||
|     public int enumRoutes() { | ||||
|         return routeSequences.length; | ||||
|     } | ||||
| 
 | ||||
|     public void selectRoute(int userSelection) { | ||||
|         routeSelected = userSelection; | ||||
|     } | ||||
| 
 | ||||
|     public void estimateTolls(double[][] route) { | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
|     public double estimateTime(double[][] route) { | ||||
|         //calculates distances between route segments, divided by speed in MPH | ||||
|         //the API would return expected speed due to traffic conditions as part of the speed (index 2) in the route array | ||||
|         double runningTime = 0; | ||||
|         //steps through subsequence route points to get distances, calculates time based on speed estimate, and accumulates on runningTime | ||||
|         for (int i = 0; i < route.length; i++) {  | ||||
|             runningTime += distanceBetweenPoints(route[i], route[i+1]) / route[i][2]; | ||||
|         } | ||||
|         return runningTime; | ||||
|     } | ||||
| 
 | ||||
|     private double distanceBetweenPoints(double[] pointA, double[] pointB) { | ||||
|         /** | ||||
|          * Code excerpt by StackOverflow user Dommer, adapted for this application's use | ||||
|          * https://stackoverflow.com/questions/3694380/calculating-distance-between-two-points-using-latitude-longitude/3694410#3694410 | ||||
|          *  | ||||
|          * Calculate distance between two points in latitude and longitude taking | ||||
|          * into account height difference. If you are not interested in height | ||||
|          * difference pass 0.0. Uses Haversine method as its base. | ||||
|          *  | ||||
|          * lat1, lon1 Start point lat2, lon2 End point el1 Start altitude in miles | ||||
|          * el2 End altitude in miles | ||||
|          * @returns Distance | ||||
|          */ | ||||
|         final int R = 6371; // Radius of the earth | ||||
|         int el1 = 0, el2 = 0; | ||||
| 
 | ||||
|         double latDistance = Math.toRadians(pointA[0] - pointB[0]); | ||||
|         double lonDistance = Math.toRadians(pointA[1] - pointB[1]); | ||||
|         double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) | ||||
|             + Math.cos(Math.toRadians(pointA[0])) * Math.cos(Math.toRadians(pointB[0])) | ||||
|             * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2); | ||||
|         double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | ||||
|         double distance = R * c * 0.6213711922; // convert to miles | ||||
| 
 | ||||
|         double height = el1 - el2; | ||||
| 
 | ||||
|         distance = Math.pow(distance, 2) + Math.pow(height, 2); | ||||
| 
 | ||||
|         return Math.sqrt(distance); | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in a new issue