Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing polygon from google maps drawingManager V3

I have a user editable google map where users can draw an overlay polygon onto the map using the drawing manager. This works fine and the console logs the lat lngs I need. However, I need to add a button that will clear the map of the polygon so they can draw again if a mistake was made. My implementation code is pasted below:

geocoder = new google.maps.Geocoder();
      var latlng = new google.maps.LatLng(<?php echo $latitude ?>, <?php echo $longitude ?>);
       var myOptions = {
         zoom: <?php echo $zoomlevel ?>,
         center: latlng,
         mapTypeId: google.maps.MapTypeId.ROADMAP
       };
       var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

       marker = new google.maps.Marker({
         map:map,
         draggable:true,
         animation: google.maps.Animation.DROP,
         position: latlng
       })

       pos = marker.position;
       //alert(pos);
       document.getElementById("gpsite-surgery-latitude").value = pos.lat();
       document.getElementById("gpsite-surgery-longitude").value = pos.lng();

       google.maps.event.addListener(marker, "dragend", function() {
               var myLatLng = marker.latLng;

               pos = marker.position;
               //alert(pos);
               document.getElementById("gpsite-surgery-latitude").value = pos.lat();
               document.getElementById("gpsite-surgery-longitude").value = pos.lng();
       })

       google.maps.event.addListener(map, 'zoom_changed', function() {
          document.getElementById("gpsite-surgery-zoomlevel").value = map.getZoom();
       });

       var drawingManager = new google.maps.drawing.DrawingManager({
        drawingMode: google.maps.drawing.OverlayType.MARKER,
        drawingControl: true,
        drawingControlOptions: {
            position: google.maps.ControlPosition.TOP_CENTER,
            drawingModes: [google.maps.drawing.OverlayType.POLYGON]
        },
        polygonOptions: {
            fillColor: '#ffff00',
            fillOpacity: 0.4,
            strokeWeight: 3,
            clickable: true,
            zIndex: 1,
            editable: false
        }
       });


       google.maps.event.addListener(drawingManager, 'polygoncomplete', function (polygon) {
            var coordinates = (polygon.getPath().getArray());
            console.log(coordinates);
        });

       drawingManager.setMap(map);

   });

There is also a marker on the map, you can ignore this.

like image 744
Steve Smith Avatar asked Jul 24 '12 13:07

Steve Smith


People also ask

How do you delete a polygon in Google Maps?

To remove a polygon from the map, call the setMap() method passing null as the argument. In the following example, bermudaTriangle is a polygon object: bermudaTriangle. setMap(null);

How do I remove POI from Google Maps?

Click the "My Places" button just beneath the search bar. Click "Maps" and wait for the list of maps to appear down the left side of the screen. When they do, click the title of the map containing the marker that you want to remove.


2 Answers

Examine this code, it sounds exactly like what you are talking about, a button to delete the shape

http://gmaps-samples-v3.googlecode.com/svn-history/r282/trunk/drawing/drawing-tools.html

Edit: above link is broken but I was able to find that code here.

  // globals
  var drawingManager;
  var selectedShape;

  ...

  function clearSelection() {
    if (selectedShape) {
      selectedShape.setEditable(false);
      selectedShape = null;
    }
  }

  function setSelection(shape) {
    clearSelection();
    selectedShape = shape;
    shape.setEditable(true);
    selectColor(shape.get('fillColor') || shape.get('strokeColor'));
  }

  function deleteSelectedShape() {
    if (selectedShape) {
      selectedShape.setMap(null);
    }
  }

     google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {
        if (e.type != google.maps.drawing.OverlayType.MARKER) {
        // Switch back to non-drawing mode after drawing a shape.
        drawingManager.setDrawingMode(null);

        // Add an event listener that selects the newly-drawn shape when the user
        // mouses down on it.
        var newShape = e.overlay;
        newShape.type = e.type;
        google.maps.event.addListener(newShape, 'click', function() {
          setSelection(newShape);
        });
        setSelection(newShape);
      }
    });

    // Clear the current selection when the drawing mode is changed, or when the
    // map is clicked.
    google.maps.event.addListener(drawingManager, 'drawingmode_changed', clearSelection);
    google.maps.event.addListener(map, 'click', clearSelection);
    google.maps.event.addDomListener(document.getElementById('delete-button'), 'click', deleteSelectedShape);

    buildColorPalette();
  }
like image 176
Tina CG Hoehr Avatar answered Sep 19 '22 21:09

Tina CG Hoehr


You need a reference to the polygon in the global context. Then in the click handler function for the button call polygon.setMap(null) (where polygon is a global reference to the polygon, can't tell if it is global or not from the incomplete snippet you posted)

like image 22
geocodezip Avatar answered Sep 17 '22 21:09

geocodezip