Google map api initialize:initialize未定义

时间:2016-02-14 16:51:23

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

使用google map api时我的控制台出错:-Uncaught ReferenceError:未定义initialize 有什么帮助吗?

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>

1 个答案:

答案 0 :(得分:2)

你打电话给谷歌地图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>