【发布时间】:2020-12-09 02:42:33
【问题描述】:
我一直在研究这个问题的答案,但最终只找到了创建可搜索地图的方法,即在没有我的 KML 数据的情况下创建一个全新的地图。我有一张带标记的地图,只是想在我拥有的地图上添加一个自动完成搜索栏。
我尝试过的所有操作都破坏了地图(完全删除了地图),并给我留下了一个不起作用的搜索栏。我已将地点添加到我的帐单中,如前所述,我通过外部 KML 文件将带有标记的地图放置在适当的位置。如何在已有的内容中添加搜索功能?
这是我的地图代码:
<!DOCTYPE html>
<html>
<head>
<title>HCV Providers</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<script
src="https://maps.googleapis.com/maps/api/js?key=&callback=initMap&libraries=&v=weekly"
defer
></script>
<style type="text/css">
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
<script>
"use strict";
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 11,
center: {
lat: 30.9843,
lng: 91.9623
}
});
const ctaLayer = new google.maps.KmlLayer({
url: "https://louisianahealthhub.org/HCVProviders.kml",
map: map
});
}
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>
有错误的整页:
<!DOCTYPE html>
<html>
<head>
<title>HCV Providers</title>
<script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap&libraries=&v=weekly" defer></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initAutocomplete&libraries=places&v=weekly" defer></script>
<style type="text/css">
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
<script>
"use strict";
function initMap() {
const map = new google.maps.Map(document.getElementById("map"), {
zoom: 11,
center: {
lat: 30.9843,
lng: 91.9623
}
});
const ctaLayer = new google.maps.KmlLayer({
url: "https://louisianahealthhub.org/HCVProviders.kml",
map: map
});
}
</script>
<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() {}); // 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>
</head>
<body>
<input id="pac-input" class="controls" type="text" placeholder="Search Box" />
<div id="map"></div>
</body>
</html>
【问题讨论】:
-
编辑了您的问题以添加代码的“完整/损坏”版本(添加了 Google 的测试密钥,以便地图可以正常工作)。
标签: javascript html google-maps search kml