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

10.6k views Asked by At

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>
2

There are 2 answers

0
JSC On BEST ANSWER

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.

3
Alberto On

The marker is shown exactly in the point where it should be?

Anyway, try this:

map.setCenter(new OpenLayers.LonLat(lon, lat).transform('EPSG:4326', 'EPSG:3857'), zoom);