UNPKG

@geogirafe/lib-geoportal

Version:

GeoGirafe is a flexible application to build online geoportals.

54 lines (53 loc) 2.02 kB
// SPDX-License-Identifier: Apache-2.0 import GirafeSingleton from '../../base/GirafeSingleton.js'; import OlMap from 'ol/Map.js'; import { defaults as defaultControls } from 'ol/control/defaults.js'; import { defaults as defaultInteractions } from 'ol/interaction/defaults.js'; import { DragPan } from 'ol/interaction.js'; import { Kinetic } from 'ol'; import { v4 as uuidv4 } from 'uuid'; /** The singleton containing the main OpenLayers map accessible from everywhere */ export default class MapManager extends GirafeSingleton { map; createMap() { const interactions = defaultInteractions({ dragPan: false }); interactions.push(new DragPan({ condition: function (mapBrowserEvent) { const event = mapBrowserEvent.originalEvent; return event.isPrimary && event.button < 2 && !event.shiftKey; }, kinetic: new Kinetic(-0.005, 0.05, 100) })); this.map = new OlMap({ controls: defaultControls({ rotate: !this.context.stateManager.state.interface.isMobile, zoom: !this.context.stateManager.state.interface.isMobile }), layers: [], interactions: interactions, moveTolerance: 5 }); // Needed to identify the map instance for reusing Overlay (or not) in the context menu (see // CustomContextMenu.contextMenuOverlayByMap) this.map.set('uuid', uuidv4()); } getMap() { if (!this.map) { this.createMap(); } return this.map; } /** * @returns an array of BaseLayer objects that should be printed and that are not in the layer tree. */ getLayersToPrint() { return this.getMap() .getAllLayers() .filter((layer) => layer.get('addToPrintedLayers')); } zoomToExtent(extent, minResolution) { this.getMap().getView().fit(extent, { minResolution: minResolution }); } }