【问题标题】:Is there a way to convert API data to a Mapbox layer?有没有办法将 API 数据转换为 Mapbox 层?
【发布时间】:2019-09-15 05:06:04
【问题描述】:

我在下面有一个回调函数,它能够从 Eventbrite API 生成数据。目前,此功能可以通过“新标记”方法在我的 Mapbox 地图上生成标记。但是,我想通过“Mapbox addLayer”方法将这些数据生成到地图上的图层中,而不是标记。

    callbackEventbrite(function(result){
    const keys = Object.values(result);
    for(const key of keys){
        geojson = {
            type: 'featureCollection',
            features: [{
                type: 'feature',
                geometry: {
                    type: 'Point',
                    coordinates: [key.venue.longitude, key.venue.latitude]
                }
            }]
        }
        eventInfo.push(
            {"longitude": key.venue.longitude , "latitude": key.venue.latitude , "name": key.name.text, "venue": key.venue.name, "date": key.start.local, "category": key.category_id}
        );
    }
});

我基本上想要这个,它根据几何坐标在地图上生成符号,但使用 API 数据代替。

map.addLayer({
        "id": "locations",
        "type": "symbol",
        "source": {
            "type": "geojson",
            "data": {
                "type": "FeatureCollection",
                "features": [
                    {
                      "type": "Feature",
                      "properties": {
                        "Title": "The Congress Inn",
                        "Description": "Pub located in Longton",
                        "Type": "Pub",
                        "Address": "14 Sutherland Rd, Stoke-on-Trent ST3 1HJ",
                        "Longitude": 2.1316,
                        "Latitude": 52.9878,
                        "icon": "bar"
                      },
                      "geometry": {
                        "coordinates": [
                          -2.131836,
                          52.987238
                        ],
                        "type": "Point"
                      }
                    },

非常感谢任何帮助!谢谢你

【问题讨论】:

    标签: javascript ajax mapbox layer mapbox-gl-js


    【解决方案1】:

    这应该相当简单,您只需要从 Eventbrite 响应中构建一个特征数组。

    1. 首先构建一组 geojson 功能以在您的源代码中使用。

    2. 然后将源单独添加到地图中,然后再添加图层。您将使用刚刚在源代码中创建的特征数组。

    3. 将源添加到地图后,您可以创建图层并在图层中引用源。

    试试下面的代码让你开始。让我知道它是否适用于您的情况。

    var featureArr;
    callbackEventbrite(function(result) {
      const keys = Object.values(result);
      for (const key of keys) {
        var feature = {
          "type": "Feature",
          "id": key.venue.id,
          "geometry": {
            "type": "Point",
            "coordinates": [key.venue.longitude, key.venue.latitude]
          },
          "properties": {
            "title": key.venue.name,
            "description": key.venue.description,
            "icon": "bar"
          }
        };
        featureArr.push(feature);
      }
    }
    
    
    map.addSource("mySource", {
      "type": "geojson",
      "data": {
        "type": "FeatureCollection",
        "features": featureArr
      }
    });
    
    map.addLayer({
      "id": "locations",
      "type": "symbol",
      "source": "mySource",
      "layout": {
        "text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
        "text-offset": [0, 0.6],
        "text-anchor": "top",
      }
    });

    注意:我不知道 Eventbrite 的响应对象中有什么,所以一些 key.value.xyz 是由变量组成的

    【讨论】:

    • 感谢您的回复!这似乎是正确的方法,但我收到一条错误消息,指出“输入数据不是有效的 geoJSON 对象”。我不确定为什么,因为错误没有给出正在破坏的代码行。
    • 我觉得这里的主要问题之一是从 API 接收到的所有数据都必须在回调中使用。出于某种原因,当在回调中添加图层或源时,Mapbox 似乎不想工作
    • 这应该不是问题。您正在从他们的 api 接收数据,这与您硬编码的数据没有什么不同。也许从 api 收到的数据类型没有被接受为有效的坐标或名称。您能否使用来自 eventbrite 的示例响应或您对 eventbrite 的 ajax 调用来更新您的问题,以便我可以通过响应对其进行测试?
    • @seanrs97 您可以将生成的 geojson 粘贴到 geojson.io 以获得更好的关于正确/错误的反馈。
    猜你喜欢
    • 1970-01-01
    • 2020-03-08
    • 2020-04-09
    • 2020-01-10
    • 1970-01-01
    • 2020-05-20
    • 2021-10-27
    • 2014-11-06
    • 2017-03-25
    相关资源
    最近更新 更多