Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Leaflet, how to change "crs" and other "Map options"?

Tags:

leaflet

mapbox

For example, I create a map like this:

    map = L.map('map',
        {maxZoom: 17,
        attributionControl: false,
        zoomControl: false}
    )

Later, I want to change the "crs" and add a key to the map object.

I expect there might be a method called setOption, like this:

map.setOption({crs:L.CRS.BEPSG3857, customOption: true})

But unfortunately there is no such setOption method..Does anyone have ideas about how to change the Map option for a leaflet map object?

like image 807
Hanfei Sun Avatar asked Jun 16 '15 08:06

Hanfei Sun


People also ask

How do you update marker position in Leaflet?

Show activity on this post. This is useful to know when attempting to add the SmoothMarkerBouncing plugin in conjunction with marker dragging. Whilst dragging Markers within the browser looks fine, subsequently firing the bounce event will reset the marker to its initial position unless this resetting, i.e. marker.

How do you get marker off a Leaflet map?

Layergroup allows to control all markers at once. Ok, removing the layer seems to be the solution, but the more straightforward answer to the question, how to remove a marker is the one given by @HarijsKrūtainis : marker. remove() , it worked perfectly to me.

What is Tilelayer in Leaflet?

A LeafletJS plugin to appy WebGL shaders to your tiles. With this plugin, you can apply colour transforms to your tiles, merge two or more tiles with a custom function, perform on-the-fly hillshading, or create synthetic tile layers based only on the map coordinates.


2 Answers

Check this example on how to change crs on the fly: http://jsfiddle.net/alekzonder/qxdxqsm3/

var center = map.getCenter();

if (...) {
   map.options.crs = L.CRS.EPSG3395;
} else {
   map.options.crs = L.CRS.EPSG3857;
}

map.setView(center);

map._resetView(map.getCenter(), map.getZoom());
like image 127
sboulema Avatar answered Nov 10 '22 11:11

sboulema


I had a similar problem. 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 the corresponding Handler. Therefore we need to enable/disable the handler manually to apply the changes to the map.

List of handlers/map-properties: boxZoom, doubleClickZoom, dragging, keyboard, scrollWheelZoom, tap, touchZoom

My solution looks somehow like this:

class MyMap {

    // constructor(props) {
    //  ...
    // }

    // getMapElement(){
    //  ...
    // }

    // getBaseLayer(){
    //  ...
    // }

    initMap( mapOptions ){
        if ( ! this.map  ) {
            this.map = L.map( this.getMapElement(), mapOptions );
            this.getBaseLayer().addTo( this.map );
        }
    }

    setMapOptions( newMapOptions ){
        // loop newMapOptions object
        for ( let newMapOptionKey in newMapOptions ) {
            if( newMapOptions.hasOwnProperty(newMapOptionKey)) {
                this.setMapOption( newMapOptionKey, newMapOptions[newMapOptionKey] );
            }
        }
    }

    setMapOption( newMapOptionKey, newMapOptionVal ){
        // set map option
        L.Util.setOptions( this.map, {[newMapOptionKey]: newMapOptionVal});
        // apply option to handler
        if ( this.map[newMapOptionKey] instanceof L.Handler ) {
            if ( newMapOptionVal ) {
                this.map[newMapOptionKey].enable();
            } else {
                this.map[newMapOptionKey].disable();
            }
        }
    }

}

This works for the scrollWheelZoom option. Think it should work for all options that enable/disable handlers. So it might work for your customOption but it won't work for the crs option.

like image 22
jhotadhari Avatar answered Nov 10 '22 11:11

jhotadhari