我有来自开放数据 API 的 json,我需要将其转换为 geojson,以便它可以在我的 Mapbox 地图上显示为图层。我正在使用 Mapbox GL JS 库:https://docs.mapbox.com/mapbox-gl-js/api/。以下是开放数据 json api 的链接: https: //data.cityofnewyork.us/resource/64uk-42ks.json。
我可以成功获取 json api 并将其打印到控制台,但现在我需要将其转换为 geojson。我知道我可以完全在前端完成此操作,因为它是开放数据。
这是我的代码:
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/niki12step/ck8q9fgpx00d91ipipual7mrl', // replace this with your style URL
center: [-73.961581,40.683868],
zoom: 9.5
})
var pluto_url = 'https://data.cityofnewyork.us/resource/64uk-42ks.json'
getData();
async function getData () {
await fetch(pluto_url)
.then(response => response.json())
.then(data => console.log(data))
}
Run Code Online (Sandbox Code Playgroud)