如何将数据集直接导入我的javascript代码

问题描述 投票:-2回答:3

我想知道是否可以将数据集直接导入到我的代码(javascript)中,因为我想为用户按下某个功能时显示给定数据集的条件。

javascript mapbox mapbox-gl
3个回答
0
投票
您发出获取请求以获取可以显示的数据。

fetch('http://localhost:portOfDatabase/path') .then(resp => resp.json()) .then(r => { //use r which holds the response from your database })


0
投票
如果可以将String格式的数据作为JSON数组或字典获取,则可以仅使用JSON.parse()从字符串格式的数据中创建对象。然后使用该对象提取所有要显示的信息。 Here are some examples

从w3schools:

var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}'); obj.name // "John" obj.age // 30 obj.city // "New York"


0
投票
从添加的标签看来,您似乎想添加可用于填充Mapbox地图的数据集。

作为其他答案,您可以从JSON文件加载数据。请注意,数据必须为geoJSON format

这将在运行时创建geoJSON数据源。但是您也可以将其指向源文件。 (请参阅第二个代码段)

map.addSource('route', { 'type': 'geojson', 'data': { 'type': 'Feature', 'properties': {}, 'geometry': { 'type': 'LineString', 'coordinates': [ [-122.48369693756104, 37.83381888486939], [-122.48348236083984, 37.83317489144141], [-122.48339653015138, 37.83270036637107], [-122.48356819152832, 37.832056363179625] ] } } )
来自文件:

map.addSource(<sourcename>, { 'type': 'geojson', 'data': <URL> });
也请参见以下示例:

© www.soinside.com 2019 - 2024. All rights reserved.