Skip to content

Instantly share code, notes, and snippets.

@dbauszus-glx
Created July 23, 2019 12:41
Show Gist options
  • Save dbauszus-glx/9560f18aa0c11a6ca641280f3728652c to your computer and use it in GitHub Desktop.
Save dbauszus-glx/9560f18aa0c11a6ca641280f3728652c to your computer and use it in GitHub Desktop.
_xyz.mapview.highlight = {
feature: null,
layer: null,
};
function pointermove(e){
// Get features from layers which have a highlight style.
const featureArray = _xyz.map.getFeaturesAtPixel(e.pixel,{
// Filter for layers which have a highlight style.
layerFilter: featureLayer => {
return Object.values(_xyz.layers.list).some(layer => {
return layer.style && layer.style.highlight && layer.L === featureLayer;
});
},
hitTolerance: 0,
});
// Return if no features are found.
if (!featureArray) return;
// The first feature in the array will be the feature with the highest zIndex.
const topFeature = featureArray[0];
// Return is feature is already highlighted.
if (_xyz.mapview.highlight.feature === topFeature) return;
// Redraw layer with previous highlighted feature.
if (_xyz.mapview.highlight.layer) {
_xyz.mapview.highlight.layer.highlight = true;
_xyz.mapview.highlight.layer.L.setStyle(_xyz.mapview.highlight.layer.L.getStyle());
}
// Iterate through all features (with layer) at pixel
_xyz.map.forEachFeatureAtPixel(e.pixel, (feature, featureLayer) => {
if (feature === topFeature) {
// Iterate through layers list.
Object.values(_xyz.layers.list).some(layer => {
// Return false to continue the arrays some method if the featureLayer does not match the current layers Openlayers object layer.L
if (layer.L !== featureLayer) return false;
// Assign feature id to the layer object.
layer.highlight = feature.get('id');
// Store layer and feature reference on the mapview object.
_xyz.mapview.highlight.feature = feature;
_xyz.mapview.highlight.layer = layer;
// Redraw layer to style highlight.
return layer.L.setStyle(layer.L.getStyle());
});
}
},{
layerFilter: featureLayer => {
// Filter for layers which have a highlight style.
return Object.values(_xyz.layers.list).some(layer => {
return layer.style && layer.style.highlight && layer.L === featureLayer;
});
},
hitTolerance: 0,
});
};
_xyz.map.on('pointermove', pointermove);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment