从Google地图中移除HeatmapLayer [英] Remove HeatmapLayer from google maps
问题描述
我使用的是HeatmapLayer api https://developers.google.com / maps / documentation / javascript / layers#JSHeatMaps
我生成这样的热图:
heatmap = new google.maps.visualization.HeatmapLayer({
data:heatmapData,
radius:50
});
heatmap.setMap(google_map);
当我想使用与上面相同的函数显示另一个热图(我使用ajax和我有新的标记显示,所以我也需要更改热图)热图层停留在地图上,在这种情况下,我的地图上有2个重叠的热图。我如何才能先移除当前的heatmaplayer?
这里是我的演示代码,如果你点击地图下方的链接,就会添加热图,点击它再次,它应该删除它,但它只是一遍又一遍地重复:
文档建议您可以移除图层通过调用 heatmap.setMap(null)
更新
在您的jsfiddle 中,您声明了您的热图
变量在每个的范围内点击
事件。为了使你的代码能够工作,我把 heatmap
变量全局存在,然后检查以确保新的 heatmap
没有覆盖现有的。
I'm using the HeatmapLayer api https://developers.google.com/maps/documentation/javascript/layers#JSHeatMaps
I generate the heatmap like this:
heatmap = new google.maps.visualization.HeatmapLayer({
data: heatmapData,
radius: 50
});
heatmap.setMap(google_map);
When i want to display another heat map using the same function as above (i'm using ajax and i have new markers to display, so i need to change the heatmap too) the heat map layer stays on the map and in this case i have 2 overlapping heatmaps on my map. How can i remove the current heatmaplayer first?
Here is my demo code, if you click on the link below the map, the heatmap is added, of you click on it again, it should remove it, but its just duplicated over and over again:
The docs suggest you can remove a layer by calling heatmap.setMap(null)
Update
In your jsfiddle you were declaring your heatmap
variable in the scope of each click
event. To make your code work I moved the heatmap
variable to exist globally, and then checked to make sure that a new heatmap
did not overwrite an existing one.
这篇关于从Google地图中移除HeatmapLayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!