html - IDK 为什么我的 mapbox 地图不是交互式的。县城应该可以点击
问题描述
所以我很确定问题出在代码的以下部分中。更具体地说,点击地图功能。但我不知道为什么。它应该让我地图上的每个县都能够被点击并将他们的数据显示在侧边栏上。但是我的县都不是可点击的。
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';
}
编辑: 访问令牌设置为只读,所以我不担心任何人能够修改任何东西,而且我的地图样式无论如何都是私有的。如果我认为问题出在哪里是错误的,那么有人无法指出。
解决方案
通过以下方式修复它:
features=features[0]
在 map.queryRenderedFeatures 行之后添加- 摆脱了
for i in features
循环 - 将所有提及的内容更改
features[i]
为 justfeatures
。
推荐阅读
- ios - Swift 包管理器不允许构建包含 `import AppKit` 的包
- sql-server - 尝试通过 SQL Server Management Studio 在 Integration Services 目录中查看标准报告的执行时出错
- javascript - 无法在 JavaScript 中为 Windows.Web.Http.HttpClient 添加超时
- c# - 如何在变换旋转时以特定角度更改变换名称?
- python-3.x - ubuntu SessionNotCreatedException 上的 Selenium webdriver Python
- javascript - VSCode 自动附加显示节点内部
- entity-framework-core - Entity-Framework-Core:使用复合键映射 1 对 1 关系
- google-apps-script - Google 脚本中的时间戳
- python-3.x - 如何在抓取 Web 数据时处理空列表项?
- mongodb - 使用 MongoDB 解析名称服务器 Xamarin.Forms Android 时出错