gpt4 book ai didi

javascript - 谷歌地图自动完成地点未定义

转载 作者:行者123 更新时间:2023-11-27 22:44:15 25 4
gpt4 key购买 nike

我正在尝试添加谷歌地图的自动完成功能。但我收到错误“未定义的地方”。

    <html>
<body>

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
<script type="text/javascript">
google.maps.event.addDomListener(window, 'load', function () {
var places = new google.maps.places.Autocomplete(document.getElementById('txtPlaces'));
google.maps.event.addListener(places, 'place_changed', function () {
var place = places.getPlace();
var address = place.formatted_address;
var latitude = place.geometry.location.lat();
var longitude = place.geometry.location.lng();
var mesg = "Address: " + address;
mesg += "\nLatitude: " + latitude;
mesg += "\nLongitude: " + longitude;
alert(mesg);
});
});
</script>
<span>Location:</span>
<input type="text" id="txtPlaces" style="width: 250px" placeholder="Enter a location" />
</body>
</html>

最佳答案

请尝试替换:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>

这样:

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&key=YOUR_API_KEY&sensor=false&libraries=places"></script>

Get a Key/Authentication 中所述,

All Google Maps JavaScript API applications require authentication.

同样,Versioning指出

You can indicate which version of the API to load in your application by specifying the v parameter of the Google Maps JavaScript API bootstrap request.

如果添加 API key 对您不起作用,请阅读此帖子 - google.maps.places is undefined可能会为您提供所需的解决方案。

关于javascript - 谷歌地图自动完成地点未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38523624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com