5

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>
Musketyr
  • 745
  • 1
  • 16
  • 37

2 Answers2

4

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.

JSC
  • 146
  • 6
1

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);
Alberto
  • 674
  • 13
  • 25
  • No, marker is a wrong position. I tried your code. Map is center to another position, but still not center to right position, see the picture [link](http://soft4you.cz/projects/map.PNG) – Musketyr Dec 09 '13 at 05:59
  • That's a transform projection problem, try with or without transform function and/or different EPSG, you have to find what projection your map uses and substitute it to EPSG:3857 (OpenLayers works with 4362, for example Google Maps works with 3857, as explained here: http://gis.stackexchange.com/a/48952/24700 -In this case I transform OpenLayers point in GMaps point) – Alberto Dec 09 '13 at 20:32
  • I am using Mercator projection, but I tried with and without transform different EPSG and still not working. – Musketyr Dec 10 '13 at 05:46