【问题标题】:IDK why my mapbox map isn't interactive. The counties should be able to clickedIDK 为什么我的 mapbox 地图不是交互式的。县城应该可以点击
【发布时间】:2021-04-30 16:10:01
【问题描述】:

所以我很确定问题出在以下代码部分中。更具体地说,点击地图功能。但我不知道为什么。它应该让我地图上的每个县都能够被点击并将他们的数据显示在侧边栏上。但是我的县没有一个是可点击的。

map.on('load', function() {   // Map legend labelling and interaction with sidebar
  // legend labelling
  var layers = ['< 4.5%', '4.5% to 6%', '6% to 7.5%', '7.5% to 9%', '9% to 10.5%', '10.5% to 12%', '12% to 13.5%','13.5% to 15%','> 15%'];
  var colors = ['#fff2b8', '#FED976', '#FEB24C', '#FD8D3C', '#FC4E2A', '#E31A1C', '#BD0026', '#8E0101', '#49000E'];
  for (i = 0; i < layers.length; i++) {
    var layer = layers[i];
    var color = colors[i];
    var item = document.createElement('div');
    var key = document.createElement('span');
    key.className = 'legend-key';
    key.style.backgroundColor = color;
    var value = document.createElement('span');
    value.innerHTML = layer;
    item.appendChild(key);
    item.appendChild(value);
    legend.appendChild(item);
  }
  
  // This for the life of me just refues to run. Something with the layer name, id is making it not run the clicking functionality.
  map.on('click', function (e) {
    var features = map.queryRenderedFeatures(e.point, { layers: ["countyData"] });
    for (i in features) {
      if (features[i].layer.id == "countyData") {
         document.getElementById('tooltip').innerHTML = 
         ('<b>Selected County:</b>' + (features[i].properties.COUNTY_LAB).toFixed(0) + '<br/> ' + 
         '<br/>Total Population in 2014:  ' + (features[i].properties.totPop14).toFixed(0) + 
         '<br/>Percent of Population in Poverty 2014:  ' + (features[i].properties.povPercent14).toFixed(0) + '%' + 
         '<br/>Total Population in 2019:  ' + (features[i].properties.totalPop19).toFixed(0) + 
         '<br/>Percent of Population in Poverty 2019:  ' + (features[i].properties.povPercent19).toFixed(0) + '%' + 

         '<br/> ' + '</br><b>Change for each Race:</b>' + '<br/> ' + 

         '<br/>Change in total White population:  ' + (features[i].properties.whiteTotDelta).toFixed(1) + 
         '<br/>Change in White population in Poverty:  ' + (features[i].properties.whitePovDelta).toFixed(1) + '<br/> ' + 

         '<br/>Change in total Hispanic population:  ' + (features[i].properties.hispTotDelta).toFixed(1) + 
         '<br/>Change in Hispanic population in Poverty:  ' + (features[i].properties.hispPovDelta).toFixed(1) + '<br/>' +

         '<br/>Change in total Black population:  ' + (features[i].properties.blackTotDelta).toFixed(1)  + 
         '<br/>Change in Black population in Poverty:  ' + (features[i].properties.blackPovDelta).toFixed(1) + '<br/> ' + 

         '<br/>Change in total Asian population:  ' + (features[i].properties.asianTotDelta).toFixed(1)  + 
         '<br/>Change in Asian population in Poverty:  ' + features[i].properties.asianPovDetlta).toFixed(1) + '<br/> ' + 

         '<br/>Change in total Native American population:  ' + (features[i].properties.nativeTotDelta).toFixed(1) + 
         '<br/>Change in Native American population in Poverty:  ' + (features[i].properties.nativePovDelta).toFixed(1) + '<br/> ' +

         '<br/>Change in total Other population:  ' + (features[i].properties.otherTotDelta).toFixed(1)  + 
         '<br/>Change in Other population in Poverty:  ' + (features[i].properties.otherPovDelta ).toFixed(1);
        
         if (clickedStateId) {
           map.setFeatureState({sourceLayer: "County_Boundaries_of_NJ_EDITE-5q8kmm", id: clickedStateId, source:"composite"}, {"click":false})
        }
        clickedStateId = features[i][id];
        map.setFeatureState({sourceLayer: "County_Boundaries_of_NJ_EDITE-5q8kmm", id: clickedStateId, source:"composite"}, {"click":true})
        return;
      }
    }
 });
     map.getCanvas().style.cursor = 'default';
}

编辑: 访问令牌设置为只读,所以我不担心任何人能够修改任何内容,而且我的地图样式无论如何都是私有的。如果我认为问题出在哪里是错误的,那么有人无法指出。

【问题讨论】:

  • 您好,请创建minimal reproducible example
  • 有点难以用我的 mapbox 来仅显示与更多相关的 sn-p(显示 choropleth 地图,具有包含点击功能的图例值),所以我添加了一个带有我的 html 代码的 pastebin 作为问题的编辑消息。

标签: html mapbox


【解决方案1】:

通过以下方式修复它:

  1. 在 map.queryRenderedFeatures 行之后添加 features=features[0]
  2. 摆脱了for i in features 循环
  3. 将所有提及的features[i] 更改为仅features

【讨论】:

    猜你喜欢
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多