【问题标题】:Can't get properties from external geojson to mapbox无法从外部 geojson 获取属性到 mapbox
【发布时间】:2022-04-10 14:44:23
【问题描述】:

我已经从 mapbox 复制并调整了这个示例: https://docs.mapbox.com/mapbox-gl-js/example/filter-markers/

一切正常,但我希望将 geojson 作为外部文件。

所以我更改了这段代码:

var places = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'properties': {
'icon': 'theatre'
},
'geometry': {
'type': 'Point',
'coordinates': [-77.038659, 38.931567]
}
},
]
};

用这个:

var places = '../images/destinations.geojson';

我在 DevTools 中收到此错误:未捕获的类型错误:无法读取未定义的属性“forEach”。

其余的代码(我得到错误)是这样的:

map.on('load', function() {
// Add a GeoJSON source containing place coordinates and information.
map.addSource('places', {
'type': 'geojson',
'data': places
});

places.features.forEach(function(feature) {
var symbol = feature.properties['icon'];
var layerID = 'poi-' + symbol;

// Add a layer for this symbol type if it hasn't been added already.
if (!map.getLayer(layerID)) {
map.addLayer({
'id': layerID,
'type': 'symbol',
'source': 'places',
'layout': {
'icon-image': symbol + '-15',
'icon-allow-overlap': true
},
'filter': ['==', 'icon', symbol]
});

【问题讨论】:

    标签: mapbox geojson


    【解决方案1】:

    加载外部 geoJSON 文件的一种方法是使用 d3js。请参阅下面的示例,该示例取自 this mapbox example。此示例将为 geoJSON 文件中给出的坐标绘制一条线。

    您应该能够使用您的 forEach 循环遍历 data.features

    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8" />
    <title>Load from an external geoJSON</title>
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
    <script src="https://api.mapbox.com/mapbox-gl-js/v1.8.0/mapbox-gl.js"></script>
    <link href="https://api.mapbox.com/mapbox-gl-js/v1.8.0/mapbox-gl.css" rel="stylesheet" />
    <style>
    	body { margin: 0; padding: 0; }
    	#map { position: absolute; top: 0; bottom: 0; width: 100%; }
    </style>
    </head>
    <body>
    <div id="map"></div>
     
    <script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
    <script>
    	mapboxgl.accessToken = '<access_token>';
    var map = new mapboxgl.Map({
    container: 'map',
    style: 'mapbox://styles/mapbox/satellite-v9',
    zoom: 0
    });
     
    map.on('load', function() {
    // We use D3 to fetch the JSON here so that we can parse and use it separately
    // from GL JS's use in the added source. You can use any request method (library
    // or otherwise) that you want.
    d3.json(
    'https://docs.mapbox.com/mapbox-gl-js/assets/hike.geojson', //the geoJSON data file
    function(err, data) {
    if (err) throw err;
     
    // save full coordinate list for later
    var coordinates = data.features[0].geometry.coordinates;
     
    // start by showing just the first coordinate
    data.features[0].geometry.coordinates = [coordinates[0]];
     
    // add it to the map
    map.addSource('trace', { type: 'geojson', data: data });
    map.addLayer({
    'id': 'trace',
    'type': 'line',
    'source': 'trace',
    'paint': {
    'line-color': 'yellow',
    'line-opacity': 0.75,
    'line-width': 5
    }
    });
     
     
    
    
    }
    );
    });
    </script>
     
    </body>
    </html>

    【讨论】:

    • 是的!那完成了工作。如果我想链接到我已导入到 mapbox 的数据集,语法是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多