你想找附近住的地方?使用我们的地图,轻松找到适合你的住宿场所。
<script> // 初始化地图const map = new google.maps.Map(document.querySelector('.map'), {center: { lat: -34.397, lng: 150.644 },zoom: 12,});// 创建信息窗口const infoWindow = new google.maps.InfoWindow();// 获取用户位置if (navigator.geolocation) {navigator.geolocation.getCurrentPosition(position => {const userLatLng = { lat: position.coords.latitude, lng: position.coords.longitude };map.setCenter(userLatLng);});}// 搜索附近的住宿const request = {location: map.getCenter(),radius: 500,type: 'lodging',};const service = new google.maps.places.PlacesService(map);service.nearbySearch(request, (results, status) => {if (status === google.maps.places.PlacesServiceStatus.OK) {results.forEach(result => {// 创建标记const marker = new google.maps.Marker({position: result.geometry.location,map: map,});// 添加信息窗口事件监听器marker.addListener('click', () => {infoWindow.setContent(result.name);infoWindow.open(map, marker);});// 创建结果列表项const li = document.createElement('li');li.innerHTML = `${result.name}${result.vicinity}
`;// 添加结果列表项到列表中document.querySelector('results').appendChild(li);});}}); script>