script.js
javascript
const searchInput = document.getElementById('search-input');
const searchButton = document.getElementById('search-button');
const results = document.getElementById('results');// 获取用户位置
function getLocation() {return new Promise((resolve, reject) => {navigator.geolocation.getCurrentPosition(resolve, reject);});
}// 根据用户位置查找最近的修车厂
async function findRepairShops(location) {const lat = location.coords.latitude;const lng = location.coords.longitude;const response = await fetch(`${lat},${lng}&radius=5000&type=car_repair&key=YOUR_API_KEY`);const data = await response.json();return data.results;
}// 创建修车厂列表
function createList(repairShops) {const ul = document.createElement('ul');repairShops.forEach(shop => {const li = document.createElement('li');const a = document.createElement('a');a.href = `tel:${shop.formatted_phone_number}`;a.innerHTML = `${shop.name} - ${shop.formatted_address}`;li.appendChild(a);ul.appendChild(li);});return ul;
}// 搜索按钮事件处理程序
searchButton.addEventListener('click', async () => {const location = await getLocation();const repairShops = await findRepairShops(location);const list = createList(repairShops);results.appendChild(list);
});