Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps draw polygon and delete the previous one

Tags:

google-maps

I'm trying to remove an existing polygon when the user selects the polygon draw button to draw a new one. It has to do something with drawingManager.setMap(null); but I can't make it work in my code. I've tried so many things but cant figure it out.

Info: When the page loads the drawingmode is set to polygon by default: drawingMode: google.maps.drawing.OverlayType.POLYGON

The user can draw a polygon to mark an area. When the polygon is completed, the DrawingMode is set to null: drawingManager.setDrawingMode(null); and the coordinates are put in an inputfield to save them later on in a database thru a form.

Can somebody please help me? Here is my code so far:

var map;
var drawingManager;    

function initialize() {
var myLatlng = new google.maps.LatLng(51.51686166794058, 3.5945892333984375);
var mapOptions = {
    zoom: 9,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
}

//Getting map DOM element
var mapElement = document.getElementById('map_canvas');

map = new google.maps.Map(map_canvas, mapOptions);

drawingManager = new google.maps.drawing.DrawingManager({
    drawingMode: google.maps.drawing.OverlayType.POLYGON,
    drawingControl: true,
    drawingControlOptions: {
        position: google.maps.ControlPosition.TOP_CENTER,
        drawingModes: [google.maps.drawing.OverlayType.POLYGON]
    },
    polygonOptions: {
        editable: true,
        draggable: true
    }
});

drawingManager.setMap(map);

// Add a listener for creating new shape event.
google.maps.event.addListener(drawingManager, "overlaycomplete", function(event) {
    var newShape = event.overlay;
    newShape.type = event.type;
    if(drawingManager.getDrawingMode()) {
        drawingManager.setDrawingMode(null);
    }

});

// Add a listener for the "drag" event.
google.maps.event.addListener(drawingManager, "overlaycomplete", function(event){
    overlayDragListener(event.overlay);
    $('#vertices').val(event.overlay.getPath().getArray());
});
}    

function overlayDragListener(overlay) {
google.maps.event.addListener(overlay.getPath(), 'set_at', function(event){
    $('#vertices').val(overlay.getPath().getArray());
});
google.maps.event.addListener(overlay.getPath(), 'insert_at', function(event){
    $('#vertices').val(overlay.getPath().getArray());
});
}

google.maps.event.addDomListener(window, 'load', initialize);
like image 432
Matt Avatar asked Dec 19 '22 07:12

Matt


1 Answers

Two things:

  1. add a global array (or variable) to allow access to the shapes
  2. add a drawingmode_changed event handler, when the drawing mode is changed back to non-null, remove any existing polygons.
var shapes = [];

google.maps.event.addListener(drawingManager, "drawingmode_changed", function() {
    if (drawingManager.getDrawingMode() != null) {
      for (var i=0; i < shapes.length; i++) {
        shapes[i].setMap(null);
      }
      shapes = [];
    }
});

working fiddle

Working code snippet:

var map;
var drawingManager;
var shapes = [];

function initialize() {
  var myLatlng = new google.maps.LatLng(51.51686166794058, 3.5945892333984375);
  var mapOptions = {
    zoom: 9,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }

  //Getting map DOM element
  var mapElement = document.getElementById('map_canvas');

  map = new google.maps.Map(map_canvas, mapOptions);

  drawingManager = new google.maps.drawing.DrawingManager({
    drawingMode: google.maps.drawing.OverlayType.POLYGON,
    drawingControl: true,
    drawingControlOptions: {
      position: google.maps.ControlPosition.TOP_CENTER,
      drawingModes: [google.maps.drawing.OverlayType.POLYGON]
    },
    polygonOptions: {
      editable: true,
      draggable: true
    }
  });

  drawingManager.setMap(map);

  // Add a listener for creating new shape event.
  google.maps.event.addListener(drawingManager, "overlaycomplete", function(event) {
    var newShape = event.overlay;
    newShape.type = event.type;
    shapes.push(newShape);
    if (drawingManager.getDrawingMode()) {
      drawingManager.setDrawingMode(null);
    }

  });

  // add a listener for the drawing mode change event, delete any existing polygons
  google.maps.event.addListener(drawingManager, "drawingmode_changed", function() {
    if (drawingManager.getDrawingMode() != null) {
      for (var i = 0; i < shapes.length; i++) {
        shapes[i].setMap(null);
      }
      shapes = [];
    }
  });


  // Add a listener for the "drag" event.
  google.maps.event.addListener(drawingManager, "overlaycomplete", function(event) {
    overlayDragListener(event.overlay);
    $('#vertices').val(event.overlay.getPath().getArray());
  });
}

function overlayDragListener(overlay) {
  google.maps.event.addListener(overlay.getPath(), 'set_at', function(event) {
    $('#vertices').val(overlay.getPath().getArray());
  });
  google.maps.event.addListener(overlay.getPath(), 'insert_at', function(event) {
    $('#vertices').val(overlay.getPath().getArray());
  });
}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=drawing&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>
like image 50
geocodezip Avatar answered Feb 01 '23 23:02

geocodezip