Convert point to lat lon
Asked Answered
T

1

23

I wonder, how can I get map click event's coordinates as lat,lon?

Here is my code:

map.on('click', function(evt) {
    var element = popup.getElement();
    var coordinate = evt.coordinate;

    var latLon = ol.proj.transform(coordinate, 'EPSG:3857', 'EPSG:4326');

    $(element).popover('destroy');
    popup.setPosition(coordinate);

Normally, coordinate value gives me an array etc: [48654.02545, 3265468.45455]

But I need lat lon etc:([39,54876,32,547821])

Abstract: I need to convert epsg:3857 coordinate to epsg:4326 coordinate (lat/lon)

Any idea?

Tarshatarshish answered 17/11, 2014 at 7:33 Comment(2)
what about map.getEventCoordinate(evt) ?Bertrand
@Bertrand it gives me sth like this [3648595.2737768563,4850925.944188948] but i have a calculation based on lat,lonTarshatarshish
D
49

If your map view projection is Web Mercator (EPSG:3857), which is the default, then the following should work:

map.on('click', function(evt) {
  var lonlat = ol.proj.transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326');
  var lon = lonlat[0];
  var lat = lonlat[1];
  // …
});
Diaphanous answered 17/11, 2014 at 8:47 Comment(4)
i solved problem but your solution is more professional. I think problem caused by android LogCat, because i coudlnt see values of array.Tarshatarshish
This solution works just for evt.coordinate. It's not working for an icon object. Etc: var geometry = feature.getGeometry(); var coord = geometry.getCoordinates(); var lonlat = ol.proj.transform(coord, 'EPSG:3857', 'EPSG:4326'); var lon = lonlat[0]; var lat = lonlat[1]; returns Lon-Lat: 0.000026949458523585642 - 0.000053898917059314044Tarshatarshish
You need to provide a complete example.Diaphanous
Just so you know, there is also ol.proj.toLonLat function, that does this for us: [link]openlayers.org/en/latest/apidoc/module-ol_proj.html#.toLonLat But keep in mind, this method works with default projection codes, works in OpenLayers 6.6.1Harrell

© 2022 - 2024 — McMap. All rights reserved.