javascript - Google HeatMap API 抛出 2 条错误消息 : H/M is not defined

标签 javascript google-maps-api-3 heatmap

几个小时以来,我一直在尝试实现一个非常简单的 Google HeatMap 演示。 我几乎复制了演示页面中的所有内容(在 google map api 文档上) 这是 2 条错误消息:

  1. Uncaught ReferenceError: H is not defined HeatMap.html:6
  2. 未捕获的 ReferenceError:M 未定义

enter image description here

这是代码

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js" type="text/javascript"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?libraries=geometry,visualization&sensor=true"></script>
<script type="text/javascript">

    // 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),
      //...........so many data here, skip...........
      new google.maps.LatLng(37.756335, -122.403719),
      new google.maps.LatLng(37.755503, -122.403406),
      new google.maps.LatLng(37.754665, -122.403242),
      new google.maps.LatLng(37.753837, -122.403172),
      new google.maps.LatLng(37.752986, -122.403112),
      new google.maps.LatLng(37.751266, -122.403355)
    ];

    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 });
    }

    $(document).load(function () {

    });

    $(document.body).on("onload", initialize);

</script>

<script type="text/javascript" charset="UTF-8" src="https://maps.gstatic.com/cat_js/intl/en_us/mapfiles/api-3/11/9/%7Bcommon,map,util,onion,visualization_impl%7D.js"></script>
<script type="text/javascript" charset="UTF-8" src="https://maps.gstatic.com/cat_js/intl/en_us/mapfiles/api-3/11/9/%7Bstats,controls%7D.js"></script>
<script type="text/javascript" charset="UTF-8" src="https://maps.gstatic.com/cat_js/intl/en_us/mapfiles/api-3/11/9/%7Bmarker%7D.js">

</script>
</head>
<body onload="initialize()">
    <div id="map_canvas"></div>
</body>
</html>

最佳答案

我不确定这会有多大帮助,但是 here's a working version它依赖于一组非常不同的库依赖项。源代码中的那些让我觉得很奇怪;他们似乎依靠非常奇怪的技术来初始化 map 。

如果您不介意我问,您从哪里获得您所依赖的代码?我使用的修改后的依赖项来了 from here ,而且我不相信我见过任何与您之前从谷歌发布的内容非常相似的东西......它似乎依赖于修改后的 eval 形式来创建 map 库,这将是非常不寻常的。总的来说,这个例子在功能上和你的很相似,只是图书馆的 build 似乎有所不同。

关于javascript - Google HeatMap API 抛出 2 条错误消息 : H/M is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14825181/

相关文章:

javascript - 如何混合 100% 高度和最小高度

javascript - 在 html/css 中调整图像大小和居中?

javascript - 数组未分配给变量?它是如何工作的以及它到底在做什么?

Javascript getElementById 行为奇怪

javascript - 检查位置是否在收费公路上

javascript - Google map 无法正确显示

r - ggplot2:快速热图绘制、 reshape ?

R - 将多个热图堆叠在一起

javascript - 谷歌地图地址自动填充错误 : Cannot read property 'length' of undefined

python - python 中的极地热图