谷歌地图自动完成地点未定义
2016-07-22
1675
我正在尝试添加谷歌地图的自动完成功能。 但我收到错误“地点未定义”。
<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>
1个回答
请尝试将:
<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>
如 获取密钥/身份验证 中所述,
All Google Maps JavaScript API applications require authentication.
同样, 版本控制 指出
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 密钥对您不起作用,则此 SO 帖子 - google.maps.places is undefined 可能会为您提供所需的解决方案。
Teyam
2016-07-23