polyline/polygon 顶点移动时获取更新的坐标。 (Google 地图)

Getting updated coords when polyline/polygon vertex moved. (Google Maps)

我已经想出如何在添加多段线或多边形或删除顶点时获取坐标,但我似乎无法应用如何在多段线或多边形顶点移动时捕捉(仅限顶点) ,我有意拖动整个多边形的禁用)。我使用这个 link 作为参考:event after modifying polygon in google maps api v3

这是我的完整 HTML 代码(为了 reader 而发布在 pastebin 上): http://pastebin.com/95HcHqQR

这是我尝试进行捕捉动作的部分:

google.maps.event.addListener(drawingManager, 'overlaycomplete', function(e) {

  var newShape = e.overlay;
  newShape.type = e.type;

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

    ///////////////////////////////////////////////////////////
    //This section is intended to catch vertex moves...currently not

    google.maps.event.addListener(newShape, 'polygoncomplete', function(e) {
      google.maps.event.addListener(newShape.getPath(), 'set_at', function(e) {
        google.maps.event.addListener(newShape.getPath(), 'insert_at', function(e) {
          var path = newShape.getPaths().getAt(e.path);
          document.getElementById("polygonEditTest").value += path.getAt(e.vertex) + '\n';
        })
      });
    });
    google.maps.event.addListener(newShape, 'polylinecomplete', function(e) {
      google.maps.event.addListener(newShape.getPath(), 'set_at', function(e) {
        google.maps.event.addListener(newShape.getPath(), 'insert_at', function(e) {
          var path = newShape.getPaths().getAt(e.path);
          document.getElementById("polylineEditTest").value += path.getAt(e.vertex) + '\n';
        })
      });
    });
    ///////////////////////////////////////////////////////////

    // Add an event listener that selects the newly-drawn shape when the user
    // clicks it.
    google.maps.event.addListener(newShape, 'click', function(e) {
      if (e.vertex !== undefined) {
        if (newShape.type === google.maps.drawing.OverlayType.POLYGON) {
          var path = newShape.getPaths().getAt(e.path);
          path.removeAt(e.vertex);

          /////////////////////////////////////////////////////////////
          //Update textboxes with geo data when polygon vertex deleted
          document.getElementById("action_gon").value = ''
          document.getElementById("action_gon").value += "#polygon vertex deleted\n";
          for (var i = 0; i < path.length; i++) {
            document.getElementById("action_gon").value += path.getAt(i) + '\n';
          }
          //
          if (path.length < 3) {
            newShape.setMap(null);
            document.getElementById("action_gon").value = 'This box shows coords when a new POLYGON shape is added and/or vertex deleted'
          }
        }

        if (newShape.type === google.maps.drawing.OverlayType.POLYLINE) {
          var path = newShape.getPath();
          path.removeAt(e.vertex);
          /////////////////////////////////////////////////////////////
          //Update textboxes with geo data when polyline vertex deleted
          document.getElementById("action_line").value = ''
          document.getElementById("action_line").value += "#polyline vertex deleted\n";
          for (var i = 0; i < path.length; i++) {
            document.getElementById("action_line").value += path.getAt(i) + '\n';
          }
          //
          if (path.length < 2) {
            newShape.setMap(null);
            document.getElementById("action_line").value = 'This box shows coords when a new POLYLINE shape is added and/or vertex deleted'
          }
        }
      }

      setSelection(newShape);
    });
    setSelection(newShape);
  } else {
    google.maps.event.addListener(newShape, 'click', function(e) {
      setSelection(newShape);
    });
    setSelection(newShape);
  }
});

编辑:我认为这是演示问题(无法捕获顶点移动)所需的最少代码。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<title>Test</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=drawing,places"></script>

<style type="text/css">
#map, html, body {
padding: 0;
margin: 0;
width: 100%;
height: 100%;
}

</style>

<script type="text/javascript">

//Map Specifications

function initialize () {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 18,
center: new google.maps.LatLng(33.27144940863937, -117.2983479390361),
mapTypeId: google.maps.MapTypeId.HYBRID,
disableDefaultUI: true,
zoomControl: true,
mapTypeControl: false,  
scaleControl: true,
streetViewControl: true,
rotateControl: true,
fullscreenControl: false
});

var polyOptions = {
strokeWeight: 0,
fillOpacity: 0.45,
editable: true,
draggable: false
};

// Creates a drawing manager attached to the map that allows the user to draw
// markers and lines
drawingManager = new google.maps.drawing.DrawingManager({
drawingControlOptions: {
position: google.maps.ControlPosition.TOP_CENTER,
drawingModes: [
google.maps.drawing.OverlayType.POLYLINE,
google.maps.drawing.OverlayType.POLYGON
]
},
markerOptions: {
draggable: false
},
polylineOptions: {
editable: true,
draggable: false
},
//rectangleOptions: polyOptions,
polygonOptions: polyOptions,
map: map
});

//////////////////////////////////////////
var drawingManager;
var selectedShape;
function clearSelection () {
if (selectedShape) {
    if (selectedShape.type !== 'marker') {
        selectedShape.setEditable(false);
    }
    selectedShape = null;
}
}

function setSelection (shape) {
if (shape.type !== 'marker') {
    clearSelection();
    shape.setEditable(true);
}
selectedShape = shape;
}
//////////////////////////////////////////

google.maps.event.addListener(drawingManager, 'overlaycomplete', function (e) {
var newShape = e.overlay;         
newShape.type = e.type;

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

    google.maps.event.addListener(newShape, 'polygoncomplete', function (e) {
    google.maps.event.addListener(newShape.getPath(), 'set_at', function() {
    google.maps.event.addListener(newShape.getPath(), 'insert_at', function() {
    var path = newShape.getPaths().getAt(e.path);
    document.getElementById("polygonEditTest").value += path.getAt(e.vertex) + '\n';
    })
    });
    });

    google.maps.event.addListener(newShape, 'polylinecomplete', function (e) {
    google.maps.event.addListener(newShape.getPath(), 'set_at', function(e) {
    google.maps.event.addListener(newShape.getPath(), 'insert_at', function(e) {
    var path = newShape.getPaths().getAt(e.path);
    document.getElementById("polylineEditTest").value += path.getAt(e.vertex) + '\n';
    })
    });
    });

    // Add an event listener that selects the newly-drawn shape when the user
    // clicks it.
    setSelection(newShape);}
else {
    google.maps.event.addListener(newShape, 'click', function (e) {
    setSelection(newShape);
    });
    setSelection(newShape);
    }
});
}

google.maps.event.addDomListener(window, 'load', initialize);

</script>
</head>
<body>
<div id="geoinfoboxes">
    <textarea id="polylineEditTest" rows="8" cols="46">This box shows coords for edited POLYLINES (ie. moving a vertex)</textarea>
    <textarea id="polygonEditTest" rows="8" cols="46">This box shows coords for edited POLYGONS (ie. moving a vertex)</textarea>
</div>
<div id="map"></div>
</body>
</html>

要监听可​​编辑多边形顶点拖动的拖动,请使用 MCVArray set_at 事件监听器:

google.maps.event.addListener(polygon.getPath(), 'set_at', processVertex);
google.maps.event.addListener(polygon.getPath(), 'insert_at', processVertex);
function processVertex(e) {
  var logStr = ""
  for (var i = 0; i < this.getLength(); i++) {
    logStr += this.getAt(i).toUrlValue(6) + " ";
  }
  console.log(logStr);
}

proof of concept fiddle

代码片段:

function initialize() {
  var map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 12,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  var polygon = new google.maps.Polygon({
    path: [new google.maps.LatLng(37.4544762, -122.1161696),
      new google.maps.LatLng(37.3751, -122.1731859),
      new google.maps.LatLng(37.4274745, -122.169719)
    ],
    map: map,
    editable: true
  });
  google.maps.event.addListener(polygon.getPath(), 'set_at', processVertex);
  google.maps.event.addListener(polygon.getPath(), 'insert_at', processVertex);

  function processVertex(e) {
    var logStr = ""
    for (var i = 0; i < this.getLength(); i++) {
      logStr += this.getAt(i).toUrlValue(6) + " ";
    }
    console.log(logStr);
  }
}
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"></script>
<div id="map_canvas"></div>