附近民宿
<script>
// 创建地图和地理编码器const map = new google.maps.Map(document.getElementById('map'), {center: { lat: 40.7127, lng: -74.0059 },zoom: 12});const geocoder = new google.maps.Geocoder();// 获取用户的当前位置if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {const { latitude, longitude } = position.coords;map.setCenter({ lat: latitude, lng: longitude });}, () => {alert('无法获取您的位置。');});} else {alert('您的浏览器不支持地理定位。');}// 当用户提交表单时,根据输入的位置搜索附近的民宿const form = document.getElementById('form');form.addEventListener('submit', event => {event.preventDefault();const location = document.getElementById('location').value;// 将位置转换为经纬度坐标geocoder.geocode({ address: location }, (results, status) => {if (status === 'OK') {const { lat, lng } = results[0].geometry.location;map.setCenter({ lat, lng });// 根据经纬度坐标搜索附近的民宿const request = {location: { lat, lng },radius: 5000, // 以米为单位的搜索半径type: ['民宿']};constservice= new google.maps.places.PlacesService(map);service.nearbySearch(request, (results, status) => {if (status === 'OK') {display 民宿(results);} else {alert('搜索民宿时出错。');}});} else {alert('无效的位置。');}});});// 在列表中显示搜索到的民宿const display 民宿 = (民宿) => {const 民宿列表 = document.getElementById('民宿列表');民宿列表.innerHTML = '';for (let 民宿 of 民宿) {const 项 = document.createElement('li');const 标题 = document.createElement('h3');标题.textContent = 民宿.name;const 地址 = document.createElement('p');地址.textContent = 民宿.vicinity;const 链接 = document.createElement('a');链接.href = 民宿.url;链接.textContent = '了解更多';项.appendChild(标题);项.appendChild(地址);项.appendChild(链接);民宿列表.appendChild(项);}};
script>
本文由:成都民宿于(2024-11-05)发表了关于
搜一下附近民宿 (搜一下附近民生银行)的文章
。如转载请注明出处:http://www.cdcy-mail.com
如果您对此感兴趣,可以通过以下联系方式与我们联系:
成都区销售
重庆区销售
云贵区销售
华南区销售
华北区销售
华东区销售
华为企业邮箱销售热线:400-0828-083