2014-03-03 157 views
0

我的問題是,當我的地圖加載時,我想根據分辨率獲得屏幕所有四個角的X和Y座標。我的代碼:如何在openLayers地圖中獲取屏幕左上角和右下角的位置?

map = new OpenLayers.Map("mapdiv", { 
    controls: [new OpenLayers.Control.Navigation(), 
    new OpenLayers.Control.PanZoomBar() 
    ], 
    numZoomLevels: 10 
}); 

map.addLayer(new OpenLayers.Layer.OSM("OpenCycleMap")); 
epsg4326 = new OpenLayers.Projection("EPSG:4326"); 
epsg900913 = new OpenLayers.Projection("EPSG:900913"); 
var lonLat = new OpenLayers.LonLat(72.58, 23.03).transform(epsg4326, epsg900913); 
console.log(lonLat); 
var zoom = 15; 

var markers = new OpenLayers.Layer.Markers("Markers"); 
map.addLayer(markers); 
map.events.register("click", map, function (e) { 
    var lonlat = map.getLonLatFromViewPortPx(e.xy); 
    alert(lonlat); 
}); 
markers.addMarker(new OpenLayers.Marker(lonLat)); 
var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer"); 

map.addLayer(polygonLayer); 
map.addControl(new OpenLayers.Control.LayerSwitcher()); 
map.addControl(new OpenLayers.Control.MousePosition()); 
map.setCenter(lonLat, zoom); 

回答

0

你應該儘量在可以的時候發表小提琴。假設你正在使用OpenLayers v2。

如果你的意思是問如何獲得視口的角落,請嘗試:

map.getViewport().getClientRects()[0]

如果你的意思是在地圖的座標中的程度:

map.getExtent()

相關問題