我需要根据空气质量指数(AQI)为不同的点着色,以生成热力图。这是我创建热力图的第一次尝试,我的主管坚持要求我只使用谷歌热力图。我从Python视图传递一个JSON对象到我的HTML模板中获取数据。我了解到谷歌热力图是基于密度而非值,但在我的情况下密度并不是一个因素。我一直在搜索类似的问题:如何为谷歌热力图上的不同区域设置不同颜色,但目前还没有人回答它。
我想根据以下AQI值为点着色:
我在尝试调整权重以使其反映值的变化,而不是密度的变化,但我仍然没有得到期望的结果。
非常感谢您的所有帮助。
我想根据以下AQI值为点着色:
- 0-50 绿色
- 51-100 黄色
- 101-150 橙色
- 151-200 红色
- 201-300 紫色
{
"lat": 44.0682019,
"data_value": {"pm10": "0","pm25": "21"},
"lon": -114.7420408,
"aqi": 70.0
}
这是我的JavaScript代码:
<script>
var json_data = {{ data|safe }};
var map, heatmap;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 2,
center: {lat: 0, lng: 0},
minZoom: 2,
maxZoom: 18
});
heatmap = new google.maps.visualization.HeatmapLayer({
data: getPoints(),
radius: 50,
dissapating: false,
map: map
});
}
function changeGradient() {
var gradient = [
'rgba(0, 255, 255, 0)',
'rgba(0, 255, 255, 1)',
'rgba(0, 191, 255, 1)',
'rgba(0, 127, 255, 1)',
'rgba(0, 63, 255, 1)',
'rgba(0, 0, 255, 1)',
'rgba(0, 0, 223, 1)',
'rgba(0, 0, 191, 1)',
'rgba(0, 0, 159, 1)',
'rgba(0, 0, 127, 1)',
'rgba(63, 0, 91, 1)',
'rgba(127, 0, 63, 1)',
'rgba(191, 0, 31, 1)',
'rgba(255, 0, 0, 1)'
]
heatmap.set('gradient', heatmap.get('gradient') ? null : gradient);
}
function changeRadius() {
heatmap.set('radius', heatmap.get('radius') ? null : 50);
}
function changeOpacity() {
heatmap.set('opacity', heatmap.get('opacity') ? null : 0.2);
}
function getPoints() {
var location = [];
for(var i = 0; i < json_data.length; i++) {
var obj = json_data[i];
if(obj.aqi < 150) {
location[i] = {location: new google.maps.LatLng(obj.lat, obj.lon), weight:Math.pow(obj.aqi, 1)};
} else if(obj.aqi > 150 && obj.aqi < 300) {
location[i] = {location: new google.maps.LatLng(obj.lat, obj.lon), weight:Math.pow(obj.aqi, 5)};
} else {
location[i] = {location: new google.maps.LatLng(obj.lat, obj.lon), weight:Math.pow(obj.aqi, 10)};
}
}
return location;
}
</script>
我在尝试调整权重以使其反映值的变化,而不是密度的变化,但我仍然没有得到期望的结果。
非常感谢您的所有帮助。