Aurora
Aurora

Reputation: 133

mapbox map layer control with javascript

i am trying to create a web map from mapbox. i have created my custom map style and generateed in this code. here i am trying to control layer color by some button. i have created the custom map of my own with four more layers named da_underconstruction, da_approve, da_completed, da_applied. the other two layers are built in water and building. by this app water can be changed but the other layers i cannot change. can anybody have a look and help me find out what am i doing wrong? NB: to run the code the copy paste it in a html file and open with browser. Thanks in advance.

i have tried different style maps.

<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8' />
    <title>Change a layer's color with buttons</title>
    <meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
    <script src='https://api.tiles.mapbox.com/mapbox-gl-js/v1.0.0/mapbox-gl.js'></script>
    <link href='https://api.tiles.mapbox.com/mapbox-gl-js/v1.0.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>
            <label>Select layer</label>
            <select id='layer' name='layer'>
                <option value='da_approved'>da_approved</option>

                 <option value='da_completed'>da_completed</option>
                  <option value='da_applied'>da_applied</option>
                   <option value='da_underconstruction'>da_underconstruction</option>
                   <option value='building'>Buildings</option>
                   <option value='water'>water</option>
            </select>
        </fieldset>
        <fieldset>
            <label>Choose a color</label>
            <div id='swatches'></div>
        </fieldset>
    </div>
</div>

<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibnNiemhkIiwiYSI6ImNqdnhudHdkejA2anI0NHBpamNqbzIxaHcifQ.w7fVujcKAIAF0WTN9A8HZg';
var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/nsbzhd/cjw4md68g226y1cp3k6iwobna',
    center: [144.961786, -37.817656],
    zoom: 15.78,
    pitch:60.00
});

var swatches = document.getElementById('swatches');
var layer = document.getElementById('layer');
var colors = [
    '#ffffcc',
    '#a1dab4',
    '#41b6c4',
    '#2c7fb8',
    '#253494',
    '#fed976',
    '#feb24c',
    '#fd8d3c',
    '#f03b20',
    '#bd0026'
];

colors.forEach(function(color) {
    var swatch = document.createElement('button');
    swatch.style.backgroundColor = color;
    swatch.addEventListener('click', function() {
        map.setPaintProperty(layer.value, 'fill-color', color);
    });
    swatches.appendChild(swatch);
});
</script>

</body>
</html>

i am trying to control the color of the da_construced,da_approved,da_applied but here only water's color can be controled.

Upvotes: 0

Views: 1147

Answers (1)

Brandi H
Brandi H

Reputation: 91

Disclaimer: I work for Mapbox.

Hi fallen_programmer,

It looks like you're using the wrong property for the fill on line 133:

map.setPaintProperty(layer.value, 'fill-color', color);

if you change that to fill-extrusion-color, it works.

map.setPaintProperty(layer.value, 'fill-extrusion-color', color);

Hope that helps!

Best, Brandi

Upvotes: 2

Related Questions