google-maps-api-3 - 带有标记的 Fusion Tables 热图图层

标签 google-maps-api-3 google-maps-markers google-fusion-tables heatmap

我正在尝试创建与此类似的东西,但在实现此功能时遇到一些问题:

https://google-developers.appspot.com/maps/documentation/javascript/examples/layer-heatmap

我在 Fusion Tables 中存储了大约 35k 条记录,我想用它们来创建热图。数据集包含纬度、经度和计数。

我还想在热图之上添加另一个图层,该图层将在 map 上显示为标记(该数据集也存储在 Fusion Tables 中,大约 600 行。如何用我的坐标替换下面的出租车数据融合表?

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Google Maps JavaScript API v3 Example: Heatmap Layer</title>
    <link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet">
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=visualization"></script>
    <script>
      // Adding 500 Data Points
      var map, pointarray, heatmap;

      var taxiData = [
        new google.maps.LatLng(37.782551, -122.445368),
        new google.maps.LatLng(37.782745, -122.444586),
        new google.maps.LatLng(37.782842, -122.443688),
        new google.maps.LatLng(37.782919, -122.442815),
        new google.maps.LatLng(37.782992, -122.442112),
        new google.maps.LatLng(37.783100, -122.441461),
        new google.maps.LatLng(37.783206, -122.440829)
      ];

      function initialize() {
        var mapOptions = {
          zoom: 13,
          center: new google.maps.LatLng(37.774546, -122.433523),
          mapTypeId: google.maps.MapTypeId.SATELLITE
        };

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

        pointArray = new google.maps.MVCArray(taxiData);

        heatmap = new google.maps.visualization.HeatmapLayer({
          data: pointArray
        });

        heatmap.setMap(map);
      }

      function toggleHeatmap() {
        heatmap.setMap(heatmap.getMap() ? null : 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.setOptions({
          gradient: heatmap.get('gradient') ? null : gradient
        });
      }

      function changeRadius() {
        heatmap.setOptions({radius: heatmap.get('radius') ? null : 20});
      }

      function changeOpacity() {
        heatmap.setOptions({opacity: heatmap.get('opacity') ? null : 0.2});
      }
    </script>
  </head>

  <body onload="initialize()">
    <div id="map_canvas" style="height: 600px; width: 800px;"></div>
    <button onclick="toggleHeatmap()">Toggle Heatmap</button>
    <button onclick="changeGradient()">Change gradient</button>
    <button onclick="changeRadius()">Change radius</button>
    <button onclick="changeOpacity()">Change opacity</button>
  </body>
</html>

最佳答案

对于融合表,您不创建 HeatmapLayer,而是创建 FusionTablesLayer like so :

layer = new google.maps.FusionTablesLayer({
  query: {
    select: 'LATITUDE',
    from: '0ILwUgu7vj0VSZnVzaW9udGFibGVzOjEzNjcwNQ'
  },
  heatmap: {
    enabled: true
  }
});
layer.setMap(map);

您可以通过向查询添加 where: 部分来限制使用的数据。

要使用融合表创建具有简单标记的图层,只需省略 heatmap: {enabled: true } 部分。

关于google-maps-api-3 - 带有标记的 Fusion Tables 热图图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14877746/

相关文章:

jquery - Google map API,隐藏/显示标记

android - 在android中更新 map 标记

javascript - 多层谷歌融合表启用MapTips

javascript - for循环创建多个元素?

php - 使用OAuth验证应用程序以访问融合表

google-maps - 如何隐藏或显示 Google map 图层?

javascript - Uncaught Error : google is not defined for Google Map API geocoder

google-maps - 加载任意地址的简单地理编码示例

javascript - 从谷歌地图中删除现有的折线

javascript - 谷歌地图中 MarkerClusterer 上的信息窗口