附近24小时药店 (附近24小时营业药店)

杭州娱乐 07-12 阅读:18 评论:3
附近24小时药店 (附近24小时营业药店)
javascript // JavaScript 代码const searchInput = document.querySelector('input[placeholder="查找附近的药店"]'); const resultsList = document.querySelector('.results-list');// 监听搜索输入框的输入事件,并获取用户的搜索项 searchInput.addEventListener('input', (event) => {const searchTerm = event.target.value;// 使用 Geolocation API 获取用户的当前位置navigator.geolocation.getCurrentPosition((position) => {const latitude = position.coords.latitude;const longitude = position.coords.longitude;// 使用 Google Places API 查找附近的药店const request = {query: '24 小时药店',location: `${latitude},${longitude}`,radius: '500' // 搜索半径为 500 米};const service = new google.maps.places.PlacesService(resultsList);service.textSearch(request, (results, status) => {if (status === google.maps.places.PlacesServiceStatus.OK) {// 清空结果列表resultsList.innerHTML = '';// 遍历搜索结果并将其添加到列表中results.forEach((result) => {const li = document.createElement('li');const name = document.createElement('h3');const address = document.createElement('p');const phone = document.createElement('p');name.textContent = result.name;address.textContent = result.formatted_address;phone.textContent = result.formatted_phone_number;li.appendChild(name);li.appendChild(address);li.appendChild(phone);resultsList.appendChild(li);});}});}); });
版权声明

本文仅代表作者观点,不代表杭州桑拿立场。
本文系作者授权发表,未经许可,不得转载。