Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bringing back the style-rules example

Features are now created programmatically as
GeoJSONFeatureCollection.
  • Loading branch information...
commit b971fc1aae4ee674238a8b241c71d3872f57a0e4 1 parent 389b8d1
Andreas Hocevar ahocevar authored
Showing with 188 additions and 0 deletions.
  1. +52 −0 examples/style-rules.html
  2. +136 −0 examples/style-rules.js
52 examples/style-rules.html
View
@@ -0,0 +1,52 @@
+<!doctype html>
+<html lang="en">
+ <head>
+ <meta charset="utf-8">
+ <meta http-equiv="X-UA-Compatible" content="chrome=1">
+ <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
+ <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css" type="text/css">
+ <link rel="stylesheet" href="examples.css" type="text/css">
+ <link rel="stylesheet" href="bootstrap/css/bootstrap-responsive.min.css" type="text/css">
+ <title>Style with rules example</title>
+ </head>
+ <body>
+
+ <div class="navbar navbar-inverse navbar-fixed-top">
+ <div class="navbar-inner">
+ <div class="container">
+ <a class="brand" href="example-list.html">OpenLayers 3 Examples</a>
+ <ul class="nav pull-right">
+ <li><a href="https://github.com/openlayers/ol3"><i class="icon-github"></i></a></li>
+ <li><a href="https://twitter.com/openlayers"><i class="icon-twitter"></i></a></li>
+ </ul>
+ </div>
+ </div>
+ </div>
+
+ <div class="container-fluid">
+
+ <div class="row-fluid">
+ <div class="span12">
+ <div id="map" class="map"></div>
+ </div>
+ </div>
+
+ <div class="row-fluid">
+
+ <div class="span4">
+ <h4 id="title">Style with rules example</h4>
+ <p id="shortdesc">Draws features with rule based styles.</p>
+ <div id="docs">
+ <p>See the <a href="style-rules.js" target="_blank">style-rules.js source</a> to see how this is done.</p>
+ </div>
+ <div id="tags">vector, geojson, style</div>
+ </div>
+
+ </div>
+
+ </div>
+
+ <script src="loader.js?id=style-rules" type="text/javascript"></script>
+
+ </body>
+</html>
136 examples/style-rules.js
View
@@ -0,0 +1,136 @@
+goog.require('ol.Collection');
+goog.require('ol.Coordinate');
+goog.require('ol.Expression');
+goog.require('ol.Feature');
+goog.require('ol.Map');
+goog.require('ol.RendererHint');
+goog.require('ol.View2D');
+goog.require('ol.control.defaults');
+goog.require('ol.filter.Filter');
+goog.require('ol.geom.LineString');
+goog.require('ol.layer.Vector');
+goog.require('ol.parser.GeoJSON');
+goog.require('ol.projection');
+goog.require('ol.source.Vector');
+goog.require('ol.style.Line');
+goog.require('ol.style.Rule');
+goog.require('ol.style.Style');
+
+
+var style = new ol.style.Style({rules: [
+ new ol.style.Rule({
+ filter: new ol.filter.Filter(function(feature) {
+ return feature.get('where') == 'outer';
+ }),
+ symbolizers: [
+ new ol.style.Line({
+ strokeColor: new ol.Expression('color'),
+ strokeWidth: 4,
+ opacity: 1
+ })
+ ]
+ }),
+ new ol.style.Rule({
+ filter: new ol.filter.Filter(function(feature) {
+ return feature.get('where') == 'inner';
+ }),
+ symbolizers: [
+ new ol.style.Line({
+ strokeColor: '#013',
+ strokeWidth: 4,
+ opacity: 1
+ }),
+ new ol.style.Line({
+ strokeColor: new ol.Expression('color'),
+ strokeWidth: 2,
+ opacity: 1
+ })
+ ]
+ })
+]});
+
+var vector = new ol.layer.Vector({
+ style: style,
+ source: new ol.source.Vector({
+ projection: ol.projection.get('EPSG:3857')
+ })
+});
+
+vector.parseFeatures({
+ 'type': 'FeatureCollection',
+ 'features': [{
+ 'type': 'Feature',
+ 'properties': {
+ 'color': '#BADA55',
+ 'where': 'inner'
+ },
+ 'geometry': {
+ 'type': 'LineString',
+ 'coordinates': [[-10000000, -10000000], [10000000, 10000000]]
+ }
+ }, {
+ 'type': 'Feature',
+ 'properties': {
+ 'color': '#BADA55',
+ 'where': 'inner'
+ },
+ 'geometry': {
+ 'type': 'LineString',
+ 'coordinates': [[-10000000, 10000000], [10000000, -10000000]]
+ }
+ }, {
+ 'type': 'Feature',
+ 'properties': {
+ 'color': '#013',
+ 'where': 'outer'
+ },
+ 'geometry': {
+ 'type': 'LineString',
+ 'coordinates': [[-10000000, -10000000], [-10000000, 10000000]]
+ }
+ }, {
+ 'type': 'Feature',
+ 'properties': {
+ 'color': '#013',
+ 'where': 'outer'
+ },
+ 'geometry': {
+ 'type': 'LineString',
+ 'coordinates': [[-10000000, 10000000], [10000000, 10000000]]
+ }
+ }, {
+ 'type': 'Feature',
+ 'properties': {
+ 'color': '#013',
+ 'where': 'outer'
+ },
+ 'geometry': {
+ 'type': 'LineString',
+ 'coordinates': [[10000000, 10000000], [10000000, -10000000]]
+ }
+ }, {
+ 'type': 'Feature',
+ 'properties': {
+ 'color': '#013',
+ 'where': 'outer'
+ },
+ 'geometry': {
+ 'type': 'LineString',
+ 'coordinates': [[10000000, -10000000], [-10000000, -10000000]]
+ }
+ }]
+}, new ol.parser.GeoJSON(), ol.projection.get('EPSG:3857'));
+
+
+var map = new ol.Map({
+ layers: new ol.Collection([vector]),
+ controls: ol.control.defaults({
+ attribution: false
+ }),
+ renderer: ol.RendererHint.CANVAS,
+ target: 'map',
+ view: new ol.View2D({
+ center: new ol.Coordinate(0, 0),
+ zoom: 1
+ })
+});
Please sign in to comment.
Something went wrong with that request. Please try again.