NOTICE: is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

Hi, I am trying to run this very simple code:

var map;

function init()
    map = new OpenLayers.Map('map');

    layerMapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik", {buffer: 4});

    map.setCenter(new OpenLayers.LonLat(16.5, 49.2), 5);


Everything is ok, map is displayed but it is still centered here instead of here

Any thoughts what I am doing wrong?

asked 08 Feb '12, 12:05

Pirozek's gravatar image

accept rate: 0%

edited 08 Feb '12, 12:33

SomeoneElse's gravatar image

SomeoneElse ♦

Your map is in spherical Mercator coordinates and you're setting it to a centre given in degrees. Try this:

var proj4326 = new OpenLayers.Projection("EPSG:4326");
var projmerc = new OpenLayers.Projection("EPSG:900913");
var lonlat = new OpenLayers.LonLat(16.5, 49.2);
lonlat.transform(proj4326, projmerc);
map.setCenter(lonlat, 5);

The problem you are seeing is a general OpenLayers issue when dealing with projections, and not limited to OpenStreetMap. Using a simpler toolbox like Leaflet would help you avoid these issues.

permanent link

answered 08 Feb '12, 12:17

Frederik%20Ramm's gravatar image

Frederik Ramm ♦
accept rate: 23%

It works like a charm! Thanks man

(08 Feb '12, 12:20) Pirozek

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:


question asked: 08 Feb '12, 12:05

question was seen: 6,609 times

last updated: 08 Feb '12, 12:33

NOTICE: is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum