I am trying to write a simple snapping function for editing a polygon in Google Maps v3.9
I have several polygons on the map, which can be made editable one at a time. When one is made editable, I also add a listener to it, to trigger when a vertex has been dragged.
As there is no drag event for google.maps.Polygon
, I add a mousedown
listener, which checks if a vertex is beneath the cursor, and if so adds a mouseup
listener.
The code within this mouseup
listener checks against the vertices of all other polygons, and updates the dragging vertex if a close match is found.
This works well, except for one problem. The check is performed using the latLng
property of the PolyMouseEvent
returned by the mouseup
listener. This property points to the location of the vertex before it was dragged, and I can't find a way to reference the vertex in its new position.
dragListener = google.maps.event.addListener( poly1, "mousedown", function( dragData ) {
// if dragging a vertex
if ( dragData.vertex != null ) {
// perform snap on mouseup
snapListener = google.maps.event.addListener( poly1, "mouseup", function( mouseData ) {
var editingVertex = mouseData.vertex;
var draggedLatLng = mouseData.latLng;
// code here to compare draggedLatLng with latLng of editingVertex
// and perform snap, which seems to work fine as is...
}
}
}