From f0254555256c83610b73dd26e61ec3a980d5f915 Mon Sep 17 00:00:00 2001 From: Tariq Soliman Date: Mon, 14 Nov 2022 15:01:49 -0800 Subject: [PATCH] mmgisAPI Add selectFeature --- docs/pages/APIs/JavaScript/JavaScript_API.md | 35 +++++ src/essence/Basics/Layers_/Layers_.js | 130 ++++++++----------- src/essence/mmgisAPI/mmgisAPI.js | 28 ++++ 3 files changed, 116 insertions(+), 77 deletions(-) diff --git a/docs/pages/APIs/JavaScript/JavaScript_API.md b/docs/pages/APIs/JavaScript/JavaScript_API.md index 16a2e817..9016c1ef 100644 --- a/docs/pages/APIs/JavaScript/JavaScript_API.md +++ b/docs/pages/APIs/JavaScript/JavaScript_API.md @@ -498,6 +498,41 @@ The following is an example of how to call the `getActiveFeature` function: window.mmgisAPI.getActiveFeature(); ``` +### selectFeature + +This function selects a vector layer feature. It supports selections either from: + +- A longitude, latitude pair +- A key:value pair to match on (selects first found match) +- A leaflet layerId + +#### Function Parameters + +- `layerName` - _string_ - Name of the vector layer to select a feature in. +- `options` - _{}_ + - `layerId` - (optional) - A leaflet layer id + - `lon` - (optional) - Longitude - needs `lat` set + - `lat` - (optional) - Latitude - needs `lon` set + - `key` - (optional) - Feature `properties` key. Use dot-notation to choose nested keys. ('desserts.cakes.birthday.name') - needs `value` set + - `value` - (optional) - Value to match `key` - needs `key` set + - `view` - (optional) - If value is `"go"` pans and zooms to the feature + - `zoom` - (optional) - If set, this is the zoom level `view` will go to. + +The following is an example of how to call the `selectFeature` function: + +```javascript +window.mmgisAPI.selectFeature("Waypoints", { layerId: 600 }); + +window.mmgisAPI.selectFeature("Waypoints", { lon: 137, lat: -4 }); + +window.mmgisAPI.selectFeature("Waypoints", { + key: "sol", + value: 1159, + view: "go", + zoom: 14, +}); +``` + ### getVisibleLayers This function returns an object with the visibility state of all layers diff --git a/src/essence/Basics/Layers_/Layers_.js b/src/essence/Basics/Layers_/Layers_.js index 8b511c5b..fff8f3e4 100644 --- a/src/essence/Basics/Layers_/Layers_.js +++ b/src/essence/Basics/Layers_/Layers_.js @@ -94,14 +94,6 @@ var L_ = { init: function (configData, missionsList, urlOnLayers) { parseConfig(configData, urlOnLayers) L_.missionsList = missionsList - - setTimeout(() => { - console.log(L_.toggledArray['Waypoints']) - window.mmgisAPI.clearVectorLayer('Waypoints') - console.log(L_.toggledArray['Waypoints']) - window.mmgisAPI.reloadLayer('Waypoints') - console.log(L_.toggledArray['Waypoints']) - }, 4000) }, clear: function () { L_.mission = null @@ -1767,40 +1759,7 @@ var L_ = { g[l]._latlng.lng == activePoint.lon ) { g[l].fireEvent('click') - if (activePoint.view == 'go') { - let newView = [] - if (g[l]._latlng) { - newView = [ - g[l]._latlng.lat, - g[l]._latlng.lng, - activePoint.zoom || - L_.Map_.mapScaleZoom || - L_.Map_.map.getZoom(), - ] - } else if (g[l]._latlngs) { - let lat = 0, - lng = 0 - let llflat = g[l]._latlngs.flat(Infinity) - for (let ll of llflat) { - lat += ll.lat - lng += ll.lng - } - newView = [ - lat / llflat.length, - lng / llflat.length, - parseInt( - activePoint.zoom || - L_.Map_.mapScaleZoom || - L_.Map_.map.getZoom() - ), - ] - } - - L_.Map_.resetView(newView) - if (L_.hasGlobe) { - L_.Globe_.litho.setCenter(newView) - } - } + L_._selectPointViewHelper(activePoint, g[l]) return true } } @@ -1822,49 +1781,66 @@ var L_ = { ) == activePoint.value ) { g[l].fireEvent('click') - if (activePoint.view == 'go') { - let newView = [] - if (g[l]._latlng) { - newView = [ - g[l]._latlng.lat, - g[l]._latlng.lng, - activePoint.zoom || - L_.Map_.mapScaleZoom || - L_.Map_.map.getZoom(), - ] - } else if (g[l]._latlngs) { - let lat = 0, - lng = 0 - let llflat = g[l]._latlngs.flat(Infinity) - for (let ll of llflat) { - lat += ll.lat - lng += ll.lng - } - newView = [ - lat / llflat.length, - lng / llflat.length, - parseInt( - activePoint.zoom || - L_.Map_.mapScaleZoom || - L_.Map_.map.getZoom() - ), - ] - } - setTimeout(() => { - L_.Map_.resetView(newView) - }, 50) - if (L_.hasGlobe) { - L_.Globe_.litho.setCenter(newView) - } - } + L_._selectPointViewHelper(activePoint, g[l]) return true } } } } + } else if ( + activePoint && + activePoint.layerName != null && + activePoint.layerId != null + ) { + if (L_.layersGroup.hasOwnProperty(activePoint.layerName)) { + let g = L_.layersGroup[activePoint.layerName]._layers + const l = activePoint.layerId + if (g[l] != null) { + g[l].fireEvent('click') + L_._selectPointViewHelper(activePoint, g[l]) + return true + } + } } return false }, + _selectPointViewHelper: function (activePoint, layer) { + if (activePoint.view === 'go') { + let newView = [] + if (layer._latlng) { + newView = [ + layer._latlng.lat, + layer._latlng.lng, + activePoint.zoom || + L_.Map_.mapScaleZoom || + L_.Map_.map.getZoom(), + ] + } else if (layer._latlngs) { + let lat = 0, + lng = 0 + let llflat = layer._latlngs.flat(Infinity) + for (let ll of llflat) { + lat += ll.lat + lng += ll.lng + } + newView = [ + lat / llflat.length, + lng / llflat.length, + parseInt( + activePoint.zoom || + L_.Map_.mapScaleZoom || + L_.Map_.map.getZoom() + ), + ] + } + setTimeout(() => { + L_.Map_.resetView(newView) + }, 50) + if (L_.hasGlobe) { + L_.Globe_.litho.setCenter(newView) + } + } + }, reorderLayers: function (newLayersOrdered) { // Check that newLayersOrdered is valid let isValid = true diff --git a/src/essence/mmgisAPI/mmgisAPI.js b/src/essence/mmgisAPI/mmgisAPI.js index 625d6dd9..3d36e0d4 100644 --- a/src/essence/mmgisAPI/mmgisAPI.js +++ b/src/essence/mmgisAPI/mmgisAPI.js @@ -164,6 +164,14 @@ var mmgisAPI_ = { return null }, + selectFeature: function (layerName, options) { + return L_.selectPoint({ + ...{ + layerName: layerName, + }, + ...options, + }) + }, getActiveTool: function () { if (ToolController_) { return { @@ -472,6 +480,26 @@ var mmgisAPI = { */ getActiveFeature: mmgisAPI_.getActiveFeature, + /** Selects a feature based on latlng, key:value, or layerId + * @param {string} [layerName] + * @param {object} [options] + * options: { + lat: num, + lon: num, + || + key: 'props.dot.notation', + value: '', + || + layerId: num, + + view: 'go' || null, + zoom: 'zoomLevel' || 'map_scale_if_view_is_go', + } + * + * @returns {boolean} - true if found and selected a feature, otherwise false + */ + selectFeature: mmgisAPI_.selectFeature, + /** getActiveTool - returns the currently active tool * @returns {object} - The currently active tool and the name of the active tool as an object. */