开发者

Finding coordinates of a point in OpenLayers

开发者 https://www.devze.com 2022-12-18 16:07 出处:网络
How can one get the coordinates of a particular point on a map 开发者_如何学Pythonin OpenLayers?Handle click event on map Click handler. Here is one of many sample codes you can find in OpenLayers mai

How can one get the coordinates of a particular point on a map 开发者_如何学Pythonin OpenLayers?


Handle click event on map Click handler. Here is one of many sample codes you can find in OpenLayers mailing list archives:

map.events.register('click', map, handleMapClick);

function handleMapClick(e)
{
   var lonlat = map.getLonLatFromViewPortPx(e.xy);
   // use lonlat

   // If you are using OpenStreetMap (etc) tiles and want to convert back 
   // to gps coords add the following line :-
   // lonlat.transform( map.projection,map.displayProjection);

   // Longitude = lonlat.lon
   // Latitude  = lonlat.lat
} 


<html>
 <head>
 <script src="http://openlayers.org/api/OpenLayers.js"></script>
 <script type="text/javascript"> 
    function init(){
      map = new OpenLayers.Map('map');
      base_layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
      "http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} );
      map.addLayer(base_layer);
      map.zoomToMaxExtent();
      map.events.register('click', map, handleMapClick);
    } 

    function handleMapClick(evt)
    {
       var lonlat = map.getLonLatFromViewPortPx(evt.xy);
       // use lonlat
       alert(lonlat);
    } 
 </script>
 </head>
 <body onload="init()">
  Hello Map.<br />
 <div id="map"></div>
 </body>
</html> 

@mloskot Your answer is great you had a mistake with the evt variable.

Just added the html markup to make it a working page.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号