| 1 |
const wdk_map_draw = (wdk_map, options = null) => { |
| 2 |
var $ = jQuery, |
| 3 |
map = wdk_map, |
| 4 |
editableLayers, drawPluginOptions, drawControl, removeButtonControl; |
| 5 |
|
| 6 |
const event_init = () => { |
| 7 |
editableLayers = new L.FeatureGroup(); |
| 8 |
map.addLayer(editableLayers); |
| 9 |
|
| 10 |
drawPluginOptions = { |
| 11 |
position: 'topleft', |
| 12 |
draw: { |
| 13 |
drag: true, |
| 14 |
polyline: false, |
| 15 |
polygon: false, |
| 16 |
circle: false, |
| 17 |
rectangle: { |
| 18 |
shapeOptions: { |
| 19 |
clickable: false, |
| 20 |
color: '#ff7800', |
| 21 |
weight: 1, |
| 22 |
}, |
| 23 |
showRadius: true, |
| 24 |
}, |
| 25 |
circlemarker: false, |
| 26 |
marker: false, |
| 27 |
showArea: true, |
| 28 |
showLength: true |
| 29 |
} |
| 30 |
}; |
| 31 |
|
| 32 |
drawPluginOptions = $.extend(drawPluginOptions, options); |
| 33 |
|
| 34 |
drawControl = new L.Control.Draw(drawPluginOptions); |
| 35 |
map.addControl(drawControl); |
| 36 |
|
| 37 |
// Create a custom button for removing all rectangles |
| 38 |
const RemoveRectanglesButton = L.Control.extend({ |
| 39 |
options: { |
| 40 |
position: drawPluginOptions.position |
| 41 |
}, |
| 42 |
onAdd: function () { |
| 43 |
var container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom leaflet-draw-toolbar'); |
| 44 |
container.innerHTML = '<a class="leaflet-draw-draw-rectangle-remove" href="#"></a>'; |
| 45 |
container.style.display = 'none'; |
| 46 |
|
| 47 |
container.onclick = function () { |
| 48 |
removeAllRectangles(); |
| 49 |
}; |
| 50 |
|
| 51 |
return container; |
| 52 |
} |
| 53 |
}); |
| 54 |
|
| 55 |
// Add the custom button to the map |
| 56 |
removeButtonControl = new RemoveRectanglesButton(); |
| 57 |
map.addControl(removeButtonControl); |
| 58 |
|
| 59 |
map.on(L.Draw.Event.CREATED, function (e) { |
| 60 |
var type = e.layerType, |
| 61 |
layer = e.layer; |
| 62 |
|
| 63 |
if (type === 'rectangle') { |
| 64 |
setRectangle(layer, false); |
| 65 |
|
| 66 |
// Get coordinates of the polygon |
| 67 |
var coordinates = layer.getLatLngs(); |
| 68 |
if(coordinates) { |
| 69 |
$('.wdk-search-form').find('input[name="rectangle_ne"]').val(coordinates[0][1].lat+','+coordinates[0][1].lng); |
| 70 |
$('.wdk-search-form').find('input[name="rectangle_sw"]').val(coordinates[0][3].lat+','+coordinates[0][3].lng); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
editableLayers.addLayer(layer); |
| 75 |
}); |
| 76 |
|
| 77 |
map.on('draw:drawstart', function (e) { |
| 78 |
editableLayers.clearLayers(); |
| 79 |
}); |
| 80 |
}; |
| 81 |
|
| 82 |
const setRectangle = (layer, fitBounds = true) => { |
| 83 |
editableLayers.addLayer(layer); |
| 84 |
|
| 85 |
// Set the zoom level |
| 86 |
if(fitBounds) { |
| 87 |
let zoom_map = map.getZoom(); |
| 88 |
map.fitBounds(layer.getBounds()); |
| 89 |
map.setZoom(zoom_map); |
| 90 |
} |
| 91 |
|
| 92 |
removeButtonControl.getContainer().style.display = 'block'; |
| 93 |
}; |
| 94 |
|
| 95 |
const removeAllRectangles = () => { |
| 96 |
editableLayers.clearLayers(); |
| 97 |
removeButtonControl.getContainer().style.display = 'none'; |
| 98 |
}; |
| 99 |
|
| 100 |
// FeatureGroup is to store editable layers |
| 101 |
event_init(); |
| 102 |
|
| 103 |
return { |
| 104 |
setRectangle: setRectangle, |
| 105 |
removeAllRectangles: removeAllRectangles |
| 106 |
}; |
| 107 |
}; |
| 108 |
|