|
|
|
@ -16,12 +16,12 @@ |
|
|
|
|
|
|
|
import L, { |
|
|
|
FeatureGroup, |
|
|
|
Icon, |
|
|
|
Icon, LatLng, |
|
|
|
LatLngBounds, |
|
|
|
LatLngTuple, |
|
|
|
markerClusterGroup, |
|
|
|
MarkerClusterGroup, |
|
|
|
MarkerClusterGroupOptions |
|
|
|
MarkerClusterGroupOptions, Projection |
|
|
|
} from 'leaflet'; |
|
|
|
import tinycolor from 'tinycolor2'; |
|
|
|
import 'leaflet-providers'; |
|
|
|
@ -46,6 +46,7 @@ import { |
|
|
|
createTooltip, |
|
|
|
} from '@home/components/widget/lib/maps/maps-utils'; |
|
|
|
import { |
|
|
|
checkLngLat, |
|
|
|
createLoadingDiv, |
|
|
|
parseArray, |
|
|
|
parseData, |
|
|
|
@ -79,6 +80,8 @@ export default abstract class LeafletMap { |
|
|
|
updatePending = false; |
|
|
|
addMarkers: L.Marker[] = []; |
|
|
|
addPolygons: L.Polygon[] = []; |
|
|
|
southWest = new L.LatLng(-Projection.SphericalMercator['MAX_LATITUDE'], -180); |
|
|
|
northEast = new L.LatLng(Projection.SphericalMercator['MAX_LATITUDE'], 180); |
|
|
|
|
|
|
|
protected constructor(public ctx: WidgetContext, |
|
|
|
public $container: HTMLElement, |
|
|
|
@ -206,21 +209,30 @@ export default abstract class LeafletMap { |
|
|
|
|
|
|
|
addPolygonControl() { |
|
|
|
if (this.options.showPolygon && this.options.editablePolygon) { |
|
|
|
let mousePositionOnMap: L.LatLng[]; |
|
|
|
let polygonPoints: L.LatLng[]; |
|
|
|
let addPolygon: L.Control; |
|
|
|
let mousePositionOnMap: LatLng; |
|
|
|
this.map.on('mousemove', (e: L.LeafletMouseEvent) => { |
|
|
|
const polygonOffset = this.options.provider === MapProviders.image ? 10 : 0.01; |
|
|
|
const latlng1 = e.latlng; |
|
|
|
const latlng2 = L.latLng(e.latlng.lat, e.latlng.lng + polygonOffset); |
|
|
|
const latlng3 = L.latLng(e.latlng.lat - polygonOffset, e.latlng.lng); |
|
|
|
mousePositionOnMap = [latlng1, latlng2, latlng3]; |
|
|
|
mousePositionOnMap = e.latlng; |
|
|
|
}); |
|
|
|
|
|
|
|
const dragListener = (e: L.DragEndEvent) => { |
|
|
|
if (e.type === 'dragend' && mousePositionOnMap) { |
|
|
|
const newPolygon = L.polygon(mousePositionOnMap).addTo(this.map); |
|
|
|
if (e.type === 'dragend') { |
|
|
|
const polygonOffset = this.options.provider === MapProviders.image ? 10 : 0.01; |
|
|
|
|
|
|
|
let convert = this.convertToCustomFormat(mousePositionOnMap,polygonOffset); |
|
|
|
mousePositionOnMap.lat = convert[this.options.latKeyName]; |
|
|
|
mousePositionOnMap.lng = convert[this.options.lngKeyName]; |
|
|
|
|
|
|
|
const latlng1 = mousePositionOnMap; |
|
|
|
const latlng2 = L.latLng(mousePositionOnMap.lat, mousePositionOnMap.lng + polygonOffset); |
|
|
|
const latlng3 = L.latLng(mousePositionOnMap.lat - polygonOffset, mousePositionOnMap.lng); |
|
|
|
polygonPoints = [latlng1, latlng2, latlng3]; |
|
|
|
|
|
|
|
const newPolygon = L.polygon(polygonPoints).addTo(this.map); |
|
|
|
this.addPolygons.push(newPolygon); |
|
|
|
const datasourcesList = document.createElement('div'); |
|
|
|
const customLatLng = {[this.options.polygonKeyName]: this.convertToPolygonFormat(mousePositionOnMap)}; |
|
|
|
const customLatLng = {[this.options.polygonKeyName]: this.convertToPolygonFormat(polygonPoints)}; |
|
|
|
const header = document.createElement('p'); |
|
|
|
header.appendChild(document.createTextNode('Select entity:')); |
|
|
|
header.setAttribute('style', 'font-size: 14px; margin: 8px 0'); |
|
|
|
@ -414,12 +426,9 @@ export default abstract class LeafletMap { |
|
|
|
}).filter(el => !!el); |
|
|
|
} |
|
|
|
|
|
|
|
convertToCustomFormat(position: L.LatLng): object { |
|
|
|
if (position.lng > 180) { |
|
|
|
position.lng = 180; |
|
|
|
} else if (position.lng < -180) { |
|
|
|
position.lng = -180; |
|
|
|
} |
|
|
|
convertToCustomFormat(position: L.LatLng, offset = 0): object { |
|
|
|
position = checkLngLat(position, this.southWest, this.northEast, offset); |
|
|
|
|
|
|
|
return { |
|
|
|
[this.options.latKeyName]: position.lat, |
|
|
|
[this.options.lngKeyName]: position.lng |
|
|
|
@ -728,6 +737,11 @@ export default abstract class LeafletMap { |
|
|
|
if (e === undefined || (e.type !== 'editable:vertex:dragend' && e.type !== 'editable:vertex:deleted')) { |
|
|
|
return; |
|
|
|
} |
|
|
|
if(this.options.provider !== MapProviders.image) { |
|
|
|
for (let key in e.layer._latlngs[0]) { |
|
|
|
e.layer._latlngs[0][key] = checkLngLat(e.layer._latlngs[0][key], this.southWest, this.northEast); |
|
|
|
} |
|
|
|
} |
|
|
|
this.savePolygonLocation({ ...data, ...this.convertPolygonToCustomFormat(e.layer._latlngs) }).subscribe(); |
|
|
|
} |
|
|
|
|
|
|
|
|