【发布时间】:2016-01-31 20:53:29
【问题描述】:
使用 google map api 时,我的控制台出现两个错误:
1-Uncaught ReferenceError: 未定义初始化
2-您已在此页面上多次包含 Google Maps API。这可能会导致意外错误 有什么帮助吗?
JS
<script type="text/javascript">
function initialize() {
var myLatLng = {lat: 42.52501, lng: 2.938979};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: myLatLng,
scrollwheel: false,
draggable:true,
});
var image='logo.png';
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon:image
});
}
</script>
HEADER.PHP
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key='YOUR API KEY HERE'"></script>
<script src='https://maps.googleapis.com/maps/api/js?v=3.exp'></script>
<body onload="initialize()";></body>
【问题讨论】:
-
密钥是敏感项目吗?如果是这样,您应该将其删除。否则——没关系。 :-) 好的,我只看了两个
标签。如果您只是打开一个新选项卡并添加这两个 https: 链接,它们都会加载相同的内容。这就是您的重复消息的来源。 -
@MarkManning 来自 Google Maps JavaScript API 的密钥,如果我删除它,将不会显示地图
-
您收到第二个错误,因为您多次包含 API。要修复它,请删除其中一个
-
@hopo :我认为这是您需要 jQuery 的 document.ready 函数来确保在继续之前所有内容都已正确加载的时候之一。我认为这就是参考错误的来源。你也可以使用 jQuery 的 $(window).onload() 函数,而不是把它放在 BODY 标签上。
-
@MarkManning 你能给我举个例子吗?
标签: javascript google-maps google-maps-api-3