javascript
// script.js
const API_KEY = "YOUR_API_KEY";
const map = new google.maps.Map(document.getElementById("map-container"), {zoom: 12,center: { lat: -33.8688, lng: 151.2093 },
});const repairShops = [];const searchForm = document.getElementById("search");
searchForm.addEventListener("submit", (event) => {event.preventDefault();const location = document.getElementById("location").value;const request= {query: "汽车修理厂",location: location,radius: 5000,};const service = new google.maps.places.PlacesService(map);service.textSearch(request, (results, status) => {if (status === google.maps.places.PlacesServiceStatus.OK) {clearResults();displayResults(results);displayMarkers(results);} else {alert("抱歉,我们无法找到任何汽车修理店。");}});
});function displayResults(results) {const resultList = document.getElementById("repair-shops");results.forEach((result) => {const item = document.createElement("li");item.innerHTML = result.name;resultList.appendChild(item);});
}function displayMarkers(results) {results.forEach((result) => {const marker = new google.maps.Marker({position: result.geometry.location,map: map,title: result.name,});repairShops.push(marker);});
}function clearResults() {const resultList = document.getElementById("repair-shops");while (resultList.firstChild) {resultList.removeChild(resultList.firstChild);}repairShops.forEach((marker) => {marker.setMap(null);});repairShops.length = 0;
}