Finding coordinates of a point in OpenLayers

cuser picture cuser · Jan 29, 2010 · Viewed 13.8k times · Source

How can one get the coordinates of a particular point on a map in OpenLayers?

Answer

mloskot picture mloskot · Feb 3, 2010

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
}