2010-06-22 239 views
2

我會從openlayers地圖獲取座標。 當我打開我的頁面時,當我點擊一個按鈕時,我收到了左上角和右上角的座標。[Javascript]從openlayers地圖獲取座標

<html> 
<head> 
    <title>OpenLayers Example</title> 
    <script src="http://openlayers.org/api/OpenLayers.js"></script> 
    </head> 
    <body> 
     <div style="width:100%; height:100%" id="map"></div> 
     <script defer="defer" type="text/javascript"> 
     var map = new OpenLayers.Map('map'); 
     var wms = new OpenLayers.Layer.WMS("OpenLayers WMS", 
      "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'}); 
     map.addLayer(wms); 
     map.zoomToMaxExtent(); 
     </script> 
<input type="button" text="Get Coordinate from map"> 
</body> 
</html> 

非常感謝。

回答

4

您應該使用Map對象的getExtent()函數。

返回

{OpenLayers.Bounds}甲界對象 表示的 當前視口的經度/緯度邊界。

它會返回一個OpenLayers.Bounds對象,該對象將具有當前視圖的頂部,左側,底部和右側座標。

+1

你可以使用.toBBOX()來獲得一個字符串版本的界限,並且你可以使用任何標準的Javascript方法來生成你的按鈕。 – 2010-06-25 00:36:20

0

我想你想使用map.getCenter()? (這是爲了防止地圖的中心座標在緯度,但它不是很清楚你想要什麼座標)