Add vector label example

This commit is contained in:
Alexandre Dubé
2014-02-27 14:41:56 -05:00
parent 6cd214c1e4
commit 0c63c76dea
2 changed files with 155 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
<!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="../css/ol.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css" type="text/css">
<link rel="stylesheet" href="../resources/layout.css" type="text/css">
<link rel="stylesheet" href="../resources/bootstrap/css/bootstrap-responsive.min.css" type="text/css">
<title>Vector labels example</title>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="./"><img src="../resources/logo.png"> OpenLayers 3 Examples</a>
</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="span12">
<h4 id="title">Vector labels example</h4>
<p id="shortdesc">Example of GeoJSON features with labels.</p>
<div id="docs">
<p>See the <a href="vector-labels.js" target="_blank">vector-labels.js source</a> to see how this is done.</p>
</div>
<div id="tags">geojson, vector, openstreetmap, label</div>
</div>
</div>
</div>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="../resources/example-behaviour.js" type="text/javascript"></script>
<script src="loader.js?id=vector-labels" type="text/javascript"></script>
</body>
</html>

104
examples/vector-labels.js Normal file
View File

@@ -0,0 +1,104 @@
goog.require('ol.Feature');
goog.require('ol.Map');
goog.require('ol.View2D');
goog.require('ol.layer.Tile');
goog.require('ol.layer.Vector');
goog.require('ol.source.GeoJSON');
goog.require('ol.source.MapQuest');
goog.require('ol.style.Circle');
goog.require('ol.style.Fill');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style');
goog.require('ol.style.Text');
// Polygons
var vectorPolygons = new ol.layer.Vector({
source: new ol.source.GeoJSON({
projection: 'EPSG:3857',
url: 'data/geojson/polygon-samples.geojson'
}),
style: function(feature, resolution) {
var style = new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'blue',
width: 1
}),
fill: new ol.style.Fill({
color: 'rgba(0, 0, 255, 0.1)'
}),
text: new ol.style.Text({
font: '12px Arial',
text: feature.getProperties().name,
fill: new ol.style.Fill({color: 'blue'}),
stroke: new ol.style.Stroke({color: 'white', width: 3})
})
});
return [style];
}
});
// Lines
var vectorLines = new ol.layer.Vector({
source: new ol.source.GeoJSON({
projection: 'EPSG:3857',
url: 'data/geojson/line-samples.geojson'
}),
style: function(feature, resolution) {
var style = new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'green',
width: 2
}),
text: new ol.style.Text({
font: '12px Arial',
text: feature.getProperties().name,
fill: new ol.style.Fill({color: 'green'}),
stroke: new ol.style.Stroke({color: 'white', width: 3})
})
});
return [style];
}
});
// Points
var vectorPoints = new ol.layer.Vector({
source: new ol.source.GeoJSON({
projection: 'EPSG:3857',
url: 'data/geojson/point-samples.geojson'
}),
style: function(feature, resolution) {
var style = new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
fill: new ol.style.Fill({color: 'rgba(255, 0, 0, 0.1)'}),
stroke: new ol.style.Stroke({color: 'red', width: 1})
}),
text: new ol.style.Text({
font: '12px Arial',
text: feature.getProperties().name,
fill: new ol.style.Fill({color: 'rgba(255, 0, 0, 1)'}),
stroke: new ol.style.Stroke({color: 'white', width: 3})
})
});
return [style];
}
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'osm'})
}),
vectorPolygons,
vectorLines,
vectorPoints
],
renderer: 'canvas',
target: 'map',
view: new ol.View2D({
center: [-8161939, 6095025],
zoom: 8
})
});