From aab5c8472c4377f98a84f1b9ee52a3e61a193307 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Wed, 26 Jun 2013 17:51:02 -0600 Subject: [PATCH] Update example to use expression parsing --- examples/kml-timezones.html | 2 +- examples/kml-timezones.js | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/examples/kml-timezones.html b/examples/kml-timezones.html index 56e0d8a92d..28ba047095 100644 --- a/examples/kml-timezones.html +++ b/examples/kml-timezones.html @@ -39,7 +39,7 @@

Timezones in KML

Demonstrates rendering timezones from KML.

-

This example parses a KML file and renders the features as a vector layer. Note that currently the kml:StyleMap elements are not parsed, so the layer is given a ol.style.Style manually. The timezones are filled yellow with an opacity calculated based on the current offset to local noon.

+

This example parses a KML file and renders the features as a vector layer. The layer is given a ol.style.Style that fills timezones yellow with an opacity calculated based on the current offset to local noon.

See the kml-timezones.js source to see how this is done.

KML, vector, style
diff --git a/examples/kml-timezones.js b/examples/kml-timezones.js index 9979d8196c..0dd9af9d1d 100644 --- a/examples/kml-timezones.js +++ b/examples/kml-timezones.js @@ -1,7 +1,7 @@ -goog.require('ol.Expression'); goog.require('ol.Map'); goog.require('ol.RendererHint'); goog.require('ol.View2D'); +goog.require('ol.expr'); goog.require('ol.layer.TileLayer'); goog.require('ol.layer.Vector'); goog.require('ol.parser.KML'); @@ -12,8 +12,15 @@ goog.require('ol.style.Polygon'); goog.require('ol.style.Rule'); goog.require('ol.style.Style'); -// calculate opacity based on difference from local noon -function getOpacity(feature) { + +/** + * Register a function to be used in a symbolizer. Here we want the opacity + * of polygons to be based on the offset from local noon. For example, a + * timezone where it is currently noon would have an opacity of 1. And a + * timezone where it is currently 6:00am would have an opacity of 0.5. + */ +ol.expr.register('getOpacity', function() { + var feature = this; var opacity = 0; var name = feature.get('name'); // e.g. GMT -08:30 var match = name.match(/([-+]\d{2}):(\d{2})$/); @@ -31,7 +38,7 @@ function getOpacity(feature) { opacity = 1 - offset / 12; } return opacity; -} +}); var style = new ol.style.Style({rules: [ new ol.style.Rule({ @@ -39,7 +46,7 @@ var style = new ol.style.Style({rules: [ new ol.style.Polygon({ fillColor: '#ffff33', strokeColor: '#ffffff', - opacity: new ol.Expression('getOpacity(this)') + opacity: ol.expr.parse('getOpacity()') }) ] })