Add controls to OpenLayers map
[gpl/argeo-suite.git] / js / src / geo / OpenLayersMapPart.js
index 71b9a17b0574dd1539e1eb00c4cc5fa7823598c6..160930d6b7447563b309de34030dcf808d24b00a 100644 (file)
@@ -2,32 +2,38 @@
  * @module OpenLayersMapPart
  */
 
-import Map from 'ol/Map.js';
-import View from 'ol/View.js';
-import OSM from 'ol/source/OSM.js';
-import TileLayer from 'ol/layer/Tile.js';
-import { fromLonLat, getPointResolution, transformExtent } from 'ol/proj.js';
-import VectorSource from 'ol/source/Vector.js';
+import { fromLonLat, getPointResolution } from 'ol/proj.js';
 import Feature from 'ol/Feature.js';
 import { Point } from 'ol/geom.js';
+import { transformExtent } from 'ol/proj.js';
+
 import VectorLayer from 'ol/layer/Vector.js';
+import TileLayer from 'ol/layer/Tile.js';
+
+import VectorSource from 'ol/source/Vector.js';
 import GeoJSON from 'ol/format/GeoJSON.js';
 import GPX from 'ol/format/GPX.js';
+import OSM from 'ol/source/OSM.js';
+
 import Select from 'ol/interaction/Select.js';
 import Overlay from 'ol/Overlay.js';
 import { Style, Icon } from 'ol/style.js';
 
+import Map from 'ol/Map.js';
+import View from 'ol/View.js';
+import { OverviewMap, ScaleLine, defaults as defaultControls } from 'ol/control.js';
+
 import * as SLDReader from '@nieuwlandgeo/sldreader';
 
 import MapPart from './MapPart.js';
 
-import { bbox } from 'ol/loadingstrategy';
-
 /** OpenLayers implementation of MapPart. */
 export default class OpenLayersMapPart extends MapPart {
        /** The OpenLayers Map. */
        #map;
 
+       #overviewMap;
+
        /** Styled layer descriptor */
        #sld;
 
@@ -37,16 +43,25 @@ export default class OpenLayersMapPart extends MapPart {
        /** Constructor taking the mapName as an argument. */
        constructor(mapName) {
                super(mapName);
+               this.#overviewMap = new OverviewMap({
+                       layers: [
+                               new TileLayer({
+                                       source: new OSM(),
+                               }),
+                       ],
+               });
                this.#map = new Map({
+                       controls: defaultControls({
+                               attribution: false,
+                               rotate: false,
+                       }).extend([this.#overviewMap, new ScaleLine({
+                               bar: false,
+                               steps: 2,
+                               text: false,
+                               minWidth: 150,
+                               maxWidth: 200,
+                       })]),
                        layers: [
-                               //                              new TileLayer({
-                               //                                      source: new SentinelCloudless(),
-                               //                              }),
-                               //                                                              new TileLayer({
-                               //                                                                      source: new OSM(),
-                               //                                                                      opacity: 0.4,
-                               //                                                                      transition: 0,
-                               //                                                              }),
                        ],
                        //                                              view: new View({
                        //                                                      projection: 'EPSG:4326',
@@ -64,8 +79,13 @@ export default class OpenLayersMapPart extends MapPart {
                this.#map.getView().setZoom(zoom);
        }
 
-       setCenter(lng, lat) {
-               this.#map.getView().setCenter(fromLonLat([lng, lat]));
+       setCenter(lat, lon) {
+               this.#map.getView().setCenter(fromLonLat([lon, lat]));
+       }
+
+       fit(extent, options) {
+               var transformed = transformExtent(extent, 'EPSG:4326', this.#map.getView().getProjection());
+               this.#map.getView().fit(transformed, options);
        }
 
        addPoint(lng, lat, style) {
@@ -286,44 +306,4 @@ export default class OpenLayersMapPart extends MapPart {
                });
                vectorLayer.setStyle(olStyleFunction);
        }
-
-       //
-       // BBOX
-       //
-       applyBboxStrategy(layerName) {
-               const layer = this.getLayerByName(layerName);
-               const vectorSource = layer.getSource();
-               const baseUrl = vectorSource.getUrl();
-               if (typeof baseUrl === 'function')
-                       throw new Error('A strategy was already applied to layer ' + layerName);
-
-               const loadFunction = function(extent, resolution, projection, success, failure) {
-
-                       const proj = projection.getCode();
-                       var bbox = transformExtent(extent, proj, 'EPSG:4326');
-
-                       const url = baseUrl + '&' +
-                               'bbox=' + bbox.join(',') ;
-//                             'bbox=' + extent.join(',') + ',' + proj;
-                       const xhr = new XMLHttpRequest();
-                       xhr.open('GET', url);
-                       const onError = function() {
-                               vectorSource.removeLoadedExtent(extent);
-                               failure();
-                       }
-                       xhr.onerror = onError;
-                       xhr.onload = function() {
-                               if (xhr.status == 200) {
-                                       const features = vectorSource.getFormat().readFeatures(xhr.responseText);
-                                       vectorSource.addFeatures(features);
-                                       success(features);
-                               } else {
-                                       onError();
-                               }
-                       }
-                       xhr.send();
-               }
-
-               vectorSource.setLoader(loadFunction);
-       }
 }