LeafletJS: How to remove the zoom control

2019-02-03 22:18发布

I'm trying to remove the zoom controls (+/-) on a LeafletJS map.

I'm using the MapBox.js version of Leaflet but most of the operations are the same as Leaflet. I implement my map like this:

var map = L.mapbox.map('map');

var layer = L.mapbox.tileLayer('MAPBOX-ID', {
    format: 'jpg70',
    minZoom: 13,
    maxZoom: 15,
    reuseTiles: true, 
    unloadInvisibleTiles: true
});
map.addLayer(layer);
map.setView([40.73547,-73.987856]);

The documentation says there's a zoomControl option that will remove the zoom control from the map but I've had no luck in getting it to work.

How can I remove the zoom control with this implementation?

Thanks!

7条回答
戒情不戒烟
2楼-- · 2019-02-03 22:38
map.scrollWheelZoom.disable();
查看更多
冷血范
3楼-- · 2019-02-03 22:41

This is work for me.

var map = new L.Map('map', { zoomControl:false });

Did you try?

var map = L.mapbox.map('map', { zoomControl:false });
查看更多
放我归山
4楼-- · 2019-02-03 22:45

To dynamically remove then add back the zoom control:

var map = L.mapbox.map('map');

if( wantToRemove ) {
    map.removeControl( map.zoomControl ); 
} else {
    map.addControl( map.zoomControl );
}
查看更多
Bombasti
5楼-- · 2019-02-03 22:57

If you want to dynamically turn on and off zooming you can do something like this:

map.touchZoom.disable();
map.doubleClickZoom.disable();
map.scrollWheelZoom.disable();
map.boxZoom.disable();
map.keyboard.disable();
$(".leaflet-control-zoom").css("visibility", "hidden");
查看更多
【Aperson】
6楼-- · 2019-02-03 22:57

you can remove the control zoomControl in this way:

map.removeControl(map.zoomControl);
查看更多
成全新的幸福
7楼-- · 2019-02-03 23:03

Thanks to coordinate's answer I was able to figure out the correct method. The solution is:

// Create the map
var map = L.mapbox.map('map', null, { zoomControl:false });

// Create my custom layer
var layer = L.mapbox.tileLayer('MAPBOX-ID', {
    format: 'jpg80',
    minZoom: 13,
    maxZoom:15,
    tileSize: 256,
    reuseTiles: true, 
    unloadInvisibleTiles: true
});


// Add the layer
map.addLayer(layer);
查看更多
登录 后发表回答