Roc*_*cky 0 mapbox mapbox-gl-js angular
我正在使用基于外部文件绘制邮政编码轮廓的mapbox,geojson如下所示:
import { Component, OnInit } from '@angular/core';
import * as mapboxgl from 'mapbox-gl';
@Component({
selector: 'app-mapbox',
templateUrl: './mapbox.component.html',
styleUrls: ['./mapbox.component.css']
})
export class MapboxComponent implements OnInit {
ngOnInit() {
mapboxgl.accessToken = 'api-key';
let map = new mapboxgl.Map({
container: 'map',
style: 'styles',
zoom: 5,
center: [-80.118, 25.897]
});
map.addControl(new mapboxgl.NavigationControl());
map.on('load', function () {
map.addSource("route", {
"type": "geojson",
"data": "./assets/shapeGeoJson.json"
});
map.addLayer({
"id": "route",
"type": "line",
"source": "route",
"paint": {
'line-color': "gray"
}
});
});
}
}
Run Code Online (Sandbox Code Playgroud)
在这里,我能够加载geojson我将获得邮政编码轮廓的文件。
现在,我想调用一个 api,其中 in 将获取density邮政编码的计数,我需要根据该density值绘制邮政编码层。
谁能告诉我如何实现这一目标?
有什么帮助吗?
您可以使用Mapbox的addSource。只需在您的代码中添加这些行
map.addSource('zipDensity', {
type: 'geojson',
data: {
...geoJsonObj // this can be your data from API containing density prop
}
});
map.addLayer({
'id': 'zipDensity',
'source': 'zipDensity',
'type': 'fill',
'paint': {
'fill-color': {
property: 'density', // this will be your density property form you geojson
stops: [
[1000, '#CECECE'],
[100000, '#DEDEDE'],
]
},
'fill-opacity': 1
}
});
Run Code Online (Sandbox Code Playgroud)
在addLayer您可以添加停靠点,您可以在其中添加密度值和颜色代码。你可以探索更多的道具addLayer
希望对你有帮助..