我似乎無法讓我的數據顯示在我的地圖上。 geojson文件很大,所以我將它加載到外部源。我的index.html文件看起來像這樣將geojson數據加載到Mapbox中
<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1Ijoic2VhbmRyYWFkbG96ZSIsImEiOiJjajA3cW1pOTcwMDA5Mndvd2hicHlmNWc1In0.7bt7sPXDoymYJyVDvVmqZw';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/seandraadloze/cj1dcc3ho00f82smkvjxp4frb', //hosted style id
center: [24.530,-32.254], // starting position
zoom: 14.5 // starting zoom
});
var url = 'http://bestersurveys.co.za/htmlsite/quintin.geojson';
var source = new map.addSource()({
data: url
});
window.setInterval(function() {
source.setData(url);
}, 1000);
map.on('style.load', function() {
map.addSource("markers", source);
map.addLayer({
"id": "markers",
"type": "circle",
"visibility": "visible",
"source": "markers",
"paint": {
"circle-radius": 0.2,
"circle-color": "#f3f3f3"
}
});
});
</script>
</body>
</html>
我從控制檯收到此錯誤
map.js:895 Uncaught TypeError: Cannot read property 'addSource' of undefined
at new e.addSource (map.js:895)
at maptest.html:27
您可以檢查出這裏的地圖 - http://bestersurveys.co.za/htmlsite/maptest.html
誰能告訴我我在哪裏出錯了?
感謝
肖恩
我猜這部分'VAR源=新map.addSource()({'取出新的關鍵字。 –
隊友您好,感謝。好吧那排序的錯誤,但還沒有數據,我的控制檯現在打印 未捕獲的錯誤:樣式沒有完成加載 在t._checkLoaded(style.js:272) 在t.addSource(style.js:372) 在e.addSource(地圖。 js:895) at maptest.html:27 –
嗯,從來沒有遇到過,我發現了一個不要在map.on('style.load'')之間放置所有東西,這樣可以解決這個問題,所以只要樣式被加載,你的代碼就會被觸發。 –