我似乎无法让我的数据显示在我的地图上。 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'')之间放置所有东西,这样可以解决这个问题,所以只要样式被加载,你的代码就会被触发。 –