OpenLayers 3 Beginner’s Guide

Creating your first map

Open sample in a new windows

<!doctype html>
<html>
  <head>
    <title>Hello OpenStreetMap</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>
    <script src="../assets/ol3/js/ol.js"></script>
    <script>
        // Declare a Tile layer with an OSM source
        var osmLayer = new ol.layer.Tile({
          source: new ol.source.OSM()
        });
        // Create latitude and longitude and convert them to default projection
        var birmingham = ol.proj.transform([-1.81185, 52.44314], 'EPSG:4326', 'EPSG:3857');
        // Create a View, set it center and zoom level
        var view = new ol.View({
          center: birmingham,
          zoom: 6
        });
        // Instanciate a Map, set the object target to the map DOM id
        var map = new ol.Map({
          target: 'map'
        });
        // Add the created layer to the Map
        map.addLayer(osmLayer);
        // Set the view for the map
        map.setView(view);
    </script>
  </body>
</html>