set map center in leaflet with 3857 lat/lon
Asked Answered
P

1

7

I'm trying to set map center with lat/lon of 3857 projection in leaflet. By using lat/lon of projection 4326 its working fine.

var map = L.map('map', {
        crs: L.CRS.EPSG3857
    }).setView([51.40457186188496, -2.3741738081973844], 13);

But it dose not working if i provide 3857 lat/lon.

var map = L.map('map', {
            crs: L.CRS.EPSG3857
        }).setView([6693172.2381477, -264291.81938326], 13);

Please help me where i am wrong.

Thanks

Purplish answered 19/3, 2014 at 11:22 Comment(0)
D
3

Leaflet's API uses lat/lng for all its operations, so you should never use projected coordinates when calling Leaflet.

If you have projected coordinates, which strictly speaking isn't latitude and longitude, you can turn them into lat/lng by unprojecting them. Since current stable versions of Leaflet has a slightly different definition of EPSG:3857, you will have to divide your coordinate by the EPSG:3857's sphere radius. Also, your coordinate appears to have x and y swapped. Anyway, here's code to perform the conversion:

function toLatLng(x, y, map) { var projected = L.point(y, x).divideBy(6378137); return map.options.crs.projection.unproject(projected); }

Call it like this:

var latLng = toLatLng(6693172.2381477, -264291.81938326, myMap);

You could also work with a library like Proj4js to do projection/unprojection: http://proj4js.org/

Dialogue answered 11/4, 2014 at 12:18 Comment(2)
yes you right first i need to unprojected the point.but the method you mention its unprojected the point but not correctly. e.g(lat:-90, lng :383490520.79999435)Purplish
i don't want to use extra lib like Proj4js.Purplish

© 2022 - 2024 — McMap. All rights reserved.