Skip to content Skip to sidebar Skip to footer

Trying To Use Epsg:3857 In Leaflet

I am trying to make leaflet use EPSG:3857 as an input coordinate system. I use porj4leaflet to achieve this. I have defined my map instance like this: var map = L.map('map', { cent

Solution 1:

The "finite numbers" error is because L.Map expects the center coordinates to be in the range -90 to 90 and -180 to 180 degrees.

In your jsfiddle, you also have the problem that you are selecting zoom:11, but only have 7 zoom levels available in your CRS definition.

The Leaflet API always uses WGS84 latitude/longitude coordinates in degrees, so using Proj4leaflet this way won't do what you want. See this explanation from the Proj4Leaflet project.

However, EPSG:3857 is already the default CRS in Leaflet, and you can use its built-in methods to convert between meters and WGS84 degrees, without needing either Proj4Leaflet or Proj4js. See the Leaflet CRS documentation.

const proj = L.CRS.EPSG3857;

// Degrees to metres
proj.project(new L.LatLng(51,-2));             // {x: -222638.98158654, y: 6621293.722740}// Metres to degrees
proj.unproject(new L.Point(-222600, 6621000)); // {lat: 50.998339473, lng: -1.99964982245}

It's probably easiest to unproject your metre coordinates to WGS whenever you need to pass them to Leaflet, but alternatively you could write overridden versions of some of Leaflet's functions. These would take metre coordinates, convert them to degrees, then pass them on the original function.

Post a Comment for "Trying To Use Epsg:3857 In Leaflet"