如何使用
-
在下方输入框中输入您的地址或邮政编码。
-
点击“搜索”按钮。
-
系统将显示附近汽修厂的信息,包括地址、联系方式和服务。
javascript
// 编写 JavaScript 代码以实现查询功能const locationInput = document.getElementById("location");
const resultList = document.getElementById("汽修厂列表");const searchButton = document.querySelector("button");
searchButton.addEventListener("click", (event) => {event.preventDefault();const location = locationInput.value;// 使用地理编码服务将地址转换为经纬度坐标fetch(`${location}&key=${API_KEY}`).then((response) => response.json()).then((data) => {const latitude = data.results[0].geometry.location.lat;const longitude = data.results[0].geometry.location.lng;// 使用经纬度进行汽修厂搜索fetch(`${latitude},${longitude}&radius=5000&type=car_repair&key=${API_KEY}`).then((response) => response.json()).then((data) => {const results = data.results;// 清空结果列表resultList.innerHTML = "";// 将搜索结果添加到列表中results.forEach((result) => {const name = result.name;const address = result.vicinity;const phone = result.formatted_phone_number;const listItem = document.createElement("li");listItem.innerHTML = `
${name}
${address}
${phone}
`;resultList.appendChild(listItem);});});});
});