【发布时间】:2016-02-20 15:17:13
【问题描述】:
正如标题所说,我正在尝试通过使用谷歌 API 自动完成来使用位置建议,而不创建谷歌地图。我在多个站点上找到了一些代码并尝试将它们放在一起,填补了缺失的部分,但到目前为止它还没有工作。我想知道是否有人知道 google API 的工作原理并可以帮助我,谢谢!
HTML:
<head>
....
<script src="file.js"></script>
....
</head>
<body>
....
<input type="text" class="form-control" id="university" onfocus="geolocate()" placeholder="Name of University" required>
....
<script src="https://maps.googleapis.com/maps/api/js?key=API_KEY&signed_in=true&libraries=places&callback=initialize" async defer></script>
</body>
JS:
/*global google */
var autocomplete;
function initialize() {
"use strict";
autocomplete = new google.maps.places.Autocomplete(document.getElementById("university"), {types: ["geocode"]});
}
function geolocate() {
"use strict";
var geolocation, circle;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
【问题讨论】:
-
你有 API 密钥吗?
-
是的。我只是不想在这里包含它。
标签: javascript api google-maps autocomplete