javascript - Google Maps Javascript API 不适用于搜索框
问题描述
这是我在访问搜索框 javascript API 时遇到的错误。我收到控制台错误 API 错误:ApiNotActivatedMapError。但是当我在控制台中检查它时,我发现 Javascript API 已启用。它适用于法线贴图显示。不知道为什么是错误。任何帮助,将不胜感激。
代码:
<script>
"use strict";
// This example adds a search box to a map, using the Google Place Autocomplete
// feature. People can enter geographical searches. The search box will return a
// pick list containing a mix of places and predicted search terms.
// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
function initAutocomplete() {
const map = new google.maps.Map(document.getElementById("map"), {
center: {
lat: -33.8688,
lng: 151.2195
},
zoom: 13,
mapTypeId: "roadmap"
}); // Create the search box and link it to the UI element.
const input = document.getElementById("pac-input");
const searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input); // Bias the SearchBox results towards current map's viewport.
map.addListener("bounds_changed", () => {
searchBox.setBounds(map.getBounds());
});
let markers = []; // Listen for the event fired when the user selects a prediction and retrieve
// more details for that place.
searchBox.addListener("places_changed", () => {
const places = searchBox.getPlaces();
if (places.length == 0) {
return;
} // Clear out the old markers.
markers.forEach(marker => {
marker.setMap(null);
});
markers = []; // For each place, get the icon, name and location.
const bounds = new google.maps.LatLngBounds();
places.forEach(place => {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
const icon = {
url: place.icon,
size: new google.maps.Size(71, 71),
origin: new google.maps.Point(0, 0),
anchor: new google.maps.Point(17, 34),
scaledSize: new google.maps.Size(25, 25)
}; // Create a marker for each place.
markers.push(
new google.maps.Marker({
map,
icon,
title: place.name,
position: place.geometry.location
})
);
if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}
});
map.fitBounds(bounds);
});
}
</script>
解决方案
推荐阅读
- mongodb - 将现有的 mongo db 属性值添加到更新中的数字许多
- android - React Native - 如何自动从设置返回应用程序?
- polkadot - 通过账户获取验证者的奖励积分
- css - 使用 flexbox 创建页脚
- swift - iOS14,WKWebView 无法打开带有 _blank 目标的链接
- html - react 图标组件不在 li 中居中
- react-native-android - 如何解决运行 react-native run android 的这个错误
- go - 防止对所有不匹配的请求调用 http.handleFunc("/", func(w, r) { }) 处理程序
- c# - .NET Core View 属性名称与模型属性名称不同
- html - Webkitdirectory 仅在顶部文件夹中查找