我已经想出如何在添加折线或多边形或删除顶点时抓住坐标,但我似乎无法应用如何在移动折线或多边形顶点时捕获(仅限顶点,I故意拖动整个poly的残疾人)。我正在使用此链接作为参考:event after modifying polygon in google maps api v3
这是我完整的HTML代码(为了读者而在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>
答案 0 :(得分:3)
要收听可编辑多边形顶点拖动的拖动,请使用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);
}
代码段
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>