3

I read everywhere that Leaflet is internally using a spherical web mercator (EPSG:3857), but whatever I try I get either Lat/Lon (EPSG:4326) or strange coordinates I dont understand:

map.getBounds().getNorthEast();          // LatLng(52.5262, 13.44907)
map.getPixelBounds().getTopRight();      // Point(2253845, 1375425) ???
map.getPixelOrigin();                    // Point(2252165, 1375425) ???

Let's try to project the spherical mercator:

L.Projection.SphericalMercator.project(
    map.getBounds().getNorthEast()
);                                       // Point(0.23473, 1.08117) !?!

All right, there is a known bug in Leaflet 0.7.3, so let's add the earth radius:

L.point(
    L.Projection.SphericalMercator.project(
        map.getBounds().getNorthEast()
    ).x * 6378137.0,
    L.Projection.SphericalMercator.project(
        map.getBounds().getNorthEast()
    ).y * 6378137.0
);                                       // Point(1497143.40914, 6895833.38915)

So, really, is it so hard to get the default projection coordinates of my map? Why do I have to call project() at all? What did I miss?

I am using leaflet 0.7.3 and did not change the CRS, using default EPSG:3857. Any ideas?

How to access Leaflet EPSG:3857 coordinates?

1

Finally got an answer on github:

Internally, Leaflet transforms EPSG:4326 coordinates to pixel coordinates. These pixel coordinates are the EPSG:3857 coordinates divided by a power of 2 (and then rounded).

It might be possible to fetch some of these coordinates (look at methods like L.Map.latLngToLayerPoint) and then multiply stuff given the zoom level, but beware of rounding errors.

So my assumption EPSG:3857 is the default was wrong.

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.