Hello!

I work with the heat map module from Yandex.Maps. There is a page with buttons:

<div id="buttons"> <button id="studii" data-path="data/k0.json">студии</button> <button id="k1" data-path="data/k1.json">1к</button> <button id="k2" data-path="data/k2.json">2к</button> <button id="k3" data-path="data/k3.json">3к</button> <button id="k4" data-path="data/k4.json">4к</button> <button id="k5" data-path="data/k5.json">5к</button> </div> 

where when pressing the buttons, the corresponding heat map is displayed by this script:

 $('#studii, #k1, #k2, #k3, #k4, #k5').on('click', function() { path = this.getAttribute('data-path'); jQuery.getJSON(path, function(json) { var heatmap = new ymaps.Heatmap(json['features'], { radius: 15, dissipating: false, opacity: 0.8, intensityOfMidpoint: 0.2, gradient: { 0.1: 'rgba(128, 255, 0, 0.7)', 0.2: 'rgba(255, 255, 0, 0.8)', 0.7: 'rgba(234, 72, 58, 0.9)', 1.0: 'rgba(162, 36, 25, 1)' } }); heatmap.setMap(myMap); }); }) 

The difficulty is that the heat maps overlap each other. I would like the previous map to disappear. How to do it?

Page: http://yakhshisarova.ru/2/heatmap/

    1 answer 1

    Delete the old heatmap.setMap(null) using heatmap.setMap(null) .

    • I do not understand where to insert this code. I think you first need to check if there is a heat map on the map. Then, if there is, destroy. If not, add. How to check the availability of heat map on the map? - Albert Alexandrov
    • Save heatmap to global variable or closure. Before creating a new heat map, check if there is an old one. If there is - delete. - flapenguin