清除谷歌地图图层时遇到问题

Having issues clearing google maps layer

本文关键字:遇到 问题 图层 谷歌地图 清除      更新时间:2023-09-26

我一直在研究这篇文章和这个jsfiddle。我正在尝试在将新数据填充到地图图层之前清除地图图层,但看起来叠加层只是将一个叠加层堆叠在一起而从未被清除过。

这是我目前存在的代码:

// For the heatmap layer
  var heatmapData = [];
  var heatmap;
  // Instantiate counter
  var files = 0;
  // Looping and loading files with timeout
  (function myLoop (i) {          
    setTimeout(function () {   
      var xhr = new XMLHttpRequest();
      xhr.open('GET', ('js/tweets'+ files + '.json'), true);
      xhr.onload = function() {
        loadTweets(this.responseText);
      };
      xhr.send();
      // Clear out map styles
      heatmap.setMap(null);           
      if (--i) myLoop(i);
      // Stall for 3 seconds
    }, 3000)
  })(100);  
  // Parse out the JSON and create markers
  function loadTweets(results) {
    // Parse out our JSON file
    var tweetStructure = $.parseJSON(results);
    // Go gets it
    for (a in tweetStructure){
      var co_arr = tweetStructure[a];
      for (coords in co_arr.coordinates){
        var d = co_arr.coordinates;
        // Stating our lat/longs 
        var first = d[0];
        var second = d[1];
        var magnitude = d[2];
        // Setting them
        var latLng = new google.maps.LatLng(first, second);
        // Weighted location to express polarity
        var weightedLoc = {
          location: latLng,
          weight: Math.pow(2, magnitude)
        };
        heatmapData.push(weightedLoc);
      }
    }
    // Instantiate heat map
    heatmap = new google.maps.visualization.HeatmapLayer({
      data: heatmapData,
      dissipating: false,
      map: map
    });
  }

我从四个静态 JSON 文件中获取数据。

在声明var d =时,使用第二个for in循环,您似乎并没有在所有[coords]进行迭代。因此,我想d[0]将容纳来自co_arr.coordinates的所有键值对。这可能是问题的一部分吗?