0
所以,我試圖從加載的樣式圖mapbox GL中獲取一個樣式圖層,但它一直返回未定義的元素。我在智慧的最後。在mapbox GL層遇到問題
我試圖讓下層水層存儲在圖層變量,所以我可以改變它的屬性。例如,它的可見性。
下面的代碼:
var layers=map.getLayer('water');
changeinnerHTML(featuresWindow, JSON.stringify(layers, null, 1));
PS - 我經過多年的返回編碼。我只是再次拂過我的js。
PPS:這裏是整個代碼,如果你不在意:
<script>
//Mapbox Access Token
mapboxgl.accessToken = 'pk.eyJ1IjoiYWtzaGF5a2h1cmFuYSIsImEiOiJjaWY3dXdkN3MxMzZ3czZsempwbWc0MnRoIn0.y7jHQWK5lONsJ_6u3EJSTg';
// Select html objects that constantly change into javascript variables. Henceforth known as elementVar
var featuresWindow = document.getElementById('features');
// GLOBAL FUNCTIONS : Functions that are used repeatedly
// Function to change inner HTML of div, span etc elements
function changeinnerHTML(elementVar, newvalue) {
elementVar.innerHTML = newvalue;
return null;
}
// Function to change colours.
function switchColor(myColor, layer) {
map.setPaintProperty(layer, 'fill-color', myColor);
}
// Intialise map
if (!mapboxgl.supported()) {
alert('Your browser does not support Mapbox GL!');
} else {
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/akshaykhurana/cihlwytjz001kb4kqu0b62xq7', //stylesheet location
minZoom: 9, //minimum zoom value
maxZoom: 16, //maximum zoom value
interactive: true
});
}
// Get some layers man.
var layers=map.getLayer();
changeinnerHTML(featuresWindow, JSON.stringify(layers, null, 1));
// Push information to sidebar on mouse hover
map.on('click', function(e) {
map.featuresAt(e.point, {
radius: 5
}, function(err, features) {
if (err) throw err;
changeinnerHTML(featuresWindow1, JSON.stringify(features, null, 2));
});
});
</script>