2016-04-22 146 views
0

閱讀了一段時間後...我想開始使用Google Maps API for Javascript。我在他們的網站上獲得了關鍵字,我嘗試了很多方法來創建地圖,但現在我想通過先前的「地點」創建地圖,所以我首先使用Geocoder.geocode(),然後創建地圖,昨晚它正在工作。谷歌未定義(api地圖錯誤)

所以,我決定開始在這些地圖中使用覆蓋(標記),但我不知道我得到了未捕獲的錯誤:google沒有定義。

我讀了一些關於這一點,我知道大多數的時間大約是異步的問題,但是,我仍然不知道如何解決它,這裏是代碼:

<!DOCTYPE html> 
<html> 
    <head> 
    <style type="text/css"> 
     html, body { height: 100%; margin: 0; padding: 0; } 
     #map { height: 100%; } 
    </style> 

    </head> 


    <body> 

    <div id="map"></div> 

    <script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script> 
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAeaWLxSHFEdwWEVVYajslt7R9eP0ZpLXQ&callback=initMap"></script> 

    <script type="text/javascript"> 

     var sLocation = "Castillejos 265 Barcelona"; 
     var sLocationToSearch =sLocation.split(' ').join('+'); 

     $.ajax({ 
      type: 'GET', 
      url: 'https://maps.googleapis.com/maps/api/geocode/json?address='+ sLocationToSearch +'&key=AIzaSyAeaWLxSHFEdwWEVVYajslt7R9eP0ZpLXQ', 

      success: function(res){ 
       // ParseFloat the <latitud> and <longitud> 
       //var myLatlng = new google.maps.LatLng(parseFloat(trader.geo.lat),parseFloat(trader.geo.lon)); 

       initMap(res.results[0].geometry.location); 
      }, 
      error: function(error){ 
       console.log(error); 
      } 
     }); 

     function initMap(oLatlng){ 
      var map = new google.maps.Map(document.getElementById('map'), { 
       center: oLatlng, 
       zoom: 15 
      }); 
     } 

    </script> 
    </body> 
</html> 

感謝了很多您的幫助頁面後

+0

每當有人有這樣的問題加載,它是ALW ays,因爲在腳本標籤中有'async','defer'和'&callback = initMap'。你在哪裏找到這個代碼? – cdm

回答

0

調用JavaScript是使用的window.onload

<!DOCTYPE html> 
<html> 
<head> 
    <style type="text/css"> 
     html, body 
     { 
      height: 100%; 
      margin: 0; 
      padding: 0; 
     } 

     #map 
     { 
      height: 100%; 
     } 
    </style> 

</head> 


<body> 

<div id="map"></div> 

<script src="https://code.jquery.com/jquery-2.2.3.min.js" integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo=" crossorigin="anonymous"></script> 
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAeaWLxSHFEdwWEVVYajslt7R9eP0ZpLXQ&callback=initMap"></script> 

<script type="text/javascript"> 
    window.onload = function() 
    { 
     var sLocation = "Castillejos 265 Barcelona"; 
     var sLocationToSearch = sLocation.split(' ').join('+'); 

     $.ajax({ 
      type : 'GET', 
      url : 'https://maps.googleapis.com/maps/api/geocode/json?address=' + sLocationToSearch + '&key=AIzaSyAeaWLxSHFEdwWEVVYajslt7R9eP0ZpLXQ', 

      success : function(res) 
      { 
       // ParseFloat the <latitud> and <longitud> 
       //var myLatlng = new google.maps.LatLng(parseFloat(trader.geo.lat),parseFloat(trader.geo.lon)); 

       initMap(res.results[0].geometry.location); 
      }, 
      error : function(error) 
      { 
       console.log(error); 
      } 
     }); 

     function initMap(oLatlng) 
     { 
      var map = new google.maps.Map(document.getElementById('map'), { 
       center : oLatlng, 
       zoom : 15 
      }); 
     } 
    } 
</script> 
</body> 
</html> 
+0

它工作的感謝!我仍然有與JS類似的問題,真的很感謝 –