From 643c981074b8b63fc8ddc13220b7af92a54d0168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89ric=20Lemoine?= Date: Tue, 7 Jan 2014 14:11:54 +0100 Subject: [PATCH] Use FeaturesOverlay in vector-layer example This demonstrates how to use ol.FeaturesOverlay for feature highlighting. --- examples/vector-layer.html | 2 +- examples/vector-layer.js | 49 +++++++++++++++++++++----------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/examples/vector-layer.html b/examples/vector-layer.html index 038a1b9eea..49608db4d4 100644 --- a/examples/vector-layer.html +++ b/examples/vector-layer.html @@ -36,7 +36,7 @@

See the vector-layer.js source to see how this is done.

-
vector, geojson, style
+
vector, geojson, style, features overlay
diff --git a/examples/vector-layer.js b/examples/vector-layer.js index adda211076..413837f406 100644 --- a/examples/vector-layer.js +++ b/examples/vector-layer.js @@ -3,6 +3,7 @@ goog.require('ol.RendererHint'); goog.require('ol.View2D'); goog.require('ol.layer.Tile'); goog.require('ol.layer.Vector'); +goog.require('ol.render.FeaturesOverlay'); goog.require('ol.source.GeoJSON'); goog.require('ol.source.MapQuestOpenAerial'); goog.require('ol.style.Fill'); @@ -44,9 +45,25 @@ var map = new ol.Map({ }) }); +var highlightStyleArray = [new ol.style.Style({ + stroke: new ol.style.Stroke({ + color: '#f00', + width: 1 + }), + fill: new ol.style.Fill({ + color: 'rgba(255,0,0,0.1)' + }) +})]; + +var featuresOverlay = new ol.render.FeaturesOverlay({ + map: map, + styleFunction: function(feature, resolution) { + return highlightStyleArray; + } +}); + var highlight; var displayFeatureInfo = function(pixel) { - var oldHighlight = highlight; var feature = map.forEachFeatureAtPixel(pixel, function(feature, layer) { return feature; @@ -59,11 +76,16 @@ var displayFeatureInfo = function(pixel) { info.innerHTML = ' '; } - highlight = feature; - - if (highlight !== oldHighlight) { - map.requestRenderFrame(); + if (feature !== highlight) { + if (highlight) { + featuresOverlay.removeFeature(highlight); + } + if (feature) { + featuresOverlay.addFeature(feature); + } + highlight = feature; } + }; $(map.getViewport()).on('mousemove', function(evt) { @@ -75,20 +97,3 @@ map.on('singleclick', function(evt) { var pixel = evt.getPixel(); displayFeatureInfo(pixel); }); - -var highlightStyle = new ol.style.Style({ - stroke: new ol.style.Stroke({ - color: '#f00', - width: 1 - }), - fill: new ol.style.Fill({ - color: 'rgba(255,0,0,0.1)' - }) -}); - -map.on('postcompose', function(evt) { - if (highlight) { - var render = evt.getRender(); - render.drawFeature(highlight, highlightStyle); - } -});