window.Map不是Google Maps API v3中的构造函数 [英] window.Map is not a constructor in Google Maps API v3

查看:84
本文介绍了window.Map不是Google Maps API v3中的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有新密钥的google map

I have a google map with a new key

但是在控制台中出现错误

but it gets error in console

错误:

Uncaught TypeError: window.Map is not a constructor
    at Zr (map.js:2)
    at ds.release (map.js:53)
    at gs (map.js:5)
    at _.rl.Ab (map.js:59)
    at map.js:46

HTML

<head>
<script async defer
        src="https://maps.googleapis.com/maps/api/js?key=mykey">
</script>
</head>

<body onload="init()">
    <div id="map_canvas"></div>
</body>
<script src="js/index.js"></script>

index.js

Map = null;
function init() {
    var mapOptions = {***};
    Map = new google.maps.Map( document.getElementById( "map_canvas" ), mapOptions );
}

感谢您的时间!

推荐答案

地图在EcmaScript中是保留字,因此您不应将其用作变量名:

Map is reserved word in EcmaScript, so you shouldn't use it as a variable name: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

问题是这一行(jsfiddle认为Map是只读的):

The problem is this line (jsfiddle thinks Map is read only):

Map =  null;

将地图变量重命名为其他名称(如map,用小写的"m"表示).

Rename your map variable to something else (like map, with a lower case "m").

概念提琴证明

代码段:

map = null;

function init() {
  var mapOptions = {
    center: {
      lat: 0,
      lng: 0
    },
    zoom: 1
  };
  map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
}
google.maps.event.addDomListener(window, "load", init);

html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}

<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map_canvas"></div>

这篇关于window.Map不是Google Maps API v3中的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆