2016-07-06 76 views
0

我已經在Google API上創建了一個項目控制檯,並獲得了一個瀏覽器鍵以顯示帶有Google地圖自動填充的文本框。雖然我在瀏覽器控制檯中沒有收到任何錯誤,但自動完成功能不起作用。這裏是我的html谷歌地圖自動填充字段不會列出地址

<html> 
<head> 
    <meta charset = "utf-8"/> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script> 
</head> 

<body onload = "initialize();"> 
<form method = "post"> 
    <input type = "text" id = "autocomplete" placeholder = "Η Διευθυνσή σας"/> 
</form> 
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBmm_XsU6QttZNhw1RfxRHTpavpbN33jX0&libraries=places,geometry" async defer></script> 
<script type = "text/javascript"> 
     var autocomplete; 
     function initialize() { 
      autocomplete = new google.maps.places.Autocomplete(
       /** @type {HTMLInputElement} */(document.getElementById('autocomplete')), 
       { types: ['geocode'] }); 

     } 
    </script> 
</body> 

,這裏是我的測試頁面:http://www.lefko-cafe.gr/content/addrress_finder.php

奇怪的是,正是相同的HTML是另一個域中工作(除鍵):http://www.laoskaikalamaki.gr/content/addrress_finder.php

回答

1

你的例子都不適合我。 onload事件和腳本加載完成是兩個不同的異步事件,它們可能在不同的服務器/瀏覽器位置以不同的順序觸發。我收到一個javascript錯誤Uncaught TypeError: Cannot read property 'innerHTML' of null 你有async defer在你的API中,但沒有&callback=initialize參數。

代碼片段:

<body> <!-- remove onload = "initialize();" --> 
<form method = "post"> 
    <input type = "text" id = "autocomplete" placeholder = "Η Διευθυνσή σας"/> 
</form> 
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBmm_XsU6QttZNhw1RfxRHTpavpbN33jX0&libraries=places,geometry&callback=initialize" async defer></script> 

<form method="post"> 
 
    <input type="text" id="autocomplete" placeholder="Η Διευθυνσή σας" /> 
 
</form> 
 
<script src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry&callback=initialize" async defer></script> 
 
<script type="text/javascript"> 
 
    var autocomplete; 
 

 
    function initialize() { 
 
    autocomplete = new google.maps.places.Autocomplete(
 
     /** @type {HTMLInputElement} */ 
 
     (document.getElementById('autocomplete')), { 
 
     types: ['geocode'] 
 
     }); 
 
    } 
 
</script>

+0

感謝您的答覆,但它沒有工作,使下面的API。 ... – meidanisalekos

+0

是否會爲您的代碼片段工作? – geocodezip

+0

是的代碼段工作 – meidanisalekos

3

所改動Maps API Standard Plan如果你想使用Places Service需要啓用Google Places API Web Servicein the developer console

的如果您想要我們Directions您需要啓用Google Maps Directions API,如果您想使用Geocoding您需要啓用Google Maps Geocoding API

最近這個變了,所以沒必要抱歉。

0

如果您的域名在6月22,2016之後,則需要api-key。你需要的,如果你想使用谷歌自動完成只用JavaScript

  • 谷歌地圖的JavaScript API
  • 谷歌Places API的Web服務