-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItinerary.java
More file actions
106 lines (80 loc) · 3.55 KB
/
Copy pathItinerary.java
File metadata and controls
106 lines (80 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import com.google.maps.GeoApiContext;
import com.google.maps.GeocodingApi;
import com.google.maps.model.GeocodingResult;
import java.lang.Math;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.*;
public class Itinerary {
static String metric;
public static void main(String[] args) {
ArrayList<String> cities = new ArrayList<String>();
ArrayList<Double> distances = new ArrayList<Double>();
//Have to change filepath to specific text filepath
String fileName = "/Users/sampathduddu/Desktop/prism/Prism/cities.txt";
String line = null;
//Reading from file and appending to local List
try {
FileReader fileReader =
new FileReader(fileName);
BufferedReader bufferedReader =
new BufferedReader(fileReader);
while((line = bufferedReader.readLine()) != null) {
cities.add(line);
}
bufferedReader.close();
}
catch(FileNotFoundException ex) {
System.out.println(
"Unable to open file '" +
fileName + "'");
}
catch(IOException ex) {
System.out.println(
"Error reading file '"
+ fileName + "'");
}
//Asking if user wants information in miles or kilometers
System.out.println("miles or kilometers?: ");
Scanner scan = new Scanner(System.in);
String metric = scan.next();
scan.close();
//Using Google Geocoder API to retrieve latLon of specific cities and calculating distance between those cities
GeoApiContext context = new GeoApiContext().setApiKey("AIzaSyAM2sw09S1GTm5Zdi4tIvmpuuQ4LBOVnFY");
for (int i = 0; i < cities.size() - 1; i++) {
try {
GeocodingResult[] from = GeocodingApi.geocode(context, cities.get(i)).await();
GeocodingResult[] to = GeocodingApi.geocode(context, cities.get(i+1)).await();
double distance = calcDistance(from,to,metric);
distances.add(distance);
} catch (Exception e) {
System.out.println(e);
}
}
//Printing out distances and total distance
double totalDistance = 0;
for (int i = 0; i < distances.size() ; i++) {
System.out.println(cities.get(i) + " -> " + cities.get(i+1) + ": " + distances.get(i) + " " + metric);
totalDistance += distances.get(i);
}
System.out.println("Total distance covered in your trip: " + totalDistance + metric);
}
//Adapted from Stack Overflow
public static int calcDistance( GeocodingResult[] from, GeocodingResult[] to, String metric) {
double latA = from[0].geometry.location.lat;
double longA = from[0].geometry.location.lng;
double latB = to[0].geometry.location.lat;
double longB = to[0].geometry.location.lng;
double theDistance = (Math.sin(Math.toRadians(latA)) *
Math.sin(Math.toRadians(latB)) +
Math.cos(Math.toRadians(latA)) *
Math.cos(Math.toRadians(latB)) *
Math.cos(Math.toRadians(longA - longB)));
if (metric.charAt(0) == 'm') {
return new Double((Math.toDegrees(Math.acos(theDistance))) * 69.09).intValue();
}
return new Double((Math.toDegrees(Math.acos(theDistance))) * 69.09 * 1.6093).intValue();
}
}