javascript
const searchForm = document.getElementById('search');
const searchResults = document.getElementById('results');const repairShops = [{name: 'ABC 汽车维修店',address: '123 Main Street',city: 'Anytown',state: 'CA',zip: '12345',phone: '555-123-4567',website: 'http://www.abc-auto-repair.com'},{name: 'XYZ 汽车维修店',address: '456 Elm Street',city: 'Anytown',state: 'CA',zip: '12346',phone: '555-234-5678',website: 'http://www.xyz-auto-repair.com'},// ...其他修车店
];// 处理表单提交事件
searchForm.addEventListener('submit', (e) => {e.preventDefault();// 获取搜索位置和半径const location = document.getElementById('location').value;const radius = document.getElementById('radius').value;// 根据位置和半径过滤修车店列表const filteredShops = repairShops.filter(shop => {// 检查距离const distance = calculateDistance(location, shop.address);return distance <= radius;});// 清空搜索结果列表searchResults.innerHTML = '';// 将过滤后的修车店列表插入搜索结果列表中filteredShops.forEach(shop => {const li = document.createElement('li');const name = document.createElement('h3');const address = document.createElement('p');const phone = document.createElement('p');const website = document.createElement('a');name.textContent = shop.name;address.textContent = `${shop.address}, ${shop.city}, ${shop.state} ${shop.zip}`;phone.textContent = `电话:${shop.phone}`;website.textContent= `网站:${shop.website}`;website.href = shop.website;li.appendChild(name);li.appendChild(address);li.appendChild(phone);li.appendChild(website);searchResults.appendChild(li);});
});// 计算两个地址之间的距离(使用示例代码)
function calculateDistance(location1, location2) {// 在实际应用中,这里应该使用地图 API 或其他地理位置库来计算实际距离。// 对于本示例,我们将其简化为使用固定距离。return 1; // 公里
}