Introduce fit to map layer
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 18 Oct 2023 09:39:02 +0000 (11:39 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 18 Oct 2023 09:39:02 +0000 (11:39 +0200)
js/src/geo/OpenLayersMapPart.js
org.argeo.app.geo/src/org/argeo/app/geo/ux/OpenLayersMapPart.java
swt/org.argeo.app.swt/src/org/argeo/app/swt/js/SwtBrowserJsPart.java

index 6fedd7a2a382f9f6d8b77de9cbfae761a96b72cb..4be5987b831f7d7617e9511b2f21449c0d1f805f 100644 (file)
@@ -14,6 +14,7 @@ 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 { isEmpty } from 'ol/extent';
 
 import Select from 'ol/interaction/Select.js';
 import Overlay from 'ol/Overlay.js';
@@ -22,6 +23,7 @@ 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 { easeOut } from 'ol/easing';
 
 import * as SLDReader from '@nieuwlandgeo/sldreader';
 
@@ -260,6 +262,24 @@ export default class OpenLayersMapPart extends MapPart {
                }
        }
 
+       fitToLayer(layerName) {
+               // we cannot use 'this' in the function provided to OpenLayers
+               let mapPart = this;
+               const layer = this.getLayerByName(layerName);
+               const source = layer.getSource();
+               const extent = source.getExtent();
+               const options = {
+                       duration: 1000,
+                       padding: [20, 20, 20, 20],
+                       easing: easeOut,
+               };
+               if (!isEmpty(extent))
+                       this.#map.getView().fit(source.getExtent(), options);
+               source.on('featuresloadend', function(e) {
+                       mapPart.getMap().getView().fit(source.getExtent(), options);
+               });
+       }
+
        //
        // STATIC FOR EXTENSION
        //
index 512a473c080f5bc906397757a0478209211cf138..6b5c9263c634ec739ea2d952c7ed49bfa33f2a49 100644 (file)
@@ -72,6 +72,10 @@ public class OpenLayersMapPart extends AbstractGeoJsObject {
                executeMethod(getMethodName(), layerName, (Object[]) ids);
        }
 
+       public void fitToLayer(String layerName) {
+               executeMethod(getMethodName(), layerName);
+       }
+
        /*
         * CALLBACKS
         */
index ec359c6865de427b7532d7978afef4147f9dd7a5..41cc3afe2ddbf587b51f3695d7c5528033839dd2 100644 (file)
@@ -85,20 +85,13 @@ public class SwtBrowserJsPart implements JsClient {
        }
 
        /**
-        * To be overridden with calls to {@link #loadExtension( Supplier<Boolean> toDo
-        * = () -> { boolean success = browser.execute(); return success; }; String)}.
+        * To be overridden with calls to {@link #loadExtension(String)}.
         */
        protected void loadExtensions() {
 
        }
 
        protected void loadExtension(String url) {
-//                     String js = """
-//                                     var script = document.createElement("script");
-//                                     script.src = '%s';
-//                                     document.head.appendChild(script);
-//                                     """;
-//                     browser.evaluate(String.format(Locale.ROOT, js, url));
                browser.evaluate(String.format(Locale.ROOT, "import('%s')", url));
        }