gpt4 book ai didi

mapbox-gl-js - mapbox gl js禁用控件

转载 作者:行者123 更新时间:2023-12-03 17:06:46 33 4
gpt4 key购买 nike

有没有办法隐藏/删除或禁用控件,例如 mapbox-gl-draw 中的控件?

我添加绘制控件如下

    draw = mapboxgl.Draw({
drawing: true,
displayControlsDefault: false,
controls: {
polygon: true,
trash: true
}
});

map.addControl(draw);

绘制多边形后,我想禁用或隐藏控件,因此不再可能绘制另一个多边形。

非常感谢!

格雷戈尔

最佳答案

您实际上可以通过一些 DOM 交互来禁用绘制按钮(单独或作为一个组)。简而言之,您可以找到绘图菜单的原生 CSS 类( 'mapbox-gl-draw_polygon''mapbox-gl-draw_point' 等),添加禁用属性,并添加带有“禁用”/灰色样式的 CSS 类。此处示例:https://jsfiddle.net/emLs72zj/9/

// HTML

<div id="map">

</div>
<button id="disable-draw">
Disable Draw Btns
</button>

<button id="enable-draw">
Enable Draw Btns
</button>


// CSS

body { margin:0; padding:0; overflow:hidden;}
#map { position:absolute; top:20px; bottom:0; width:100%; }

.disabled-button {
-webkit-filter: opacity(.3) drop-shadow(0 0 0 #FFF);
filter: opacity(.3) drop-shadow(0 0 0 #FFF);
cursor: default !important;
}


// JS

mapboxgl.accessToken = 'pk.eyJ1IjoieXVuamllIiwiYSI6ImNpZnd0ZjZkczNjNHd0Mm0xcGRoc21nY28ifQ.8lFXo9aC9PfoKQF9ywWW-g';
var sfmapbox = [-122.413692, 37.775712];

// Create a new dark theme map
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/outdoors-v9', //stylesheet location
center: sfmapbox, // starting position
zoom: 12, // starting zoom
minZoom: 11,
});

map.on('load', function(){
// create control
var draw = mapboxgl.Draw({
drawing: true,
displayControlsDefault: false,
controls: {
polygon: true,
trash: true
}
});
// add control to map
map.addControl(draw);

var disableBtn = document.getElementById('disable-draw');
var enableBtn = document.getElementById('enable-draw');

var drawPolygon = document.getElementsByClassName('mapbox-gl-draw_polygon');

disableBtn.onclick = (e) => {
drawPolygon[0].disabled = true;
drawPolygon[0].classList.add('disabled-button');
};

enableBtn.onclick = (e) => {
drawPolygon[0].disabled = false;
drawPolygon[0].classList.remove('disabled-button');
};
})

关于mapbox-gl-js - mapbox gl js禁用控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40350464/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com