Skip to content

Commit

Permalink
mmgisAPI Add selectFeature
Browse files Browse the repository at this point in the history
  • Loading branch information
tariqksoliman committed Nov 14, 2022
1 parent f643aec commit f025455
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 77 deletions.
35 changes: 35 additions & 0 deletions docs/pages/APIs/JavaScript/JavaScript_API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
130 changes: 53 additions & 77 deletions src/essence/Basics/Layers_/Layers_.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
Expand Down
28 changes: 28 additions & 0 deletions src/essence/mmgisAPI/mmgisAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,14 @@ var mmgisAPI_ = {

return null
},
selectFeature: function (layerName, options) {
return L_.selectPoint({
...{
layerName: layerName,
},
...options,
})
},
getActiveTool: function () {
if (ToolController_) {
return {
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit f025455

Please sign in to comment.