@hexide-digital/geoportal.lt
Version:
Set of libs for geoportal.lt
112 lines (111 loc) • 3.29 kB
JavaScript
import { clone, find, merge, remove } from 'lodash';
export default class Drawer {
constructor(options) {
this.options = {
markerIcon: null,
pointZoom: 16,
directionMarkerStyle: {
radius: 5,
color: '#ea4106',
fillColor: 'white',
opacity: 1,
fillOpacity: 0.7
},
};
this.paths = [];
this.points = [];
this.options = merge({}, this.options, options);
}
addPoint(point) {
if (!this.map) {
throw new Error('Map Object not found. Add map to drawer first.');
}
const p = clone(point);
// @ts-ignore
const marker = L.marker(p, { icon: this.options.markerIcon }).addTo(this.pointersLayer);
this.map.panTo(p).setView(p); // , this.options.pointZoom
p.marker = marker;
this.points.push(p);
return this;
}
panTo(location, zoom = null) {
if (!this.map) {
throw new Error('Map Object not found. Add map to drawer first.');
}
if (zoom) {
this.map.setView(location, zoom);
}
else {
this.map.panTo(location);
}
return this;
}
showDirectionMarker(location) {
if (!this.map) {
throw new Error('Map Object not found. Add map to drawer first.');
}
const p = clone(location);
// @ts-ignore
L.circleMarker(p, this.options.directionMarkerStyle).addTo(this.directionMarkersLayer);
return this;
}
hideDirectionMarker(location) {
if (!this.map) {
throw new Error('Map Object not found. Add map to drawer first.');
}
this.directionMarkersLayer.clearLayers();
return this;
}
addPoints(points) {
points.forEach((point) => {
this.addPoint(point);
});
return this;
}
removePoint(point) {
if (!this.map) {
throw new Error('Map Object not found. Add map to drawer first.');
}
const marker = find(this.points, (el) => {
return el.lat === point.lat && el.lng === point.lng;
});
if (marker) {
this.pointersLayer.removeLayer(marker.marker);
remove(this.points, (el) => {
return el === marker;
});
}
return this;
}
clear() {
if (this.map) {
this.pointersLayer.clearLayers();
this.pathLayer.clearLayers();
}
this.points = [];
this.paths = [];
return this;
}
draw(path) {
if (!this.map) {
throw new Error('Map Object not found. Add map to drawer first.');
}
this.paths.push(path);
// @ts-ignore
L.geoJSON(path).addTo(this.pathLayer);
return this;
}
setMap(map) {
this.map = map;
// @ts-ignore
this.pointersLayer = L.layerGroup().addTo(map);
// @ts-ignore
this.pathLayer = L.layerGroup().addTo(map);
// @ts-ignore
this.directionMarkersLayer = L.layerGroup().addTo(map);
return this;
}
onMap(map) {
return this.setMap(map);
}
}