1

I need to get the coordinates of the click when the user clicks on any place on the base map. I have checked the answer to similar problem here: How do I get the coordinates of a click on vector feature/layer in OpenLayers?

But, I need the coordinates transformed to the projection system used by my map: that is EPSG:900913, Spherical Mercator Projection and passed to a html textbox.

How can I do that?

2

You should use the OpenLayers.LonLat.transform() function.

If you want to transform between EPSG 4326 & EPSG:900913, Openlayers can do it for you.

If you want to transform between some other projections, then you will have to use the Proj4js library.

Include in your page:

<script src="http://proj4js.org/lib/proj4js-compressed.js"></script>

Then in your javascript

var lonlat = yourpoint.clone();
lonlat.transform(map.projection, <target projection>);

Note that transform() changes the actual object, so if you need the orignal object for something, remember to clone it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.