【问题标题】:Mapbox popup displays "unknown" instead of propertiesMapbox 弹出窗口显示“未知”而不是属性
【发布时间】:2019-03-15 14:09:25
【问题描述】:

点击一条街道(应该显示带有属性的弹出窗口)后,弹出窗口显示“未知”。应该怎么做才能使弹出窗口显示属性?如果有人知道要更改什么或弹出窗口无法正常工作的原因,我将不胜感激!

<div class="container" style="background-color:#F6F3F3">
  <div id='map' style='width: 100%; height: 900px;'></div>
  <script>
    mapboxgl.accessToken =

      var map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/bisqpski/cjssto8kw77g11gk4rwur575q'
      });


    map.on('load', function() {
      // Add a layer showing the state polygons.
      map.addLayer({
        'id': 'kazimierz-tileset',
        'type': 'fill',
        'source': {
          "type": "Feature",
          "properties": {
            "Street": "Świętego Wawrzyńca",
            "Probability": "13%"
          },
          "geometry": {
            "coordinates": [
              [19.944511, 50.049316],
              [19.94617, 50.049681],
              [19.946307, 50.049719],
              [19.947699, 50.050025],
              [19.948851, 50.050282],
              [19.949689, 50.050456],
              [19.951076, 50.05076],
              [19.951401, 50.050831]
            ],
            "type": "LineString"
          }
        }
      });

      // When a click event occurs on a feature in the states layer, open a popup 
      at the
      // location of the click, with description HTML from its properties.
      map.on('click', 'kazimierz-tileset', function(e) {
        new mapboxgl.Popup()
          .setLngLat(e.lngLat)
          .setHTML(e.features[0].properties.name)
          .addTo(map);
      });

      // Change the cursor to a pointer when the mouse is over the states layer.
      map.on('mouseenter', 'kazimierz-tileset', function() {
        map.getCanvas().style.cursor = 'pointer';
      });

      // Change it back to a pointer when it leaves.
      map.on('mouseleave', 'kazimierz-tileset', function() {
        map.getCanvas().style.cursor = '';
      });
    });
  </script>
</div>

【问题讨论】:

  • 无关:如果我是你,我会从你的问题中删除你的个人 Mapbox 令牌代码。

标签: popup mapbox mapbox-gl-js


【解决方案1】:

您没有 name 作为您的功能的属性。只有街道和概率。所以你正在调用一个不存在的属性。使用街道或任意定义属性名称。

  "properties": {
    "Street": "Świętego Wawrzyńca",
    "Probability": "13%"
    "Name": "Your Name Here"
  },

或者只使用街道属性。

.setHTML(e.features[0].properties.Street)

片段:

<div class="container" style="background-color:#F6F3F3">
  <div id='map' style='width: 100%; height: 900px;'></div>
  <script>
    mapboxgl.accessToken =

      var map = new mapboxgl.Map({
        container: 'map',
        style: 'mapbox://styles/bisqpski/cjssto8kw77g11gk4rwur575q'
      });


    map.on('load', function() {
      // Add a layer showing the state polygons.
      map.addLayer({
        'id': 'kazimierz-tileset',
        'type': 'fill',
        'source': {
          "type": "Feature",
          "properties": {
            "Street": "Świętego Wawrzyńca",
            "Probability": "13%"
          },
          "geometry": {
            "coordinates": [
              [19.944511, 50.049316],
              [19.94617, 50.049681],
              [19.946307, 50.049719],
              [19.947699, 50.050025],
              [19.948851, 50.050282],
              [19.949689, 50.050456],
              [19.951076, 50.05076],
              [19.951401, 50.050831]
            ],
            "type": "LineString"
          }
        }
      });

      // When a click event occurs on a feature in the states layer, open a popup 
      at the
      // location of the click, with description HTML from its properties.
      map.on('click', 'kazimierz-tileset', function(e) {
        new mapboxgl.Popup()
          .setLngLat(e.lngLat)
          .setHTML(e.features[0].properties.Street)
          .addTo(map);
      });

      // Change the cursor to a pointer when the mouse is over the states layer.
      map.on('mouseenter', 'kazimierz-tileset', function() {
        map.getCanvas().style.cursor = 'pointer';
      });

      // Change it back to a pointer when it leaves.
      map.on('mouseleave', 'kazimierz-tileset', function() {
        map.getCanvas().style.cursor = '';
      });
    });
  </script>
</div>

最后,在您的示例中,您要添加一个图层。但是您已经在您创建的样式中添加了具有相同名称的图层。您无需再次添加它,否则您将获得Error: Layer with id "kazimierz-tileset" already exists on this map,因为它自然会直接从样式中提取。确保重命名图层或删除addLayer

【讨论】:

  • 嘿,非常感谢!我理解我的错误,但还有一个问题要问你:我希望我的弹出窗口显示两个属性:街道名称,因此它是:e.features[0].properties.Street 和概率,我的代码现在看起来像这样:.setHTML (e.features[0].properties.Street + ' ' + e.features[0].properties.Probability)。但它不起作用。弹出窗口显示街道名称,但之后它再次“未定义”但我定义了它。你知道怎么解决吗?
  • 另外,我不太了解 map.addlayer 函数。图层的 id 是 kazimierz-tileset-2 ,它的源是一个名为“Kazimierz-_tileset”的图块集,其中需要包含在弹出窗口中的所有数据。 (名称和概率)。如果我删除了这一层,那么我就没有数据可以借鉴了。你能解释一下我该怎么做吗?如果给我添麻烦了,我很抱歉,但我真的需要你的帮助!
猜你喜欢
  • 1970-01-01
  • 2017-03-26
  • 1970-01-01
  • 1970-01-01
  • 2022-08-22
  • 1970-01-01
  • 2018-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多