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

强颜欢笑 提交于 2020-01-05 08:42:14

问题


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>

回答1:


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.




回答2:


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);


来源:https://stackoverflow.com/questions/20346172/openlayers-after-call-setcenter-map-is-still-on-0-0-position

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!