-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapFrame.java
430 lines (355 loc) · 15.4 KB
/
MapFrame.java
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
public class MapFrame extends javax.swing.JFrame {
private URL url;
private ImageIcon icon;
private String source;
private String destination;
private ArrayList<String> waypoints;
private double lat;
private double lon;
private int zoom;
private double[][] pollutionMatrix;
private String polyLine;
/**
* Creates new form NewJFrame
*
* @param source
* @param destination
* @param waypoints
*/
public MapFrame(String source, String destination, ArrayList<String> waypoints) {
initComponents();
this.source = source.replace(" ", "+");
this.destination = destination.replace(" ", "+");
this.waypoints = new ArrayList<String>();
for (String s : waypoints) {
String aux = s.replace(" ", "+");
this.waypoints.add(aux);
}
this.polyLine = getPolyLine();
createMap();
/* try {
this.pollutionMatrix = createMatrix();
} catch (IOException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}*/
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setPreferredSize(new java.awt.Dimension(760, 760));
setResizable(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(51, 51, 51)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 1131, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(124, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 726, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(47, Short.MAX_VALUE))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private double[][] createMatrix() throws IOException {
double minLat = 44.36476;
double maxLat = 44.523937;
double minLon = 25.971842;
double maxLon = 26.249818;
double lonRatio = (maxLon - minLon) / 6;
double latRatio = (maxLat - minLat) / 6;
double[][] matrix = new double[31][31];
double[][] finMatrix = new double[31][31];
URL resource = null;
BufferedReader in = null;
StringBuilder response;
for (int i = 0; i < 31; i += 5) {
double latCoord = minLat + i / 5 * latRatio;
for (int j = 0; j < 31; j += 5) {
double lonCoord = minLon + j / 5 * lonRatio;
try {
resource = new URL("https://api.breezometer.com/baqi/?lat=" + latCoord + "&lon=" + lonCoord + "&key=141bc4abd9ec4901b9619d9b34325e81 ");
} catch (MalformedURLException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
HttpURLConnection conBreeze = null;
try {
conBreeze = (HttpURLConnection) resource.openConnection();
} catch (IOException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
try {
conBreeze.setRequestMethod("GET");
} catch (ProtocolException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
try {
in = new BufferedReader(new InputStreamReader(conBreeze.getInputStream()));
} catch (IOException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
response = new StringBuilder();
String inputLine;
try {
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
} catch (IOException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
in.close();
String[] tokensB = new String[3];
tokensB = response.toString().split("\"breezometer_aqi\":");
String quality = tokensB[1].split(",")[0].replaceAll("\\s", "");
matrix[i][j] = Integer.parseInt(quality);
}
}
for (int i = 0; i < 26; i += 5) {
for (int j = 0; j < 26; j += 5) {
for (int k = 1; k < 5; k++) {
for (int l = 1; l < 5; l++) {
matrix[i + k][j + l] = matrix[i][j] * (5 - k) * (5 - l) / 25
+ matrix[i + 5][j] * l * (5 - k) / 25
+ matrix[i][j + 5] * (5 - l) * k / 25
+ matrix[i + 5][j + 5] * (k * l) / 25;
}
matrix[i + k][j] = matrix[i][j] * (5 - k) / 5 + matrix[i + 5][j] * k / 5;
matrix[i][j + k] = matrix[i][j] * (5 - k) / 5 + matrix[i][j + 5] * k / 5;
}
}
}
for (int i = 0; i < 26; i += 5) {
for (int l = 1; l < 5; l++) {
matrix[30][i + l] = matrix[30][i] * (5 - l) / 5 + matrix[30][i + 5] * l / 5;
matrix[i + l][30] = matrix[i][30] * (5 - l) / 5 + matrix[i + 5][30] * l / 5;
}
}
for (int i = 0; i < 31; i++) {
finMatrix[i] = matrix[30 - i];
}
return finMatrix;
}
private String[] getCoord(String location) throws IOException {
String transf = location.replace(" ", "+");
URL url1 = null;
try {
url1 = new URL("https://maps.googleapis.com/maps/api/geocode/json"
+ "?address=" + transf + "&key=AIzaSyDQKLqlmunVm8LGlZIiGsDlFZGcMgJ_4wI");
} catch (MalformedURLException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
HttpURLConnection con = null;
try {
con = (HttpURLConnection) url1.openConnection();
} catch (IOException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
try {
con.setRequestMethod("GET");
} catch (ProtocolException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
} catch (IOException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
String inputLine;
StringBuilder response = new StringBuilder();
try {
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
} catch (IOException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
if (in != null) {
in.close();
}
String[] tokens = new String[3];
tokens = response.toString().split("location");
String locLat, locLon;
try {
String[] tokens2 = tokens[1].split(":");
locLat = tokens2[2].split(",")[0].replaceAll("\\s", "");
locLon = tokens2[3].split("}")[0].replaceAll("\\s", "");
return new String[]{locLat, locLon};
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Invalid location(s)");
}
return null;
}
private String getPolyLine() {
String polyline = null;
String req = "https://maps.googleapis.com/maps/api/directions/json?"
+ "origin=" + source + "&destination=" + destination;
if (waypoints.isEmpty() == false) {
req += "&waypoints=";
for (int i = 0; i < waypoints.size(); i++) {
req += waypoints.get(i);
if (i < waypoints.size() - 1) {
req += "|";
}
}
}
req += "&key=AIzaSyAxOzdyQ21cKWSywxCDDVZ7TZljwnAAq5Q";
try {
URL request = new URL(req);
HttpURLConnection con = (HttpURLConnection) request.openConnection();
con.setRequestMethod("GET");
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
} catch (IOException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
String inputLine;
StringBuilder response = new StringBuilder();
try {
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
System.out.println(inputLine);
}
} catch (IOException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
if (in != null) {
in.close();
}
System.out.println(req);
String str = response.toString().split("\"overview_polyline\"")[1];
String str2 = str.split("\"points\"")[1];
polyline = str2.split(" ")[2].split("\"")[1];
} catch (MalformedURLException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(MapFrame.class.getName()).log(Level.SEVERE, null, ex);
}
return polyline;
}
private void createMap() {
String[] sourceCoord = null, destCoord = null;
ArrayList<String[]> waypointsCoord = new ArrayList<String[]>();
try {
sourceCoord = getCoord(source);
destCoord = getCoord(destination);
for (String s : waypoints) {
waypointsCoord.add(getCoord(s));
}
} catch (IOException ex) {
Logger.getLogger(MapFrame.class
.getName()).log(Level.SEVERE, null, ex);
}
String markers;
int no;
double maxLat = Double.MIN_VALUE;
double maxLon = Double.MIN_VALUE;
double minLat = Double.MAX_VALUE;
double minLon = Double.MAX_VALUE;
if (sourceCoord != null && destCoord != null) {
markers = "&markers=color:red%7Clabel:S%7C" + sourceCoord[0] + "," + sourceCoord[1]
+ "&markers=color:red%7Clabel:D%7C" + destCoord[0] + "," + destCoord[1];
lat = Double.parseDouble(sourceCoord[0]) + Double.parseDouble(destCoord[0]);
lon = Double.parseDouble(sourceCoord[1]) + Double.parseDouble(destCoord[1]);
no = 2;
if (maxLat < Double.parseDouble(sourceCoord[0])) {
maxLat = Double.parseDouble(sourceCoord[0]);
}
if (maxLon < Double.parseDouble(sourceCoord[1])) {
maxLon = Double.parseDouble(sourceCoord[1]);
}
if (maxLat < Double.parseDouble(destCoord[0])) {
maxLat = Double.parseDouble(destCoord[0]);
}
if (maxLon < Double.parseDouble(destCoord[1])) {
maxLon = Double.parseDouble(destCoord[1]);
}
if (minLat > Double.parseDouble(sourceCoord[0])) {
minLat = Double.parseDouble(sourceCoord[0]);
}
if (minLon > Double.parseDouble(sourceCoord[1])) {
minLon = Double.parseDouble(sourceCoord[1]);
}
if (minLat > Double.parseDouble(destCoord[0])) {
minLat = Double.parseDouble(destCoord[0]);
}
if (minLon > Double.parseDouble(destCoord[1])) {
minLon = Double.parseDouble(destCoord[1]);
}
for (int i = 0; i < waypointsCoord.size(); i++) {
markers += "&markers=color:red%7Clabel:W%7C" + waypointsCoord.get(i)[0] + "," + waypointsCoord.get(i)[1];
lat += Double.parseDouble(waypointsCoord.get(i)[0]);
lon += Double.parseDouble(waypointsCoord.get(i)[1]);
no++;
if (maxLat < Double.parseDouble(waypointsCoord.get(i)[0])) {
maxLat = Double.parseDouble(waypointsCoord.get(i)[0]);
}
if (maxLon < Double.parseDouble(waypointsCoord.get(i)[1])) {
maxLon = Double.parseDouble(waypointsCoord.get(i)[1]);
}
if (minLat > Double.parseDouble(waypointsCoord.get(i)[0])) {
minLat = Double.parseDouble(waypointsCoord.get(i)[0]);
}
if (minLon > Double.parseDouble(waypointsCoord.get(i)[1])) {
minLon = Double.parseDouble(waypointsCoord.get(i)[1]);
}
}
lat /= no;
lon /= no;
if (maxLat - minLat >= 0.05 || maxLon - minLon >= 0.05) {
zoom = 12;
} else if (maxLat - minLat >= 0.01 || maxLon - minLon >= 0.01) {
zoom = 13;
} else {
zoom = 14;
}
try {
url = new URL("https://maps.googleapis.com/maps/api/staticmap?"
+ "center=" + lat + "," + lon + "&"
+ "size=700x700&maptype=roadmap&"
+ "zoom=" + zoom + "&" + markers
+ "&path=weight:3%7color:blue%7Cenc:" + polyLine
+ "&key=AIzaSyBPdzxz3LQzNkM5u3Fcn-4wvdxOWFEDK9g");
} catch (MalformedURLException ex) {
Logger.getLogger(MapFrame.class
.getName()).log(Level.SEVERE, null, ex);
}
try {
icon = new ImageIcon(ImageIO.read(url));
} catch (IOException ex) {
Logger.getLogger(MapFrame.class
.getName()).log(Level.SEVERE, null, ex);
}
jLabel1.setIcon(icon);
}
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JLabel jLabel1;
// End of variables declaration//GEN-END:variables
}