javascript LeafletJS:如何删除缩放控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16537326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 05:03:23  来源:igfitidea点击:

LeafletJS: How to remove the zoom control

javascriptmapleafletmapbox

提问by Brett DeWoody

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

我正在尝试删除LeafletJS地图上的缩放控件 (+/-) 。

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

我使用的是MapBox.js 版本的 Leaflet,但大部分操作与 Leaflet 相同。我像这样实现我的地图:

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 documentationsays 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.

文件说,有将从地图中删除变焦控制zoomControl可选项,但我没有运气在里面自己动手。

How can I remove the zoom control with this implementation?

如何使用此实现删除缩放控件?

Thanks!

谢谢!

回答by coordinate

This worked for me:

这对我有用:

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

With mapbox try:

使用 mapbox 尝试:

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

See map creationand the zoomControl optionin the Leaflet documentation.

请参阅Leaflet 文档中的地图创建zoomControl 选项

回答by Allie Hoch Janoch

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");

回答by Brett DeWoody

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);

回答by leobelizquierdo

you can remove the control zoomControlin this way:

您可以通过zoomControl这种方式删除控件:

map.removeControl(map.zoomControl);

回答by yemaw

You can just use

你可以使用

map.zoomControl.remove();

map.zoomControl.remove();

回答by Anik

map.scrollWheelZoom.disable();

回答by CDM

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 );
}