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?
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.
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.
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.
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());
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With