如何在Mapbox中按邮政编码组构建自定义地区并填充颜色?

时间:2019-12-19 09:58:08

标签: mapbox mapbox-gl-js mapbox-gl

我有一些基于邮政编码区域的自定义销售地区。 如何用不同的颜色显示该地区?

1 个答案:

答案 0 :(得分:0)

如果自定义销售地区的数据采用GeoJSON格式,则可以遵循Mapbox GL JS文档中的this example,该文档显示了如何使用自定义颜色将GeoJSON多边形添加到地图中。如链接的示例所示,您可以为每个销售区域添加一个源和一个图层,然后通过要添加的图层对象的paint属性为每个区域指定颜色。因此,对于每个地区,您都将使用以下代码:

map.addSource('territory_name', {
    'type': 'geojson',
    'data': {
        'type': 'Feature',
        'geometry': {
            'type': 'Polygon',
            'coordinates': [ list_of_coordinates_defining_territory_boundaries ]
        }
    }
});

map.addLayer({
    'id': 'territory_name',
    'type': 'fill',
    'source': 'territory_name',
    'layout': {},
    'paint': {
        'fill-color': 'hex code of color to be used for this territory',
        'fill-opacity': 0.8
    }
});

或者,您可以在tileset中以Mapbox Studio的形式上传数据,并按照this tutorial中的描述,将所述图块作为图层添加到自定义地图样式中。此选项的确切步骤更多地取决于您的基础数据格式,但是Mapbox Studio Manual是使用Studio根据您自己的数据创建高度自定义地图的理想指南。