[Mapbox GL]使用按钮修改layer的颜色


<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title></title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.28.0/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.28.0/mapbox-gl.css' rel='stylesheet' />
    <style>
        body { margin:0; padding:0; }
        #map { position:absolute; top:0; bottom:0; width:100%; }
    </style>
</head>
<body>

<style>
.map-overlay {
    font: 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
    position: absolute;
    width: 200px;
    top: 0;
    left: 0;
    padding: 10px;
}

.map-overlay .map-overlay-inner {
    background-color: #fff;
    box-shadow:0 1px 2px rgba(0, 0, 0, 0.10);
    border-radius: 3px;
    padding: 10px;
    margin-bottom: 10px;
}

.map-overlay-inner fieldset {
    border: none;
    padding: 0;
    margin: 0 0 10px;
}

.map-overlay-inner fieldset:last-child {
    margin: 0;
}

.map-overlay-inner select {
    width: 100%;
}

.map-overlay-inner label {
    display: block;
    font-weight: bold;
    margin: 0 0 5px;
}

.map-overlay-inner button {
    display: inline-block;
    width: 36px;
    height: 20px;
    border: none;
    cursor: pointer;
}

.map-overlay-inner button:focus {
    outline: none;
}

.map-overlay-inner button:hover {
    box-shadow:inset 0 0 0 3px rgba(0, 0, 0, 0.10);
}
</style>

<div id='map'></div>
<div class='map-overlay top'>
    <div class='map-overlay-inner'>
        <fieldset>          /* fieldset 元素可将表单内的相关元素分组,当一组表单元素放到 <fieldset> 标签内时,浏览器会以特殊方式来显示它们 */
            <label>Select layer</label>
            <select id='layer' name='layer'>
                <option value='water'>Water</option>
                <option value='building'>Buildings</option>
            </select>
        </fieldset>
        <fieldset>
            <label>Choose a color</label>
            <div id='swatches'></div>
        </fieldset>
    </div>
</div>

<script>
mapboxgl.accessToken = '<your access token here>';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/light-v9',
    center: [12.338, 45.4385],
    zoom: 17.4
});

var swatches = document.getElementById('swatches');  /* 获取id为swatches的DOM元素 */
var layer = document.getElementById('layer');        /* 获取id为layer的DOM元素 */   
var colors = [
    '#ffffcc',
    '#a1dab4',
    '#41b6c4',
    '#2c7fb8',
    '#253494',
    '#fed976',
    '#feb24c',
    '#fd8d3c',
    '#f03b20',
    '#bd0026'
];

colors.forEach(function(color) {          /* 使用forEach(function(element,id))遍历数组,function在每访问一个元素时就执行,element是访问的元素,                                                      * id为元素对应的索引,此处忽略function第二个参数 */
    var swatch = document.createElement('button');  /* 创建button类型html标签 */
    swatch.style.backgroundColor = color;           /* .style.backgroundColor 元素背景色*/
    swatch.addEventListener('click', function() {   /* addEventListener(type,callback)为DOM元素添加监听器,type为事件类型,function是响应回调函数 */
        map.setPaintProperty(layer.value, 'fill-color', color); /* setPaintProperty  (layer, name, value, [klass])函数layer表示layer ID,
                                                                /* name表示paint属性名,value是要设置的对应属性的值 */
    });
    swatches.appendChild(swatch);                   /* 将新添加的button插入id为swatches的DOM元素末尾 */
});
</script>

</body>
</html>



原文:https://www.mapbox.com/mapbox-gl-js/example/color-switcher/

发布了44 篇原创文章 · 获赞 54 · 访问量 34万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览