tags:

views:

379

answers:

2

How to get the coordinates of a particular point in a map in openlayers javascript libraray?

Thank you.

+4  A: 

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(evt)
{
   var lonlat = map.getLonLatFromViewPortPx(e.xy);
   // use lonlat
} 
mloskot
Thank you for your answer!
Chamikara
@Chamikara is it useful?
mloskot
@mloskot: Too bad your answer isn't accepted, since it is the straight forward and correct answer.
Chau
A: 
<html>
 <head>
 <script src="http://openlayers.org/api/OpenLayers.js"&gt;&lt;/script&gt;
 <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.

MINDoSOFT