| 1 |
const wdk_map_draw = (wdk_map, options = null) => { |
| 2 |
var $ = jQuery, |
| 3 |
map = wdk_map, |
| 4 |
editableLayers, drawPluginOptions, drawControl, removeButtonControl,outGreyMask; |
| 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: 'topright', /*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 |
event_rectange_changed(); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
editableLayers.addLayer(layer); |
| 76 |
}); |
| 77 |
|
| 78 |
map.on('draw:drawstart', function (e) { |
| 79 |
editableLayers.clearLayers(); |
| 80 |
removeOuther(); |
| 81 |
}); |
| 82 |
|
| 83 |
|
| 84 |
// Custom control for the "Search by Drawing" button |
| 85 |
var CustomControl = L.Control.extend({ |
| 86 |
options: { |
| 87 |
position: 'topright' // Position of the control |
| 88 |
}, |
| 89 |
|
| 90 |
onAdd: function (map) { |
| 91 |
var container = L.DomUtil.create('div', 'custom-draw-button'); |
| 92 |
container.innerHTML = 'Search by Drawing'; |
| 93 |
container.onclick = function () { |
| 94 |
new L.Draw.Rectangle(map, drawControl.options.draw.rectangle).enable(); |
| 95 |
}; |
| 96 |
return container; |
| 97 |
} |
| 98 |
}); |
| 99 |
|
| 100 |
// Add the custom control to the map |
| 101 |
map.addControl(new CustomControl()); |
| 102 |
}; |
| 103 |
|
| 104 |
const setRectangle = (layer, fitBounds = true) => { |
| 105 |
editableLayers.addLayer(layer); |
| 106 |
|
| 107 |
// Set the zoom level |
| 108 |
if(fitBounds) { |
| 109 |
let zoom_map = map.getZoom(); |
| 110 |
map.fitBounds(layer.getBounds()); |
| 111 |
map.setZoom(zoom_map); |
| 112 |
} |
| 113 |
|
| 114 |
removeButtonControl.getContainer().style.display = 'block'; |
| 115 |
}; |
| 116 |
|
| 117 |
const event_rectange_changed = (coordinates = '') => { |
| 118 |
if(true) { |
| 119 |
$('.wdk-search-form .wdk-search-start[type="submit"]').trigger('click'); |
| 120 |
} |
| 121 |
}; |
| 122 |
|
| 123 |
const removeAllRectangles = () => { |
| 124 |
editableLayers.clearLayers(); |
| 125 |
removeButtonControl.getContainer().style.display = 'none'; |
| 126 |
|
| 127 |
// Clear the input fields |
| 128 |
$('.wdk-search-form').find('input[name="rectangle_ne"]').val(''); |
| 129 |
$('.wdk-search-form').find('input[name="rectangle_sw"]').val(''); |
| 130 |
|
| 131 |
removeOuther(); |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
const drawOuther = (bounds, fitBounds = true) => { |
| 136 |
//editableLayers.addLayer(layer); |
| 137 |
|
| 138 |
// Create a rectangle with the defined bounds |
| 139 |
var rectangle = [ |
| 140 |
[bounds[0][0], bounds[0][1]], |
| 141 |
[bounds[0][0], bounds[1][1]], |
| 142 |
[bounds[1][0], bounds[1][1]], |
| 143 |
[bounds[1][0], bounds[0][1]] |
| 144 |
]; |
| 145 |
|
| 146 |
// Create a polygon for the grey area covering the whole map |
| 147 |
var outerBounds = [ |
| 148 |
[[-90, -180], [90, -180], [90, 180], [-90, 180]], |
| 149 |
rectangle |
| 150 |
]; |
| 151 |
|
| 152 |
// Add the grey overlay with a hole in the middle where the rectangle is |
| 153 |
outGreyMask = L.polygon(outerBounds, { |
| 154 |
color: "grey", |
| 155 |
weight: 1, |
| 156 |
fillColor: "grey", |
| 157 |
fillOpacity: 0.4, |
| 158 |
interactive: false |
| 159 |
}).addTo(wdk_map); |
| 160 |
|
| 161 |
// Set the zoom level |
| 162 |
if(fitBounds) { |
| 163 |
let zoom_map = map.getZoom(); |
| 164 |
map.fitBounds(bounds); |
| 165 |
map.setZoom(zoom_map); |
| 166 |
} |
| 167 |
|
| 168 |
removeButtonControl.getContainer().style.display = 'block'; |
| 169 |
}; |
| 170 |
|
| 171 |
const removeOuther = () => { |
| 172 |
if(outGreyMask) { |
| 173 |
map.removeLayer(outGreyMask); |
| 174 |
outGreyMask = null; |
| 175 |
} |
| 176 |
}; |
| 177 |
|
| 178 |
// FeatureGroup is to store editable layers |
| 179 |
event_init(); |
| 180 |
|
| 181 |
return { |
| 182 |
setRectangle: setRectangle, |
| 183 |
removeOuther: removeOuther, |
| 184 |
drawOuther: drawOuther, |
| 185 |
removeAllRectangles: removeAllRectangles |
| 186 |
}; |
| 187 |
}; |
| 188 |
|