这是我正在使用的代码:
奇怪的是,在我点击地理编码按钮后,自动建议似乎只在最后一个输入框上激活。这是我工作的代码:
<html>
<head>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script>
function getLatLng() {
var geocoder = new google.maps.Geocoder();
var address = document.getElementById('address').value;
var input = document.getElementById('searchTextField');
var options = {
types: [],
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var latLng = results[0].geometry.location;
$('#lat').val(results[0].geometry.location.lat());
$('#lng').val(results[0].geometry.location.lng());
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
</head>
<body>
<div>
<input id="address" type="textbox" value="Sydney, NSW">
<input type="button" value="Geocode" onclick="getLatLng()">
<input id="lat" type="textbox" value="lat">
<input id="lng" type="textbox" value="lng">
<input id="searchTextField" type="text" size="50" value="">
</div>
</body>
</html>
什么给了?我以前从未有过这种情况,但这是我尝试编写的第一个脚本。