Alvaro
Alvaro

Reputation: 12037

OpenLayers 3 Map creation not centering

I have the following very simple code:

@map_center = [-32.951106, -60.669952]
@map = new ol.Map({
        target: 'map-canvas',
        layers: [new ol.layer.Tile({source: new ol.source.OSM()})],
        view: new ol.View({
          center: @map_center,
          zoom: 5
        })
    })

It's in coffeescript, but you will get the idea. The problem is, the map does not center at all. It gets stuck in [0, 0] Am I doing something wrong?

Upvotes: 0

Views: 826

Answers (1)

erilem
erilem

Reputation: 2664

By default the view's projection is Web Mercator (EPSG:3857). This means the view center's coordinates should be expressed in that projection.

If you have latitudes and longitudes you can use the ol.proj.transform function to transform the latitudes longitudes to Web Mercator coordinates. For example:

var view = new ol.View({
  zoom: 4,
  center: ol.proj.transform([-60, -32], 'EPSG:4326', 'EPSG:3857')
});

Upvotes: 2

Related Questions