1

我在这里添加了我的全部编码。他们可能看起来和你们很相似,因为我已经根据地震可视化教程构建了项目。该教程非常好,但就目前而言,我正在尝试获取一个按钮或一个复选框来在不同的叠加层之间切换。

我已将我的代码分成 4 个部分。加热、标记、圆圈和热图。如果有人可以帮助我真的会很高兴,因为我确实在外面尝试了许多教程或示例,但它不是很成功。

这是我的代码

标题

<script>
    var map;
    var results;

    //setting up map
    function initialize() {
      var mapOptions = {
            zoom: 14,
            center: new google.maps.LatLng(-27.48939, 153.012772),
            mapTypeId: google.maps.MapTypeId.ROADMAP 
          };

      map = new google.maps.Map(document.getElementById('map_canvas'),
          mapOptions);

      //set location of file
      var script = document.createElement('script');
          script.src = '\week.json';
          document.getElementsByTagName('head')[0].appendChild(script); 
    }



 var infowindow = new google.maps.InfoWindow({});
    //createMarker function
    function createMarker(latLng, title, content,icon) {
            var marker = new google.maps.Marker({
              position: latLng,
              map: map,
              title: title,
            });


    //click or moveover listener for infowindow
            google.maps.event.addListener(marker, "click", function() {
                infowindow.setContent(content);
                infowindow.open(map, marker);
            });

    }

带有标记的信息窗口

var infowindow = new google.maps.InfoWindow({});

function createMarker(latLng, title, content,icon) {
        var marker = new google.maps.Marker({
          position: latLng,
          map: map,
          title: title,
        });

    window.eqfeed_callback = function(results) {
      var bounds=new google.maps.LatLngBounds();
      for (var i = 0; i < results.features.length; i++) {

        var wifin = results.features[i]; 
        var coords = wifin.geometry.coordinates; 
        var latLng = new google.maps.LatLng(coords[0],coords[1]); 
        bounds.extend(latLng);

        var content ="<div style='height:100px; width:300px; overflow:auto;'><table>";
        content += "<tr><th align='left'>WifiMacAddress</th><td>"+wifin.properties.WifiMacAddress+"</td></tr>";
        content += "<tr><th align='left'>SSID</th><td>"+wifin.properties.SSID+"</td></tr>";
        content += "<tr><th align='left'>SignalStrength</th><td>"+wifin.properties.SignalStrength+"</td></tr>";
        content += "<tr><th align='left'>WifiFrequency</th><td>"+wifin.properties.WifiFrequency+"</td></tr>";
        content +="</table>";

        createMarker(latLng,wifin.WifiMacAddress,content);
      }
      map.fitBounds(bounds);
    }

圆圈大小

window.eqfeed_callback = function(results) {
  for (var i = 0; i < results.features.length; i++) {
     var wifin = results.features[i]; 
        var coords = wifin.geometry.coordinates; 
        var latLng = new google.maps.LatLng(coords[0],coords[1]); 
        //bounds.extend(latLng);
    var marker = new google.maps.Marker({
      position: latLng,
      map: map,
      icon: getCircle(wifin.properties.SignalStrength)
    });
  }
}

function getCircle(strength) {
  return {
    path: google.maps.SymbolPath.CIRCLE,
    fillColor: 'green',
    fillOpacity: .2,
    scale: Math.pow(2, strength) / Math.PI,
    strokeColor: 'white',
    strokeWeight: .5
  };
}

热图

window.eqfeed_callback = function(results) {
var heatmapData = [];
  for (var i = 0; i < results.features.length; i++) {
     var wifin = results.features[i]; 
        var coords = wifin.geometry.coordinates; 
        var latLng = new google.maps.LatLng(coords[0],coords[1]); 
        var magnitude = wifin.properties.SignalStrength;
    var weightedLoc = {
      location: latLng,
      weight: Math.pow(2, magnitude)
    };
    heatmapData.push(weightedLoc);
  }
     var heatmap = new google.maps.visualization.HeatmapLayer({
      //position: latLng,
      data: heatmapData,
      dissipating: false,
      map: map
    });
  }

google.maps.event.addDomListener(window, 'load', initialize);
</script>
4

1 回答 1

0

这会切换Google Earthquake 示例中的热图(如果我将“热图”变量设为全局变量以及地图)

<input type="button" id="toggle" value="toggle layer" onclick="if (heatmap.getMap() == null) {heatmap.setMap(map) } else {heatmap.setMap(null)}"></input>

工作示例

于 2013-10-06T18:19:26.883 回答