OpenLayers调用setCenter后,地图仍位于0,0位置 [英] OpenLayers after call setCenter, map is still on 0,0 position
本文介绍了OpenLayers调用setCenter后,地图仍位于0,0位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过setCenter方法设置地图中心,但仍然无法正常工作.地图不动. 我尝试使用从投影到地图投影的转换,但没有成功. 这是代码的一部分.谢谢.
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>
推荐答案
我认为这可能是由错误的地图投影引起的,在您的地图中应设置地图投影(在示例中,我使用EPSG:900913),例如:
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")
});
如果您不这样做,则map.getProjectionObject()仍将获得EPSG:4326投影.
if you don't do that, map.getProjectionObject() will still get the EPSG:4326 projection.
这篇关于OpenLayers调用setCenter后,地图仍位于0,0位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文