OpenLayers 3 Beginner’s Guide

Introducing ol.Overlay with a static example

Open sample in a new windows

<!doctype html>
<html>
  <head>
    <title>Simple Overlay</title>
    <link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
    <link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
  </head>
  <body>
    <div id="map" class="map">
      <div id="popup"><b>OpenLayers 3 Code Sprint</b> <i>Humanities A3</i></div>
    </div>
    <script src="../assets/ol3/js/ol.js"></script>
    <script>

      var popup = new ol.Overlay({
        element: document.getElementById('popup')
      });

      var osmLayer = new ol.layer.Tile({
        source: new ol.source.OSM()
      });

      var ol3_sprint_location = ol.proj.transform([-1.20472, 52.93646], 'EPSG:4326', 'EPSG:3857');

      var view = new ol.View({
        center: ol3_sprint_location,
        zoom: 16
      });

      var map = new ol.Map({
        target: 'map'
      });

      map.addLayer(osmLayer);
      map.setView(view);

      map.addOverlay(popup);
      popup.setPosition(ol3_sprint_location);

    </script>
  </body>
</html>