-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleMapsGeocoder.java
More file actions
162 lines (119 loc) · 5.19 KB
/
GoogleMapsGeocoder.java
File metadata and controls
162 lines (119 loc) · 5.19 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package cas2xb3.greenlight;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.*;
/**
*
* This class is used to geocode a location. It allows us to retrieve the coordinates of an input location, like the coordinates of Toronto Ontario.
* This class makes use of several files located in the org.json package. The code located in the org.json package was retrieved from https://github.com/stleary/JSON-java.
* The methods contained within this class reads from a JSON output and retrieves the coordinates of a location. It includes methods which return the coordinates, along with
* the latitude and longitude independently.
*
*/
public class GoogleMapsGeocoder {
private static JSONObject json;
private static double longitude;
private static double latitude;
private static String lng;
private static String lat;
private static String ReplacementAddress;
/**
* This method reads the content of a file and transitions the contents into a string.
* The framework for this piece of code was referred from http://stackoverflow.com/a/4308662/7454970.
*
* @param br - This represents a BufferedReader that reads the content of a file.
* @return This method returns a string holding all the contents of a file.
* @throws IOException
*/
private static String readAll(BufferedReader br) throws IOException {
StringBuilder contents = new StringBuilder();
int i;
// Moving the contents which is being read into the string builder.
while ((i = br.read()) != -1) {
contents.append((char) i);
}
// Returning the string with all the contents from the file.
return contents.toString();
}
/**
* This method reads JSON code from a url. It moves the contents from the url into a string, and then moves the string into a JSONObject.
* The JSONObject is then returned.
* The framework for this piece of code was referred from http://stackoverflow.com/a/4308662/7454970.
*
* @param url - This parameter represents a url which contains JSON code.
* @return This method returns a JSONObject containing all the JSON code retrieved from the url.
* @throws IOException
* @throws JSONException
*/
public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
// Reading the contents from a url, moving it to a string, and then into a JSONObject
BufferedReader br = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(br);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
/**
* This method is used to retrieve the latitude and longitude of an input address from a url generated by the Google Maps Geocoding API.
* It then returns a string which includes the coordinates of the location.
* This method made use of the Google Maps Geocoding API, primarily the url, at https://developers.google.com/maps/documentation/geocoding/intro.
*
* @param address - This represents an address which is to be geocoded.
* @return A string of form latitude , longnitude is returned, containing the coordinates of an object.
* @throws JSONException
* @throws IOException
*/
public static String getCoordinates(String address) throws JSONException, IOException {
// Spaces cause errors with the url, if the input contains spaces they will be removed.
if (address.contains(" ")) {
for (int i = 0; i < address.length(); i++) {
ReplacementAddress = address.replace(" ", "");
}
json = readJsonFromUrl("https://maps.googleapis.com/maps/api/geocode/json?address=" + ReplacementAddress
+ "&key=AIzaSyAgOPBEIzwBTQmWbZHHApZfqi8sEbSmKy8");
} else {
json = readJsonFromUrl("https://maps.googleapis.com/maps/api/geocode/json?address=" + address
+ "&key=AIzaSyAgOPBEIzwBTQmWbZHHApZfqi8sEbSmKy8");
}
String name = json.getJSONArray("results").toString();
// Splitting the string with the JSON output in order to isolate the coordinates.
String[] test = name.split(",|}|:");
// Extracting the coordinates by searching for where they are located
for (int i = 0; i < test.length; i++) {
if (test[i].equals('"' + "location" + '"')) {
// Longitude is located 2 indexes away from "location"
lng = test[i + 2];
// Latitude is located 4 indexes away from "location"
lat = test[i + 4];
}
}
// Parsing the coordinates into a double so it could be used properly in other classes.
longitude = Double.parseDouble(lng);
latitude = Double.parseDouble(lat);
return (getLatitude() + " , " + getlongitude());
}
/**
* This method acts as a getter for the latitude.
*
* @return The latitude of a location is returned.
*/
public static double getLatitude() {
return latitude;
}
/**
* This method acts as a getter for the longitude.
*
* @return The longitude of a location is returned.
*/
public static double getlongitude() {
return longitude;
}
}