From 81eed8ea01759bd67f4ae9f6de970b899c303d88 Mon Sep 17 00:00:00 2001 From: Tom Payne Date: Thu, 23 Jan 2014 15:09:47 +0100 Subject: [PATCH] Add text to vector-layer example --- examples/vector-layer.js | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/examples/vector-layer.js b/examples/vector-layer.js index 737b37f3be..e9de0cd6bd 100644 --- a/examples/vector-layer.js +++ b/examples/vector-layer.js @@ -9,24 +9,39 @@ goog.require('ol.source.MapQuest'); goog.require('ol.style.Fill'); goog.require('ol.style.Stroke'); goog.require('ol.style.Style'); +goog.require('ol.style.Text'); -var styleArray = [new ol.style.Style({ - fill: new ol.style.Fill({ - color: 'rgba(255, 255, 255, 0.6)' - }), - stroke: new ol.style.Stroke({ - color: '#319FD3', - width: 1 - }) -})]; - +var styleCache = {}; var vectorLayer = new ol.layer.Vector({ source: new ol.source.GeoJSON({ url: 'data/geojson/countries.geojson' }), styleFunction: function(feature, resolution) { - return styleArray; + var text = resolution < 5000 ? feature.get('name') : ''; + if (!styleCache[text]) { + styleCache[text] = [new ol.style.Style({ + fill: new ol.style.Fill({ + color: 'rgba(255, 255, 255, 0.6)' + }), + stroke: new ol.style.Stroke({ + color: '#319FD3', + width: 1 + }), + text: new ol.style.Text({ + font: '12px Calibri,sans-serif', + text: text, + fill: new ol.style.Fill({ + color: '#000' + }), + stroke: new ol.style.Stroke({ + color: '#fff', + width: 3 + }) + }) + })]; + } + return styleCache[text]; } });