我正在使用 Python Flask 和 Google Places API 来自动完成地点。我希望我的网站自动完成该位置,然后将其发送fetch
到 Flask 服务器。现在,我只想将位置记录到控制台。但是,当我使用 Places API ( https://developers.google.com/places/web-service/autocomplete ) 实现 API 时,下拉列表甚至不显示。这是我的代码的相关片段:
<div id="locationField">
<input
id="autocomplete"
placeholder="Enter your address"
onfocus="geolocate()"
type="text"
/>
</div>
<script defer
src="https://maps.googleapis.com/maps/api/js?key=MY_API_KEY&libraries=places&callback=initMap">
let placeSearch;
let autocomplete;
function initAutocomplete() {
// Create the autocomplete object, restricting the search predictions to
// geographical location types.
autocomplete = new google.maps.places.Autocomplete(
document.getElementById("autocomplete"),
{ types: ["geocode"] }
);
// Avoid paying for data that you don't need by restricting the set of
// place fields that are returned to just the address components.
autocomplete.setFields(["address_component"]);
// When the user selects an address from the drop-down, populate the
// address fields in the form.
autocomplete.addListener("place_changed", getAddress);
}
function getAddress() {
console.log(autocomplete.getPlace());
}
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
const geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude,
};
const circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy,
});
autocomplete.setBounds(circle.getBounds());
});
}
}
</script>
任何帮助表示赞赏。先感谢您!
编辑:我检查了开发工具并发现了 2 个 JS 错误:submit:1 Uncaught (in promise) le和Uncaught ReferenceError: geolocate is not defined at HTMLInputElement.onfocus (submit:76)
莫回无
相关分类