Search code examples
javascriptopenlayerscenter

OpenLayers 4 center map on city coordinates


I have a problem with centering my map on creation.

My javascript code is from the quick start guide, same goes for the html code.

var map = new ol.Map({
    target: 'map',
    layers: [
      new ol.layer.Tile({
        source: new ol.source.OSM()
      })
    ],
    view: new ol.View({
      center: ol.proj.transform([51.35847, 7.49918], 'EPSG:4326', 'EPSG:3857'),
      zoom: 4
    })
  });

It just won't center and I don't understand why? I did exactly as described in other questions and topics on stackoverflow....

Fiddle:https://jsfiddle.net/gpo33ga6/


Solution

  • Here is the solution :

    var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          })
        ],
        view: new ol.View({
          center: ol.proj.fromLonLat([7.49918, 51.35847]),
          zoom: 4
        })
      });
    

    Take care that the function takes (Lon, Lat) and not (Lat, Lon).