javascript - 谷歌地图热图未显示

标签 javascript google-maps

我正在尝试创建一个简单的热图示例,但没有显示任何内容。我没有任何错误,也不知道问题是什么。如果有人可以让我知道他们看到了什么,我将不胜感激。

<!DOCTYPE html>
<html>
<head>
<title>HeatMap</title>
 <script
        src="https://maps.googleapis.com/maps/api/js?key=MYKEY&sensor=false&libraries=visualization"
        type="text/javascript"></script>
<script>
var map;
var heatmapData = [
new google.maps.LatLng(37.782, -122.447),
new google.maps.LatLng(36.778261, -119.417932),
new google.maps.LatLng(-37.814107, 144.96328),
new google.maps.LatLng(33.867487, 151.20699),
{ location: new google.maps.LatLng(40.416775, -3.70379), weight: 6 },
{ location: new google.maps.LatLng(41.385064, 2.173403), weight: 2 },
{ location: new google.maps.LatLng(52.130661, -3.783712), weight: 2 },
{ location: new google.maps.LatLng(55.378051, -3.435973), weight: 8 },
{ location: new google.maps.LatLng(-40.900557, 174.885971), weight: 6 },
{ location: new google.maps.LatLng(40.714353, -74.005973), weight: 6 }
];
function initializeMap() {
var myMapOptions = {
zoom: 4,
center: new google.maps.LatLng(40.785611, -25.94700),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById(‘map’),myMapOptions);
var heatmap = new google.maps.visualization.HeatmapLayer({
data: heatmapData,
dissipating: false,
map: map
});
}
google.maps.event.addDomListener(window, ‘load’, initializeMap);
</script>
</head>
<body>
<div id="map" style="width: 1000px; height: 800px;"></div>
</body>
</html>

最佳答案

您的代码中包含非法字符。 Chrome 控制台在第 28 行报告:Uncaught SyntaxError: Unexpected token ILLEGAL。如果我用单引号替换它们,它对我有用。

两个变化:

  1. google.maps.event.addDomListener(窗口,'加载',initializeMap); ->“加载”
  2. map = new google.maps.Map(document.getElementById('map'),myMapOptions); ->“ map ”
<!DOCTYPE html>
<html>
<head>
<title>HeatMap</title>
 <script
        src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=visualization"
        type="text/javascript"></script>
<script>
var map;
var heatmapData = [
new google.maps.LatLng(37.782, -122.447),
new google.maps.LatLng(36.778261, -119.417932),
new google.maps.LatLng(-37.814107, 144.96328),
new google.maps.LatLng(33.867487, 151.20699),
{ location: new google.maps.LatLng(40.416775, -3.70379), weight: 6 },
{ location: new google.maps.LatLng(41.385064, 2.173403), weight: 2 },
{ location: new google.maps.LatLng(52.130661, -3.783712), weight: 2 },
{ location: new google.maps.LatLng(55.378051, -3.435973), weight: 8 },
{ location: new google.maps.LatLng(-40.900557, 174.885971), weight: 6 },
{ location: new google.maps.LatLng(40.714353, -74.005973), weight: 6 }
];
function initializeMap() {
var myMapOptions = {
zoom: 4,
center: new google.maps.LatLng(40.785611, -25.94700),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'),myMapOptions);
var heatmap = new google.maps.visualization.HeatmapLayer({
data: heatmapData,
dissipating: false,
map: map
});
}
google.maps.event.addDomListener(window, 'load', initializeMap);
</script>
</head>
<body>
<div id="map" style="width: 600px; height: 500px;"></div>
</body>
</html>

working example

代码片段:

html,
body,
#map {
  width: 100%;
  height: 100%;
  margin: 0px;
  padding: 0px;
}
<!DOCTYPE html>
<html>

<head>
  <title>HeatMap</title>
  <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=visualization" type="text/javascript"></script>
  <script>
    var map;
    var heatmapData = [
      new google.maps.LatLng(37.782, -122.447),
      new google.maps.LatLng(36.778261, -119.417932),
      new google.maps.LatLng(-37.814107, 144.96328),
      new google.maps.LatLng(33.867487, 151.20699),
      {
        location: new google.maps.LatLng(40.416775, -3.70379),
        weight: 6
      },
      {
        location: new google.maps.LatLng(41.385064, 2.173403),
        weight: 2
      },
      {
        location: new google.maps.LatLng(52.130661, -3.783712),
        weight: 2
      },
      {
        location: new google.maps.LatLng(55.378051, -3.435973),
        weight: 8
      },
      {
        location: new google.maps.LatLng(-40.900557, 174.885971),
        weight: 6
      },
      {
        location: new google.maps.LatLng(40.714353, -74.005973),
        weight: 6
      }
    ];

    function initializeMap() {
      var myMapOptions = {
        zoom: 4,
        center: new google.maps.LatLng(40.785611, -25.94700),
        mapTypeId: google.maps.MapTypeId.ROADMAP
      };
      map = new google.maps.Map(document.getElementById('map'), myMapOptions);
      var heatmap = new google.maps.visualization.HeatmapLayer({
        data: heatmapData,
        dissipating: false,
        map: map
      });
    }
    google.maps.event.addDomListener(window, 'load', initializeMap);
  </script>
</head>

<body>
  <div id="map"></div>
</body>

</html>

关于javascript - 谷歌地图热图未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20861906/

相关文章:

Javascript 性能 : reduce() vs for-loop

mysql - 在 Google map 上追踪位置路径

javascript - 取消之前调用的 Google Maps geocode 方法

javascript - Chrome 中的完全缓冲视频

reactjs - 使用 React 更新基于地理位置的 Google map

android - 连续跳动标记 : android maps v2

javascript - 从 Google Maps Javascript 中删除标记

javascript - 如何使用 javascript 检查链接是否指向某个站点

javascript - 根据 URL 或 URL 字符串前缀不同的 html、body 背景

php - Javascript eval() 不工作