自动完成 google 个地图
Autocomplete google maps
我正在使用 google 地图自动完成功能。
我想获得 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>
有什么问题?
试试这个方法(没有你的 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);
}
});
};
希望对您有所帮助!
我正在使用 google 地图自动完成功能。 我想获得 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>
有什么问题?
试试这个方法(没有你的 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);
}
});
};
希望对您有所帮助!