-
Notifications
You must be signed in to change notification settings - Fork 1
/
gmaps.js
331 lines (295 loc) · 10 KB
/
gmaps.js
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
var TILE_SIZE = 256;
var tileCoordinate, zoomLevel, currentLatLng;
var marker;
var coordInfo;
var aqiLevel;
var waterLevel;
var weatherApiKey;
init();
// TODO : save map state on reaload, using API maybe
// TODO : show model analysis data
function init() {
loadMap();
getAndUpdateAnalysisResult();
loadWeatherData();
// link generateModel button with the API call. If you directly call generateModel(),
// then it will call it infinitely (every time the page refreshes)
generateModelButton = document.getElementById('generateModelsButton');
generateModelButton.onclick = generateModel;
placeObjectOnMapButton = document.getElementById('placeStuffButton');
placeObjectOnMapButton.onclick = placeObjectOnMapRedirect;
coordInfo = document.getElementById('coordInfo');
aqiLevel = document.getElementById('aqi-index');
aqiBox = document.getElementById('aqi');
waterLevel = document.getElementById('water-index');
waterBox = document.getElementById('water-table');
}
function placeObjectOnMapRedirect() {
if (currentLatLng==undefined){
window.open(`placeObjects.html`);
}
else{
window.open(`placeObjects.html?lat=${currentLatLng.lat()}&lng=${currentLatLng.lng()}&zoom=${zoomLevel}`);
}
}
async function loadWeatherData() {
const response = await fetch('http://127.0.0.1:5000/getWeatherApiKey', {
mode: 'cors'
});
weatherApiKey = await response.json();
/*global jQuery, Highcharts, OWM , from loader.js */
(loadWeatherData(jQuery, OWM));
}
async function getPreviousPos() {
const response = await fetch(`http://127.0.0.1:5000/getLastPosition`, {
mode: 'cors'
});
const json = await response.json();
console.log(json);
if (json == 'False') {
return false;
} else {
var lat = parseFloat(json['lat']);
var long = parseFloat(json['long']);
console.log(`LATLONG ${lat}, ${long}`);
return json;
// return [lat, long]
// currentLatLng = new google.maps.LatLng(lat, long)
//add marker to position
}
}
async function getAndUpdateAnalysisResult() {
const response = await fetch(`http://127.0.0.1:5000/getAnalysisResult`, {
mode: 'cors'
});
const json = await response.json();
// return json;
if (json != 'False') {
var result = document.getElementById('analysisResult');
var pbar = document.getElementById('pbar');
var buildingTopArea = json['FlatSurfaceArea'];
var totalArea = json['TotalArea'];
result.innerHTML = `Analysis result:<br>BuildingTop area : ${buildingTopArea}, Total area : ${totalArea}`;
var percent = (buildingTopArea/totalArea)*100;
pbar.style = `width : ${percent}%`
}
}
async function generateModel() {
var tilex = tileCoordinate.x;
var tiley = tileCoordinate.y;
console.log(`Current location ${tileCoordinate}, ${zoomLevel}`);
// console.log(tileCoordinate);
console.log(zoomLevel);
if (zoomLevel < 15) {
console.log(
'Terrain export is only available over zoom 15. Please zoom in more!'
);
} else {
console.log('Calling API..');
const response = await fetch(
`http://127.0.0.1:5000/run/${tilex}/${tiley}/${zoomLevel}/${currentLatLng.lat()}/${currentLatLng.lng()}`,
{ mode: 'cors' }
);
const myJson = await response.json();
console.log(myJson);
}
}
async function loadMap() {
console.log('Getting key..');
const response = await fetch('http://127.0.0.1:5000/getApiKey', {
mode: 'cors'
});
const key = await response.json();
console.log(key);
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = `https://maps.googleapis.com/maps/api/js?key=${key}&callback=initMap`;
document.body.appendChild(script);
}
async function initMap() {
var pos, map;
var prev = await getPreviousPos();
if (prev == false) {
pos = new google.maps.LatLng(1.334, 103.847); // Singapore
map = new google.maps.Map(document.getElementById('map'), {
center: pos,
zoom: 3,
gestureHandling: 'greedy'
});
tileCoordinate = getTileCoord(pos);
} else {
pos = new google.maps.LatLng(prev['lat'], prev['long']);
currentLatLng = pos;
map = new google.maps.Map(document.getElementById('map'), {
center: pos,
zoom: parseInt(prev['zoom']),
gestureHandling: 'greedy'
});
tileCoordinate = new google.maps.Point(
parseInt(prev['tilex']),
parseInt(prev['tiley'])
);
}
marker = new google.maps.Marker({
position: pos,
map: map,
title: 'Drag me around!',
draggable: true,
animation: google.maps.Animation.DROP
});
marker.setMap(map);
zoomLevel = map.getZoom();
console.log(tileCoordinate, zoomLevel);
// if (currentLatLng != undefined) {
// showPositionInfo(
// tileCoordinate.x,
// tileCoordinate.y,
// zoomLevel,
// currentLatLng.lat(),
// currentLatLng.lng()
// );
// }
google.maps.event.addListener(marker, 'mouseup', function() {
updateMarkerLocation(map, marker);
});
map.addListener('zoom_changed', function() {
updateMarkerLocation(map, marker);
});
}
// function showPositionInfo(tilex, tiley, zoom, lat, long) {
// coordInfo.innerHTML = `Latitude ${lat} Longitude ${long}<br>tilex : ${tilex} tiley: ${tiley} zoom: ${zoom}`;
// }
function updateMarkerLocation(map, marker) {
zoomLevel = map.getZoom();
var oldLatLng = currentLatLng;
currentLatLng = marker.getPosition();
tileCoordinate = getTileCoord(currentLatLng, zoomLevel);
console.log(tileCoordinate, zoomLevel);
console.log('Updated! ^');
// showPositionInfo(
// tileCoordinate.x,
// tileCoordinate.y,
// zoomLevel,
// currentLatLng.lat(),
// currentLatLng.lng()
// );
if (zoomLevel < 15) {
generateModelButton.classList.add('disabled');
} else {
generateModelButton.classList.remove('disabled');
}
if (oldLatLng !== currentLatLng) {
water = Math.floor(Math.random() * 300 + 1);
waterLevel.innerHTML = water;
if (water <= 50) {
waterBox.style.backgroundColor = '#FE1603';
} else if (water > 50 && water <= 100) {
waterBox.style.backgroundColor = '#FE7103';
} else if (water > 100 && water <= 150) {
waterBox.style.backgroundColor = '#F7D209';
} else if (water > 150 && water <= 200) {
waterBox.style.backgroundColor = '#08E6F7';
} else if (water > 200 && water <= 300) {
waterBox.style.backgroundColor = '#0DA8E3';
} else {
waterBox.style.backgroundColor = '#0D26E3';
}
axios
.get(
'https://api.waqi.info/feed/geo:' +
currentLatLng.lat() +
';' +
currentLatLng.lng() +
'/?token=' +
AQI_KEY,
null,
null
)
.then(response => {
const data = response.data.data;
aqi = data.aqi;
aqiLevel.innerHTML = aqi;
if (aqi <= 50) {
aqiBox.style.backgroundColor = '#00ff4c';
} else if (aqi > 50 && aqi <= 100) {
aqiBox.style.backgroundColor = '#e1ff00';
} else if (aqi > 100 && aqi <= 150) {
aqiBox.style.backgroundColor = '#ffae00';
} else if (aqi > 150 && aqi <= 200) {
aqiBox.style.backgroundColor = '#ff000d';
} else if (aqi > 200 && aqi <= 300) {
aqiBox.style.backgroundColor = '#850099';
} else {
aqiBox.style.backgroundColor = '#800000';
}
})
.catch(error => console.error('Error in getting AQI', error));
var jugad = Math.floor((Math.random() * 3) + 1);
var suggestions = ''
if(jugad == 1) {
suggestions+="<li>Build more solar panels</li>";
} else if(jugad == 2){
suggestions+="<li>Build more rainwater harvesters</li>";
} else if(jugad == 3){
suggestions+="<li>Build more windmills</li>";
}else if(aqi > 100 || water < 150){
suggestions+="<li>Plant more trees</li>";
}else if(aqi > 100 || water < 150){ //ROHAN CHANGE THIS LINE TO If (Ratio of trees/roads<0.6) == plant more trees
suggestions+="<li>Plant more trees</li>";
}
var sug = document.getElementById("dynamic_sug");
sug.innerHTML = suggestions;
}
}
function getTileCoord(currentLatLng, zoom) {
var scale = 1 << zoom;
var worldCoordinate = project(currentLatLng);
tileCoordinate = new google.maps.Point(
Math.floor((worldCoordinate.x * scale) / TILE_SIZE),
Math.floor((worldCoordinate.y * scale) / TILE_SIZE)
);
return tileCoordinate;
}
// The mapping between latitude, longitude and pixels is defined by the web
// mercator projection.
function project(latLng) {
var siny = Math.sin((latLng.lat() * Math.PI) / 180);
// Truncating to 0.9999 effectively limits latitude to 89.189. This is
// about a third of a tile past the edge of the world tile.
siny = Math.min(Math.max(siny, -0.9999), 0.9999);
return new google.maps.Point(
TILE_SIZE * (0.5 + latLng.lng() / 360),
TILE_SIZE * (0.5 - Math.log((1 + siny) / (1 - siny)) / (4 * Math.PI))
);
}
async function Export() {
//URL of Google Static Maps.
const response = await fetch('http://127.0.0.1:5000/getApiKey', {
mode: 'cors'
});
const key = await response.json();
var staticMapUrl = 'https://maps.googleapis.com/maps/api/staticmap';
//Set the Google Map Center.
staticMapUrl += '?center=' + currentLatLng.lat() + ',' + currentLatLng.lng();
//Set the Google Map Size.
staticMapUrl += '&size=600x900';
//Set the Google Map Zoom.
staticMapUrl += '&zoom=' + zoomLevel;
//Set the Google Map Type.
staticMapUrl += '&maptype=satellite';
//Set the Google Map Type.
staticMapUrl += `&key=${key}`;
//Display the Image of Google Map.
var imgMap = document.getElementById('element-out');
imgMap.src = staticMapUrl;
imgMap.style.display = 'block';
// console.log("BEANS");
// const response = await fetch(`http://127.0.0.1:5000/getImage/${currentLatLng.lat()}/${currentLatLng.lng()}/${zoomLevel}`, {
// mode: 'cors'
// });
// const json = await response.json();
// console.log(json);
// document.getElementById('colorInfo').innerHTML = json
// var a = document.getElementById('dynamic_link');
// a.href = staticMapUrl;
}