2016-07-28 205 views
0

我得到一個谷歌地圖錯誤「沒有定義谷歌」谷歌地圖V3錯誤

我使用的是API密鑰谷歌地圖V3有人點我了這裏的問題。

我之前使用沒有API密鑰的谷歌地圖,它工作正常。但是,我添加API密鑰後,它似乎並沒有工作。

<script async defer src="https://maps.googleapis.com/maps/api/js?key=MY-API-KEY&callback=initMap"></script> 

<script> 
    $(document).ready(function() { 
     // initialize Google Maps 
     initialize(); 
     // save marker to database 
     $('#submitButton').click(function() { 
     // we read the position of the marker and send it via AJAX 
     var position = marker.getPosition(); 
     $.ajax({ 
      url: 'update_map.php', 
      type: 'post', 
      data: { 
      lat: position.lat(), 
      lng: position.lng(), 
      id : <?php echo $id;?> 
      }, 
      success: function(response) { 
      // we print the INSERT query to #display 
      $('#output').html(response); 
      } 
     }); 
     }); 

    }); 




    var map = null; 
    var marker = null; 

    // Google Maps 
    function initialize() { 
     var startDragPosition = null; 
     var mapOptions = { 
     zoom: 15, 
     center: new google.maps.LatLng(<?php echo $lat;?>, <?php echo $long;?>), // Over Belgium 
     mapTypeId: google.maps.MapTypeId.TERRAIN 
     }; 
     map = new google.maps.Map(document.getElementById('map-big'), mapOptions); 
     // set the new marker 
     marker = new google.maps.Marker({ 
     position: new google.maps.LatLng(<?php echo $lat;?>, <?php echo $long;?>), 
     map: map, 
     draggable: true 
     }); 

     var myGeocoder = new google.maps.Geocoder(); 

     // set a callback for the start and end of dragging 
     google.maps.event.addListener(marker,'dragstart',function(event) { 
     // we remember the position from which the marker started. 
     // If the marker is dropped in an other country, we will set the marker back to this position 
     startDragPosition = marker.getPosition(); 
     }); 
     google.maps.event.addListener(marker,'dragend',function(event) { 
     // now we have to see if the country is the right country. 
     myGeocoder.geocode({'latLng': marker.getPosition()}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK && results[1]) { 
      var countryMarker = addresComponent('country', results[1], true); 

      } 
      else { 
      // geocoder didn't find anything. So let's presume the position is invalid 
      marker.setPosition(startDragPosition); 
      } 
     }); 
     }); 
    } 

    function addresComponent(type, geocodeResponse, shortName) { 
     for(var i=0; i < geocodeResponse.address_components.length; i++) { 
     for (var j=0; j < geocodeResponse.address_components[i].types.length; j++) { 
      if (geocodeResponse.address_components[i].types[j] == type) { 
      if (shortName) { 
       return geocodeResponse.address_components[i].short_name; 
      } 
      else { 
       return geocodeResponse.address_components[i].long_name; 
      } 
      } 
     } 
     } 
     return ''; 
    } 
</script? 

回答

1

您在腳本中調用標籤callback=initMap但沒有在JavaScript代碼的任何initMap功能。取而代之的是一個initialize()函數。 我想你應該改變其中的一個

+0

你是一個救星。謝謝你,先生。它的工作就像一個魅力。 – Jordyn