OpenLayers after call setCenter, map is still on 0,0 position

Musketyr picture Musketyr · Dec 3, 2013 · Viewed 8.5k times · Source

I tried to set map center by method setCenter, but still not working. Map is not moving. I tried to used transform from projection to map projection and without successful. Here is part of code. Thanks.

<script type="text/javascript">
        var lon = 15.869378; //WGS LL84
        var lat = 49.528964;
        var zoom = 5;
        var map, layer;

        function init(){
            map = new OpenLayers.Map('map');
            layer = new OpenLayers.Layer.TMS("Name",
                   "[URL]",
                   { 'type':'png', 'getURL':get_my_url });

            map.addLayer(layer);

            var markers = new OpenLayers.Layer.Markers( "Markers" );
            map.addLayer(markers);
            markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(lon, lat).transform(
                new OpenLayers.Projection("EPSG:4326"),
                map.getProjectionObject())));

            map.setCenter(new OpenLayers.LonLat(lon, lat).transform(
                new OpenLayers.Projection("EPSG:4326"),
                map.getProjectionObject()), zoom);

        }                       
    </script>

Answer

JSC picture JSC · Dec 9, 2013

I think it may caused by wrong map projection, in your map you should set map projection (I use EPSG:900913 in the example), such as:

map = new OpenLayers.Map('testmap', {           
    numZoomLevels: 10,
    projection: new OpenLayers.Projection("EPSG:900913"),
    displayProjection: new OpenLayers.Projection("EPSG: 4326")
    });

if you don't do that, map.getProjectionObject() will still get the EPSG:4326 projection.