source: gs3-extensions/map-editor/DrawingManager/checkpoints-singlefile/index backup v9.html@ 32709

Last change on this file since 32709 was 32709, checked in by ak19, 5 years ago

Jump-start to the map-editor extension, which takes account of the 21 previous check-point versions.

File size: 14.3 KB
Line 
1<!DOCTYPE html>
2<html>
3 <head>
4 <title>Drawing Tools</title>
5 <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
6 <meta charset="utf-8">
7 <style>
8 /* Always set the map height explicitly to define the size of the div
9 * element that contains the map. */
10 #map {
11 height: 83%;
12 width: 90;
13 float: bottom;
14 clear: both;
15 }
16 /* Optional: Makes the sample page fill the window. */
17 html, body {
18 height: 100%;
19 width: 99%;
20 margin: 0;
21 padding: 0;
22
23 }
24
25 #DeleteButtons{
26 display: inline-block;
27 float: left;
28 padding-left: 10px;
29 padding-top: 5px;
30 width: 100%
31
32 }
33
34 #LineThickness{
35 display: inline-block;
36 float: left;
37 padding-left: 10px;
38 padding-top: 5px;
39
40 }
41
42 #ColourOpacity{
43 display: inline-block;
44 float: left;
45 padding-left: 10px;
46 padding-top: 5px;
47
48 }
49
50 #buttons3{
51 display: inline-block;
52 float: left;
53 padding-left: 10px;
54 padding-top: 5px;
55
56 }
57
58 #FillColour {
59 display: inline-block;
60 float: left;
61 padding-left: 10px;
62 padding-top: 5px;
63 }
64
65 #color-palette1 {
66 clear: both;
67 }
68
69 .color-buttons1 {
70 width: 14px;
71 height: 14px;
72 font-size: 0;
73 margin: 2px;
74 float: left;
75 cursor: pointer;
76 }
77
78
79 </style>
80 </head>
81 <body>
82 <div id = "DeleteButtons">
83 <button onclick="removeLine()" accesskey="z">Delete Last</button>
84 <button onclick="deleteAllShape()" accesskey="c">Clear All</button>
85 <button onclick="deleteSelectedShape()" accesskey="b">Delete Selected</button>
86 <button onclick="polygonGetPath()" accesskey="n">Get Path</button>
87
88 </div>
89
90 <div id = "LineThickness">
91 <p>Line thickness: <input type="range" min="20" max="100" value="1" class="slider" id="thicknessRange"> </p>
92 <p>Value: <span id="thicknessRangeVal"></span></p>
93 </div>
94
95 <div id = "ColourOpacity">
96 <p>Colour opacity: <input type="range" min="10" max="100" value="40" class="slider" id="colourOpacity"> </p>
97 <p>Value: <span id="opacityRangeVal"></span></p>
98 </div>
99
100
101
102 <div id="FillColour">
103 <p> Fill Colour:</p> <div id="color-palette1"></div>
104 </div>
105
106
107
108 <div id="map"></div>
109
110 <script>
111 var shiftKeyPressed = false;
112 var mousedDownFired = false;
113 var colors = ['#1E90FF', '#FF1493', '#4B0082', '#32CD32', '#FF8C00', '#000000'];
114 var selectedColor;
115 var colorButtons = {};
116 var thicknessValue = 1;
117 var opacityValue = 0.4;
118 var polyOptions = {
119 fillColor: '#CA4A2F',
120 strokeWeight: thicknessValue,
121 fillOpacity: opacityValue,
122 editable: true,
123 draggable: true,
124 geodesic : false,
125 };
126
127
128 var overlays = [];
129 var drawingManager;
130 var selectedShape;
131 var selectedShapes = [];
132
133
134
135 //Update thickness
136 var thicknessSlider = document.getElementById("thicknessRange");
137 var thicknessSliderOutput = document.getElementById("thicknessRangeVal");
138 thicknessSliderOutput.innerHTML = thicknessSlider.value/20;
139
140 thicknessSlider.oninput = function() {
141 thicknessSliderOutput.innerHTML = this.value/20;
142 thicknessValue = this.value/20;
143 polyOptions.strokeWeight = thicknessValue;
144 setSelectedThickness(thicknessValue);
145 }
146
147 //Update opacity
148 var opacitySlider = document.getElementById("colourOpacity");
149 var opacitySliderOutput = document.getElementById("opacityRangeVal");
150 opacitySliderOutput.innerHTML = "% " + Math.round(opacitySlider.value);
151
152 opacitySlider.oninput = function() {
153 opacityValue = this.value/100;
154 polyOptions.fillOpacity = opacityValue;
155 opacitySliderOutput = opacityValue;
156 opacityRangeVal.innerHTML = "% " + Math.round( opacitySliderOutput * 100);
157 setSelectedOpacity(opacityValue);
158 }
159
160
161 ////////////////////////////////////////////////////////////////////////////////////////
162 function makeColorButton (color) {
163 var button = document.createElement('span');
164 button.className = 'color-buttons1';
165 button.style.backgroundColor = color;
166 google.maps.event.addDomListener(button, 'click', function () {
167 selectColor(color);
168 setSelectedShapeColor(color);
169 });
170 return button;
171 }
172 function buildColorPalette () {
173 var colorPalette = document.getElementById('color-palette1');
174 for (var i = 0; i < colors.length; ++i) {
175 var currColor = colors[i];
176 var colorButton = makeColorButton(currColor);
177 colorPalette.appendChild(colorButton);
178 colorButtons[currColor] = colorButton;
179 }
180 selectColor(colors[0]);
181 };
182 function selectColor (color) {
183 selectedColor = color;
184 for (var i = 0; i < colors.length; ++i) {
185 var currColor = colors[i];
186 colorButtons[currColor].style.border = currColor == color ? '2px solid #789' : '2px solid #fff';
187 }
188
189 // Retrieves the current options from the drawing manager and replaces the
190 // stroke or fill color as appropriate.
191 var polylineOptions = drawingManager.get('polylineOptions');
192 polylineOptions.strokeColor = color;
193 drawingManager.set('polylineOptions', polylineOptions);
194
195 var rectangleOptions = drawingManager.get('rectangleOptions');
196 rectangleOptions.fillColor = color;
197 drawingManager.set('rectangleOptions', rectangleOptions);
198
199 var circleOptions = drawingManager.get('circleOptions');
200 circleOptions.fillColor = color;
201 drawingManager.set('circleOptions', circleOptions);
202
203 var polygonOptions = drawingManager.get('polygonOptions');
204 polygonOptions.fillColor = color;
205 drawingManager.set('polygonOptions', polygonOptions);
206 }
207
208
209
210
211 function initMap() {
212
213 var map = new google.maps.Map(document.getElementById('map'), {
214 center: {lat: -34.397, lng: 150.644},
215 zoom: 3
216 });
217
218
219 drawingManager = new google.maps.drawing.DrawingManager({
220 drawingMode: google.maps.drawing.OverlayType.RECTANGLE,
221 drawingControl: true,
222 drawingControlOptions: {
223 position: google.maps.ControlPosition.TOP_CENTER,
224 drawingModes: ['marker', 'circle', 'polygon', 'polyline', 'rectangle']
225 },
226 markerOptions: { draggable: true},
227 circleOptions: polyOptions,
228 polylineOptions : polyOptions,
229 polygonOptions: polyOptions,
230 rectangleOptions: polyOptions,
231 });
232
233 drawingManager.setMap(map);
234
235
236 //overlays.push(event.overlay); // store reference to added overlay
237 google.maps.event.addListener(drawingManager, 'overlaycomplete', function (e) {
238 overlays.push(e.overlay); // store reference to added overlay
239 var newShape = e.overlay;
240 newShape.type = e.type;
241
242 if (e.type !== google.maps.drawing.OverlayType.MARKER) {
243
244 // Switch back to non-drawing mode after drawing a shape.
245 //drawingManager.setDrawingMode(null);
246
247 // Add an event listener that selects the newly-drawn shape when the user
248 // mouses down on it.
249 google.maps.event.addListener(newShape, 'click', function(e) {
250
251 vertexAndPolyDel(e,newShape);
252 mousedDownFired = false;
253 return;
254
255 });
256
257 google.maps.event.addListener(newShape, 'drag', function() {
258 //console.log(selectedShape);
259 console.log(selectedShapes.length);
260 if (selectedShapes.length > 1) {
261 //var xPos = selectedShape.
262 console.log(selectedShape);
263 }
264
265 });
266
267 //Add an event listener to select a shape if the mouse hovers over it
268 google.maps.event.addListener(newShape, 'mousedown', function(e) {
269 if(drawingManager.drawingMode == null) {
270 mousedDownFired =true;
271// console.log("MOUSDOWN ", e);
272
273 setSelection(newShape);
274 }
275 });
276 setSelection(newShape);
277 }
278 else {
279 //cLICK EVENT IF A MARKER IS CREATED
280 google.maps.event.addListener(newShape, 'click', function (e) {
281 setSelection(newShape);
282 });
283 setSelection(newShape);
284 }
285 });
286
287 //Deletes a vertex if clicked on it
288 function vertexAndPolyDel(e,newShape) {
289 var vertex = e.vertex;
290 if (e.vertex !== undefined) {
291 if (newShape.type === google.maps.drawing.OverlayType.POLYGON) {
292 var path = newShape.getPaths().getAt(e.path);
293 path.removeAt(e.vertex);
294 if (path.length < 3) {
295 newShape.setMap(null);
296 }
297 }
298 if (newShape.type === google.maps.drawing.OverlayType.POLYLINE) {
299 var path = newShape.getPath();
300 path.removeAt(e.vertex);
301 if (path.length < 2) {
302 newShape.setMap(null);
303 }
304 }
305 }
306
307 //if(mousedDownFired == false)
308 //{
309 //setSelection(newShape, vertex);
310 //mousedDownFired = false;
311 //return;
312 //}
313 }
314
315 function clearSelection () {
316 if (selectedShape) {
317 if (selectedShape.type !== 'marker') {
318 selectedShape.setEditable(false);
319 if(shiftKeyPressed == false){
320 for (var i=0; i < selectedShapes.length; i++) {
321 selectedShapes[i].setEditable(false);
322 }
323 selectedShapes = [];
324 }
325 }
326
327 selectedShape = null;
328 }
329 }
330 var i = 0;
331 function setSelection (shape, e) {
332// console.log(e);
333// console.log(selectedShapes.length);
334 if (shape.type !== 'marker') {
335 if(shiftKeyPressed == false) {
336 if(e == undefined) {
337 clearSelection();
338 shape.setEditable(true);
339 }
340
341 }
342
343 //var shapeAlreadySelected = selectedShapes.includes(shape);
344
345 if (selectedShapes.includes(shape)){
346 shape.setEditable(false);
347 selectedShapes.splice(shape, 1)
348 }
349
350 else {
351 shape.setEditable(true);
352 selectedShapes.push(shape);
353 }
354
355
356 console.log(selectedShapes.length);
357
358 //Send the values to be updated
359 var thi = shape.strokeWeight;
360 var opa = shape.fillOpacity;
361 var fCol = shape.fillColor;
362 var sCol = shape.strokeColor;
363 updateMenuValues(thi, opa, fCol, sCol);
364
365 }
366 else if (shape.type == 'marker') {
367 clearSelection();
368 }
369
370 selectedShape = shape;
371 }
372
373
374 //Clears selection if clicked on the map when shift is not presseed
375 google.maps.event.addListener(map, 'click', function() {
376 if (shiftKeyPressed == false) {
377 clearSelection();
378 }
379 });
380
381
382
383
384 //Setting drawing tool option
385 document.addEventListener('keydown', function() {
386 //if (event.code == 'KeyX' && (event.ctrlKey || event.metaKey))
387 if (event.code == 'Digit0' || event.code == 'Numpad0' || event.code == 'Backquote') {
388 //clearSelection();
389 drawingManager.setDrawingMode(null);
390 } else if (event.code == 'Digit1') {
391 drawingManager.setDrawingMode('marker');
392 } else if (event.code == 'Digit2') {
393 drawingManager.setDrawingMode('circle');
394 } else if (event.code == 'Digit3') {
395 drawingManager.setDrawingMode('polygon');
396 } else if (event.code == 'Digit4') {
397 drawingManager.setDrawingMode('polyline');
398 } else if (event.code == 'Digit5' ) {
399 drawingManager.setDrawingMode('rectangle');
400 }
401// console.log(event.code);
402 });
403
404 //Sets shift as pressed
405 document.addEventListener('keydown', function() {
406 if (event.code == 'ShiftLeft' || event.code == 'ShiftRight')
407 {
408 shiftKeyPressed = true;
409
410 }
411
412 });
413
414 //Sets shift as unpressed
415 document.addEventListener('keyup', function() {
416 if (event.code == 'ShiftLeft' || event.code == 'ShiftRight')
417 {
418 shiftKeyPressed = false;
419 }
420
421 });
422
423
424 buildColorPalette();
425}
426 //Set selected thickness
427 function setSelectedThickness(sWeight) {
428 if (selectedShape){
429 selectedShape.set('strokeWeight', sWeight)
430 }
431 if(selectedShapes !== 0) {
432 for (var i=0; i < selectedShapes.length; i++) {
433 selectedShapes[i].set('strokeWeight', sWeight);
434 }
435 }
436 }
437
438 //Set selected opacity
439 function setSelectedOpacity(fOpacity) {
440 if (selectedShape) {
441 selectedShape.set('fillOpacity', fOpacity)
442 }
443 if(selectedShapes !== 0) {
444 for (var i=0; i < selectedShapes.length; i++) {
445 selectedShapes[i].set('fillOpacity', fOpacity);
446 }
447 }
448 }
449
450
451
452 //set selected fill colour
453 function setSelectedShapeColor (color) {
454 if (selectedShape) {
455 if (selectedShape.type == google.maps.drawing.OverlayType.POLYLINE) {
456 selectedShape.set('strokeColor', color);
457 } else {
458 selectedShape.set('fillColor', color);
459 selectedShape.set('strokeColor', color);
460 }
461 }
462 if(selectedShapes !== 0) {
463 for (var i=0; i < selectedShapes.length; i++) {
464 selectedShapes[i].set('fillColor', color);
465 selectedShapes[i].set('strokeColor', color);
466 }
467 }
468 }
469
470
471 function updateMenuValues(thi, opa, fCol, sCol){
472
473 //Update thickness slider and value on the settings menu
474 var thicknessSliderOutput = document.getElementById("thicknessRangeVal");
475 thicknessSliderOutput.innerHTML = thi;
476 document.getElementById("thicknessRange").value = thi*20;
477
478 //Update the opacity slider and value on the settings menu
479 var opacitySliderOutput = document.getElementById("opacityRangeVal");
480 opacitySliderOutput.innerHTML = "% " + opa*100;
481 document.getElementById("colourOpacity").value = opa*100;
482
483
484 if (drawingManager.drawingMode == null) {
485 selectColor(fCol);
486 }
487 }
488
489
490function polygonGetPath() {
491 if (selectedShape){
492 var Psp = selectedShape.getPaths();
493 console.log(Psp);
494 }
495
496}
497
498function deleteSelectedShape () {
499 for (var i=0; i < selectedShapes.length; i++){
500 selectedShapes[i].setMap(null);
501 }
502 selectedShapes = [];
503}
504
505function removeLine() {
506 var lastOverlay = overlays.pop();
507 if (lastOverlay) lastOverlay.setMap(null);
508 }
509
510function deleteAllShape() {
511 for (var i=0; i < overlays.length; i++) {
512 overlays[i].setMap(null);
513
514 }
515 overlays = [];
516}
517
518 </script>
519 <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBzunGwIF2OwsfrOkIC_8bhh0OPGZXo52Y&libraries=drawing&callback=initMap" async defer></script>
520 <script src="shortcuts.js"></script>
521 </body>
522</html>
Note: See TracBrowser for help on using the repository browser.