question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

change the crs property of map dynamically

See original GitHub issue

Recently one of our project need to change the crs at run-time(during we change the base layer of the map), and I found someone have meet the same requirement through #1818 , I have tried the manner it mentioned(by using the baselayerchange event, however the author use leaflet 0.5 while I am using 0.7.2 . Which means that it does not work.

So I tried to make it myself, this is what I tried:

layer.on('add',function(){
    var crs=getCrsByLayer(this);
    map.options.crs=crs; //reset the crs of the map

    //we have to clear this two properties, or the coord of the tile would be re-wrapped wrongly by the former crs of the map
    layer._wrapLng=null;
    layer._wrapLat=null;

       //it seems that the tiles calculation procedure have been done before the add event fired, so hack it to redraw the layer.
    layer.redraw();
});

However it does not work, so I tried to dig into the codes, and finally I found that the _initialTopLeftPoint property of the map does not changed when I reset the crs through map.options.crs, which means the tiles bounds will not calculated correctly. Since this is a private property, I do not think set it outside is a good idea.

So I think if you can re-consider this feature? It does not cause more codes, you just make the crs related property can be re-initialized.

Thank you.

Issue Analytics

  • State:open
  • Created 10 years ago
  • Reactions:2
  • Comments:38 (14 by maintainers)

github_iconTop GitHub Comments

2reactions
lhywellcommented, Jul 10, 2019

It’s the same problem at /issues/2553.And I use the baselayerchange Layer events to change multiple maps,Baidu use the CRS EPSG:900913,other map use EPSG3857.This is my demo:https://lhywell.github.io/map/example/example2/leaf2.html

map.on('baselayerchange', function(layer) {
        let center = map.getCenter();
        let zoom = map.getZoom();
        console.log(map.options.crs)
        if (layer.name.indexOf('百度') > -1) {
            map.options.crs = L.CRS.Baidu;
            map.options.tms = true;
            map._resetView(center, zoom, true);
        } else {
            map.options.crs = L.CRS.EPSG3857;
            map.options.tms = false;
            map._resetView(center, zoom, true);
        }
})
2reactions
guliashcommented, Sep 23, 2014

@Hashedhash, I have found a solution, it works but as not well as I’d like to. Something like this:

map.removeLayer(map.curLayer);
var center = map.getCenter();
map.options.crs = map.newLayer.crs;
map.setView(center); //we need this, because after changing crs the center is shifted (as mentioned above probably it's an issue to)
map._resetView(map.getCenter(), map.getZoom(), true); //we need this to redraw all layers (polygons, markers...) in the new projection.
map.addLayer(map.newLayer);
Read more comments on GitHub >

github_iconTop Results From Across the Web

In Leaflet, how to change "crs" and other "Map options"?
I needed to dynamically set the scrollWheelZoom option. I'm using L.version 1.3.1. If scrollWheelZoom option is true on map init, it will enable ......
Read more >
Leaflet, changing base map CRS - GIS Stack Exchange
I am wondering whether I should change the CRS property of the map object, which is by default EPSG3857. The map I am...
Read more >
Set the map extent—ArcGIS Pro | Documentation
Set a dynamic full map extent ; In a map or scene view, right-click the name of the map or scene in the...
Read more >
Properties and options | Mapbox GL JS
Set the map's access token, test whether the browser support Mapbox GL JS, ... around (LngLatLike) : The location serving as the origin...
Read more >
MapOptions class - flutter_map library - Dart API - Pub.dev
Allows you to provide your map's starting properties for zoom, ... If you download offline tiles dynamically, you can set adaptiveBoundaries to true...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found