OpenLayers Map Not Showing – Reasons and Fixes

javascriptopenlayers

I'm trying to create a plunker to find out a solution when adding projection list in Openlayers. But when I add a sample map, it's not showing the map and there is no error in the console either.

var olview = new ol.View({
  center: [0, 0],
  zoom: 2
});
var map = new ol.Map({
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    })
  ],
  target: 'map',
  view: olview
});

Plunker

Best Answer

If you move your <script> tags containing your .js file into <body> the map will show.

<!DOCTYPE html>
<html>    
  <head>
    <title>Simple Map</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol-debug.css" type="text/css">
    <link rel="stylesheet" href="./style.css" type="text/css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/openlayers/4.6.5/ol.js"> 

  </head>

  <body>
    <div id="map" class="map1"></div>
    <script src="./script.js"></script>
  </body>

</html>
Related Question