javascript - 谷歌地图API初始化: initialize is not defined

标签 javascript google-maps google-maps-api-3

使用 google map api 时,我的控制台出现错误:-Uncaught ReferenceError:初始化未定义 有什么帮助吗?

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>

最佳答案

您调用了两次谷歌地图API,主体没有代码,并且不清楚带有初始化函数的脚本放置在哪里...

我对你的代码进行了一些操作..

HEADER.PHP

<script src='https://maps.googleapis.com/maps/api/js?v=3.exp'></script>

<body>

  <div id="map" style="height:500px; width:500px;"></div>
  <script>

    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

      });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
  </script>
</body>

关于javascript - 谷歌地图API初始化: initialize is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35394383/

相关文章:

java - Google Places 的搜索功能无法检索与 Google map 相同的数据

ios - 在 iOS 中获取谷歌地图的中心

javascript - 不能 console.log 事件目标名称

javascript - Ajax 文件上传, 'progress' 上传事件不会在 Edge 浏览器中触发

javascript - 为什么我在这里得到无效日期和 NaN ?

r - 地理编码结果与谷歌地图不同

google-maps - 通过搜索框添加 Google Maps V3 用户输入

javascript - PrimeFaces gmap 组件中的 fitBounds

php - 将更新的谷歌地图可拖动标记的纬度和经度传递到另一个页面

javascript - 如何让我的字母对单词的顺序不敏感