-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
334 lines (297 loc) · 10.3 KB
/
Copy pathindex.html
File metadata and controls
334 lines (297 loc) · 10.3 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
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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Map-dl</title>
<style>
html,
body {
font-family: Roboto, Arial, sans-serif;
height: 100%;
margin: 0;
padding: 0;
}
#map {
height: 100%;
}
#UI {
position: absolute;
user-select: none;
top: 10px;
left: 200px;
z-index: 9;
font-size: 11px;
}
#scaleInput {
padding: 8px 2px;
border: none;
height: 10pt;
margin: 0px;
width: 50px;
}
#coords {
background-color: black; /* fall-back */
background-color: rgba(0, 0, 0, 0.5);
color: white;
padding: 5px;
}
.btn.on, .btn:active {
font-weight: 500;
background: #2196f3;
color: #fff;
}
.btn.on:hover {
background: #1976d2;
color: #fff;
}
.btn {
padding: 8px;
background: #fff;
border-radius: 2px;
display: inline-block;
text-align: center;
vertical-align: middle;
box-shadow: rgba(0, 0, 0, 0.3) 0px 1px 4px -1px;
min-width: 39px;
cursor: pointer;
}
.btn:hover {
background: #eee;
color: #000;
}
.btn.disabled {
background: #ddd;
color: #777;
cursor: default;
}
span.btn {
padding: 0px 8px;
cursor: auto;
}
.hidden {
display: none;
visibility: hidden;
}
progress {
/* reset browser defaults */
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
border: none;
width: 80%;
border: none;
padding: 0;
height: 20px;
top: 26%;
left: 10%;
position: absolute;
/* IE10 */
color: #3572E3;
background-color: #eee;
}
progress::-webkit-progress-bar {
background-color: #eee;
}
progress::-webkit-progress-value {
background-color: #3572E3;
}
progress::-moz-progress-bar {
background-color: #3572E3;
}
</style>
</head>
<body>
<div id="UI">
<div class="btn disabled" id="exportBtn" title="Download all images shown in the grid)">Export All</div>
<div class="btn" id="gridBtn" title="Show grid">Grid</div>
<span class="btn"><input type="number" id="scaleInput" min="1" max="1000" step="1" value="5">Km</span>
</div>
<div id="map"></div>
<div id="coords"></div>
<progress id="progress" class="hidden" min="0" value="0"></progress>
<script>
'use strict';
// var APIKEY = '';
var map;
var mapWorker;
var sizeValue;
var rectArr = [];
var limit = 25;
if (window.Worker) { // Check if Browser supports the Worker api.
mapWorker = new Worker("worker.js");
}
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 13,
center: {
lat: -27.938433,
lng: 153.407170
},
mapTypeId: 'satellite'
});
// Show the lat and lng under the mouse cursor.
var coordsDiv = document.getElementById('coords');
map.controls[google.maps.ControlPosition.TOP_CENTER].push(coordsDiv);
map.addListener('mousemove', function(event) {
coordsDiv.textContent =
'lat: ' + Math.round(event.latLng.lat() * 1000) / 1000 + ', ' +
'lng: ' + Math.round(event.latLng.lng() * 1000) / 1000;
});
}
(function setupEventListeners() {
var gridVisable = false;
var listenerDrag, listenerZoom, listenerTextInput;
var gridBtn = document.getElementById('gridBtn');
var exportBtn = document.getElementById('exportBtn');
gridBtn.addEventListener('click', function(e) {
e.target.classList.toggle("on");
if (gridVisable == false) {
exportBtn.classList.remove("disabled");
gridUpdate();
listenerDrag = map.addListener('dragend', gridUpdate);
listenerZoom = map.addListener('zoom_changed', gridUpdate);
listenerTextInput = scaleInput.addEventListener('change', gridUpdate);
} else {
exportBtn.classList.add("disabled");
google.maps.event.removeListener(listenerDrag);
google.maps.event.removeListener(listenerZoom);
scaleInput.removeEventListener('change', gridUpdate);
removeGrid(rectArr);
}
gridVisable = !gridVisable;
});
// EXPORT ALL
exportBtn.addEventListener('click', function (e) {
if (gridVisable == false) {
return
}
// if (rectArr.length > 15) {
// var result = prompt("Please input your own API key.\nYou can get your API key here: https://developers.google.com/maps/documentation/static-maps/get-api-key");
// if (result !== null) {
// console.log('Using API KEY:', result);
// APIKEY = result;
// zipMe();
// }
if (rectArr.length > limit) {
alert("Mass downloading is prohibited see Google Maps T&C Section 10.5e:\nhttps://developers.google.com/maps/terms#section_10\nI've set the max number of requests to "+limit+" requests for now.");
} else {
zipMe();
}
});
})();
function gridUpdate() {
var t1 = performance.now();
removeGrid(rectArr);
rectArr = []; // reset array
var mapDlOptions = createMapDlOptions();
if (typeof mapWorker != 'undefined') {
mapWorker.postMessage(mapDlOptions);
mapWorker.onmessage = function(e) { // 90% of the time is spend here
// console.log('Message received from worker', e);
var rectangles = e.data;
for (var i = rectangles.length - 1; i >= 0; i--) {
var bounds = new google.maps.LatLngBounds(rectangles[i].bounds.sw, rectangles[i].bounds.ne);
drawRectangle(bounds, rectangles[i].url);
}
var t2 = performance.now();
console.log("I completed the grid in " + (t2 - t1) + " milliseconds.");
};
} else {
console.info('WebWorker API not supported!');
mapDlOptions.gMap = google;
mapDlOptions.callback = function (rectangle) { // 90% of the time is spend here
drawRectangle(rectangle.gbounds, rectangle.url);
}
MapDl(mapDlOptions);
var t2 = performance.now();
console.log("I completed the grid in " + (t2 - t1) + " milliseconds.");
}
}
function drawRectangle(bounds, url) {
var gRectangle = new google.maps.Rectangle({
strokeColor: "#00FF00",
strokeOpacity: 0.8,
strokeWeight: 1,
fillOpacity: 0,
map: map,
bounds: bounds,
url: url,
});
rectArr.push(gRectangle);
google.maps.event.addListener(gRectangle, 'click', function (event) {
download(gRectangle.url)
});
}
function createMapDlOptions() {
sizeValue = document.getElementById('scaleInput').value // km
if (sizeValue == NaN || sizeValue == Infinity || sizeValue <= 0) { // some browsers don't sanitize user input
console.error("Error: size is not a valid number!", sizeValue);
return;
}
return {
boundaries: map.getBounds().toJSON(),
scale: sizeValue,
maptype: map.getMapTypeId()
};
}
var removeGrid = function (rectangles) {
for (var i = rectangles.length - 1; i >= 0; i--) {
rectangles[i].setMap(null);
}
}
function ajax (url, responseType, callback) {
var request = new XMLHttpRequest(); // http://youmightnotneedjquery.com/#request // IE9+
request.open('GET', url, true);
request.responseType = 'blob';
console.log('Requesting:', url);
request.onload = function(e) {
if (request.status >= 200 && request.status < 400) {
// Success!
callback(e.target.response);
} else {
// We reached our target server, but it returned an error
console.error("Google Maps API send an Error!: " + request.statusText, request.status);
// https://developers.google.com/maps/documentation/static-maps/error-messages
}
};
request.onerror = function() {
// There was a connection error of some sort
console.error("Unable to contact Google Maps API!");
};
request.send();
}
function zipMe() {
var progress = document.getElementById ('progress');
if (progress) { // Show progress bar
progress.max = rectArr.length -1;
progress.classList.toggle("hidden");
}
var zip = new JSZip();
var count = 0;
for (var i = 0; i < rectArr.length; i++) {
ajax(rectArr[i].url, 'blob', function (response) {
console.log('Received #'+count+':', response);
zip.file("map("+count+").png", response);
if (count == rectArr.length -1) { // if the last image is added than download the zip.
if (progress) { // Hide progress bar
progress.classList.toggle("hidden");
}
zip.generateAsync({type:"blob"}).then(function (blob) {
download(blob, "map.zip");
});
}
count++;
if (progress) { // Update progress bar
progress.value = count;
}
});
}
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?v=3.exp&key=AIzaSyDLGb2-Qs3xFIx2TYQ7yKYLTypgo3TGcoY&callback=initMap&libraries=geometry"></script>
<script async defer src="map-dl.js"></script>
<script async defer src="jszip/jszip.min.js"></script>
<script async defer src="download/download.js"></script>
</body>
</html>