0

我使用谷歌地圖自動填充。 我想獲得LatLng結果。 自動完成功能無效。 代碼是:谷歌地圖自動填寫

<script src="https://maps.googleapis.com/maps/api/js?key=[API]&libraries=places"></script> 
<script> 
var geocoder; 
function initialize() { 
    geocoder = new google.maps.Geocoder(); 
} 
function codeAddress() { 
    var input = document.getElementById("address"); 
    var autocomplete = new google.maps.places.Autocomplete(input); 
    var address = document.getElementById("address").value; 
    geocoder.geocode({ 'address': address }, function(r, s) { 
    alert (r[0].geometry.location); 
    }); 
} 
</script> 
<body onload="initialize()"> 
    <input type="search" id="address"> 
    <input type="button" value="Submit" onclick="codeAddress()"> 

</body> 

問題是什麼?

回答

1

嘗試這樣(沒有你codeAddress()功能):

function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var input = document.getElementById("address"); 
    var autocomplete = new google.maps.places.Autocomplete(input); 
    autocomplete.addListener('place_changed', function() { 
     var place = autocomplete.getPlace(); 
     if (place.geometry) { 
      alert(place.geometry.location); 
     } 
    }); 
}; 

希望這有助於!

+0

你不需要以「var address ...」開頭的行 – stepanian

+0

是的,你是對的@stepanian!我刪除了這一行:'var address = document.getElementById(「address」)。value;' – MKiss

+0

Thanks!它正在工作! – Jack