Basic popup example

This commit is contained in:
Tim Schaub
2013-08-28 12:28:47 -06:00
parent e93383c859
commit 7f66376315
2 changed files with 161 additions and 0 deletions

97
examples/popup.html Normal file
View File

@@ -0,0 +1,97 @@
<!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">
<style type="text/css">
#vienna {
text-decoration: none;
color: white;
font-size: 11pt;
font-weight: bold;
text-shadow: black 0.1em 0.1em 0.2em;
}
#popup-container {
background-color: white;
padding: 15px 20px;
border-radius: 10px;
margin-left: -30px;
margin-bottom: 10px;
}
#popup-closer {
position: absolute;
top: 2px;
right: 8px;
}
#popup-arrow {
position: absolute;
bottom: 1px;
width: 0px;
height: 0px;
border-left: 10px solid transparent;
border-right: 10px solid transparent;
border-top: 10px solid white;
}
</style>
<title>Overlay example</title>
</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="popup-container">
<a href="#" id="popup-closer"></a>
<div id="popup-content"></div>
<div id="popup-arrow"></div>
</div>
</div>
</div>
</div>
<div class="row-fluid">
<div class="span12">
<h4 id="title">Popup example</h4>
<p id="shortdesc">Uses an overlay to create a popup.</p>
<div id="docs">
<p>
Click on the map to get a popup. The popup is composed of a few basic elements: a container, a close button, a place for the content, and an element to represent the little arrow at the bottom. To anchor the popup to the map, an <code>ol.Overlay</code> is created with the popup container. A listener is registered for the map's <code>click</code> event to render the popup, and another listener is set as the <code>click</code> handler for the close button to hide the popup.
</p>
<p>
See the <a href="popup.js" target="_blank">popup.js source</a> to see how this is done.
</p>
</div>
<div id="tags">overlay, popup, mapquest, openaerial</div>
</div>
</div>
</div>
<script src="loader.js?id=popup" type="text/javascript"></script>
<script src="../resources/social-links.js" type="text/javascript"></script>
</body>
</html>

64
examples/popup.js Normal file
View File

@@ -0,0 +1,64 @@
goog.require('ol.Map');
goog.require('ol.Overlay');
goog.require('ol.View2D');
goog.require('ol.coordinate');
goog.require('ol.layer.TileLayer');
goog.require('ol.proj');
goog.require('ol.source.MapQuestOpenAerial');
var layer = new ol.layer.TileLayer({
source: new ol.source.MapQuestOpenAerial()
});
var map = new ol.Map({
layers: [layer],
target: 'map',
view: new ol.View2D({
center: [0, 0],
zoom: 2
})
});
/**
* Elements that make up the popup.
*/
var container = document.getElementById('popup-container');
var content = document.getElementById('popup-content');
var closer = document.getElementById('popup-closer');
/**
* Add a click handler to hide the popup.
* @return {boolean} Don't follow the href.
*/
closer.onclick = function() {
container.style.visibility = 'hidden';
return false;
};
/**
* Create an overlay to anchor the popup to the map.
*/
var overlay = new ol.Overlay({
map: map,
element: container
});
/**
* Add a click handler to the map to render the popup.
*/
map.on('click', function(evt) {
var coordinate = evt.getCoordinate();
var hdms = ol.coordinate.toStringHDMS(ol.proj.transform(
coordinate, 'EPSG:3857', 'EPSG:4326'));
overlay.setPosition(coordinate);
content.innerHTML = '<p>The location you clicked was:</p><code>' + hdms +
'</code>';
container.style.visibility = 'visible';
});