I'm trying to make an alert window show the current mouse coordinates in openlayers 2 using the google maps projection EPSG:4326. Essentially like this post but in openlayers2.

But its not working out like that. I get . . .

I get . . .

Heres the code inside the click event.

var point = map.getLonLatFromPixel(e.xy);
                    myXY = new OpenLayers.LonLat(point.lon,point.lat).transform('EPSG:4326', 'EPSG:102113');
  • EPSG:4326 is not the Google maps projection, that is EPSG:3857.
    – nmtoken
    Nov 20 '16 at 14:30
  • Ah yes okay thanks. I put 3857 and I now get both coordinates but they are still not in the correct format. I was expecting something like 40.741895, -73.989308. Anyone know what this is? Nov 21 '16 at 9:29

This does it for those of you who are having this issue

    //inside click event
    var lonlat = map.getLonLatFromPixel(e.xy);
    var converted = lonlat.transform(map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326"));

lon or lat is NaN when openlayers it can not convert, check your coordenate system. If you have lon and lat, you could convert to point with this. (obviously in openlayers2, not tested in openlayers3).

var myLocation = new OpenLayers.Geometry.Point(lon, lat) .transform('EPSG:4326', 'EPSG:3857');

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.