javascript - How to undo last added line in google map drawing -


i have 2 options in google map.
-->one polygon , 1 draw lines.
now trying perform undo functionality in page.when click undo button added line should removed(that may in either polygon option or draw line option).
is possible?

 function initmap() {           var map = new google.maps.map(document.getelementbyid('map'), {           center: {lat: -34.397, lng: 150.644},           zoom: 8           });            var drawingmanager = new google.maps.drawing.drawingmanager({             drawingmode: google.maps.drawing.overlaytype.marker,             drawingcontrol: true,             drawingcontroloptions: {               position: google.maps.controlposition.top_center,               drawingmodes: ['polygon', 'polyline']             },              circleoptions: {               strokecolor: '#00db00',               fillcolor: 'green',               fillopacity: 0.05,               strokeweight: 5,               clickable: false,               editable: true,               zindex: 1             }            });           drawingmanager.setmap(map);            google.maps.event.addlistener(drawingmanager, 'overlaycomplete', function (event)             {               if (event.type === 'marker') console.log('lat: ' + event.overlay.position.lat() + ', long: ' + event.overlay.position.lng())               else displaypolylatlng(event.overlay.getpath().b);              });             function displaypolylatlng(pointarray)           {debugger                var result=" ";               (var = 0; < pointarray.length; i++)                {                   result +='<b>lat: ' + pointarray[i].lat() + ', long: ' + pointarray[i].lng()+ '</b><br/>';               }               $('#info').html(result);                 /*var lastel = pointarray[pointarray.length-1];               alert(lastel)               if(lastel.length>1){               undo_redo.push(lastel.pop());               } */               }       }       function removeline(){         polylines.remove(polylines.size() - 1)       } 

fiddle: https://jsfiddle.net/rc5p32nt/

basically @geocodezip said...

keep reference shapes added:

var overlays = []; 

you can push latest overlay object event.overlay in overlaycomplete event:

google.maps.event.addlistener(drawingmanager, 'overlaycomplete', function(event) {    overlays.push(event.overlay); // store reference added overlay }); 

finally, on click of undo button hide overlay via setmap(null):

// undo last overlay action function removeline() {   var lastoverlay = overlays.pop();   if (lastoverlay) lastoverlay.setmap(null); } 

note doesn't remove object, hides showing on map. if want remove overlay should set null.

you can show them again - if wanted add redo button (in case wouldn't pop them have done). see https://developers.google.com/maps/documentation/javascript/examples/polyline-remove example of how implement this.

updated demo: https://jsfiddle.net/3fe6nfdr/


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -