Example using an icon symbolizer and displaying a popup

I'm adding this example to demonstrate a few things that should change:

 * the icon symbolizer should accept xOffset and yOffset in pixels
 * the `ol.geom.Geometry.prototype.getCoordinates` method should be exported
 * overlays should have a `panIntoView` method
This commit is contained in:
Tim Schaub
2013-08-26 16:56:15 -06:00
parent 700bb64d2d
commit 9c824584b3
3 changed files with 158 additions and 0 deletions

67
examples/icon.html Normal file
View File

@@ -0,0 +1,67 @@
<!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 Icon Example</title>
<style>
#map {
position: relative;
}
#popup {
padding-bottom: 65px;
}
</style>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="./">OpenLayers 3 Examples</a>
<ul class="nav pull-right">
<li><iframe class="github-watch-button" src="http://ghbtns.com/github-btn.html?user=openlayers&repo=ol3&type=watch&count=true"
allowtransparency="true" frameborder="0" scrolling="0" height="20" width="90"></iframe></li>
<li><a href="https://twitter.com/share" class="twitter-share-button" data-count="none" data-hashtags="openlayers">&nbsp;</a></li>
<li><div class="g-plusone-wrapper"><div class="g-plusone" data-size="medium" data-annotation="none"></div></div></li>
</ul>
</div>
</div>
</div>
<div class="container-fluid">
<div class="row-fluid">
<div class="span12">
<div id="map" class="map"><div id="info"></div></div>
<div id="popup"><div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<h4 id="title">Icon example</h4>
<p id="shortdesc">Example using an icon to symbolize a point.</p>
<div id="docs">
<p>See the <a href="icon.js" target="_blank">icon.js source</a> to see how this is done.</p>
</div>
<div id="tags">vector, style, icon, marker, popup</div>
</div>
</div>
</div>
<script src="jquery.min.js" type="text/javascript"></script>
<script src="../resources/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="loader.js?id=icon" type="text/javascript"></script>
<script src="../resources/social-links.js" type="text/javascript"></script>
</body>
</html>

91
examples/icon.js Normal file
View File

@@ -0,0 +1,91 @@
goog.require('ol.Map');
goog.require('ol.Overlay');
goog.require('ol.RendererHint');
goog.require('ol.View2D');
goog.require('ol.layer.TileLayer');
goog.require('ol.layer.Vector');
goog.require('ol.parser.GeoJSON');
goog.require('ol.source.TileJSON');
goog.require('ol.source.Vector');
goog.require('ol.style.Icon');
goog.require('ol.style.Style');
var raster = new ol.layer.TileLayer({
source: new ol.source.TileJSON({
url: 'http://api.tiles.mapbox.com/v3/mapbox.geography-class.jsonp'
})
});
var data = {
type: 'FeatureCollection',
features: [{
type: 'Feature',
properties: {
name: 'Null Island',
population: 4000,
rainfall: 500
},
geometry: {
type: 'Point',
coordinates: [0, 0]
}
}]
};
var style = new ol.style.Style({
symbolizers: [
new ol.style.Icon({
url: 'icon.png'
})
]
});
var vector = new ol.layer.Vector({
source: new ol.source.Vector({
parser: new ol.parser.GeoJSON(),
data: data
}),
style: style
});
var map = new ol.Map({
layers: [raster, vector],
renderer: ol.RendererHint.CANVAS,
target: 'map',
view: new ol.View2D({
center: [0, 0],
zoom: 3
})
});
var element = document.getElementById('popup');
var popup = new ol.Overlay({
map: map,
element: element
});
map.on('click', function(evt) {
map.getFeatures({
pixel: evt.getPixel(),
layers: [vector],
success: function(layerFeatures) {
var feature = layerFeatures[0][0];
if (feature) {
var geometry = feature.getGeometry();
var coord = geometry.getCoordinates();
popup.setPosition(coord);
$(element).popover({
'placement': 'top',
'html': true,
'content': feature.get('name')
});
$(element).popover('show');
} else {
$(element).popover('hide');
}
}
});
});

BIN
examples/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB