kartena / Proj4Leaflet

Smooth Proj4js integration with Leaflet.
http://kartena.github.io/Proj4Leaflet/
BSD 2-Clause "Simplified" License
589 stars 173 forks source link

how to define EPSG 3857 using Proj4Leaflet? #141

Closed danzhuibing closed 7 years ago

danzhuibing commented 7 years ago

I know EPSG 3857 is the default CRS for Leaflet. I'm just curious about how to define EPSG 3857 by Proj4Leaflet.

My sketch code is:

var crs = new L.Proj.CRS('EPSG:3857',
            '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs',
            {
                origin: [?, ?],
                transformation: ?,
                resolutions: ?,
                bounds: ?
            }
        );

var mymap = L.map('mapid', {
            crs: crs
        }).setView([51.505, -0.09], 13);

L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
        attribution: '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors',
        maxZoom: 18,
        minZoo: 9,
        subdomains: "abc"
}).addTo(mymap);

I just couldn't figure out the ? syntax in the code. Can anyone help?

danzhuibing commented 7 years ago

finally I figure it out by myself:

   var get_resolution = function() {
        level = 19;
        var res = [];
        res[0] = Math.PI * 2 * 6378137 / 256;
        for(var i = 1 ; i < level; i++) {
            res[i] = Math.PI * 2 * 6378137 / 256 / Math.pow(2, i);
        }
        return res;
    };

    var crs = new L.Proj.CRS('EPSG:3857',
        '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs',
        {
            resolutions: get_resolution(),
            origin: [-Math.PI * 2 * 6378137 / 2.0, Math.PI * 2 * 6378137 / 2.0],
            bounds: L.bounds([-20037508.342789244, 20037508.342789244], [20037508.342789244, -20037508.342789244])
        }
    );