87 lines
4.2 KiB
Java
87 lines
4.2 KiB
Java
public class Route {
|
|
private double[] currentLocation; //Lat-Long coordinates for current location
|
|
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 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() {
|
|
//execute api call out to GPS service to obtain current location
|
|
//current location will be hardcoded for testing
|
|
double[] tmpLoc = {28.1700863880887, -80.67088403224037};
|
|
currentLocation = tmpLoc;
|
|
}
|
|
|
|
private void calculateRoutes() {
|
|
//execute api call out to routing service to get route info
|
|
//ideally a query would go out using multiple pathfiding algorithms to get route options
|
|
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 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);
|
|
}
|
|
}
|