Highlight features in vector-layer example
This commit is contained in:
@@ -53,13 +53,20 @@ $.getJSON('data/countries.geojson', function(data) {
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var highlight;
|
||||||
var displayFeatureInfo = function(pixel) {
|
var displayFeatureInfo = function(pixel) {
|
||||||
var coordinate = map.getCoordinateFromPixel(pixel);
|
var coordinate = map.getCoordinateFromPixel(pixel);
|
||||||
var features = vectorSource.getAllFeaturesAtCoordinate(coordinate);
|
var features = vectorSource.getAllFeaturesAtCoordinate(coordinate);
|
||||||
var innerHTML = features.length ?
|
var info = document.getElementById('info');
|
||||||
features[0].getId() + ': ' + features[0].get('name') :
|
if (features.length > 0) {
|
||||||
' ';
|
var feature = features[0];
|
||||||
document.getElementById('info').innerHTML = innerHTML;
|
info.innerHTML = feature.getId() + ': ' + features[0].get('name');
|
||||||
|
highlight = feature;
|
||||||
|
} else {
|
||||||
|
info.innerHTML = ' ';
|
||||||
|
highlight = undefined;
|
||||||
|
}
|
||||||
|
map.render();
|
||||||
};
|
};
|
||||||
|
|
||||||
$(map.getViewport()).on('mousemove', function(evt) {
|
$(map.getViewport()).on('mousemove', function(evt) {
|
||||||
@@ -71,3 +78,18 @@ map.on('singleclick', function(evt) {
|
|||||||
var pixel = evt.getPixel();
|
var pixel = evt.getPixel();
|
||||||
displayFeatureInfo(pixel);
|
displayFeatureInfo(pixel);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
map.on('postcompose', function(evt) {
|
||||||
|
if (highlight) {
|
||||||
|
var render = evt.getRender();
|
||||||
|
render.drawFeature(highlight, {
|
||||||
|
stroke: {
|
||||||
|
color: '#f00',
|
||||||
|
width: 1
|
||||||
|
},
|
||||||
|
fill: {
|
||||||
|
color: 'rgba(255,0,0,0.1)'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user