成都民宿

成都民宿

当前位置: 主页 > 成都民宿

查找附近住宿 (查找附近住宿的地方)

作者:企业邮箱       发布于:2024-11-05        点击量: 797

输入您当前的位置,我们会为您找到附近的住宿。

<script> // 从 HTML 中获取元素const searchForm = document.querySelector(".search-form");const resultsElement = document.querySelector(".results");// 创建一个用于处理位置的函数const getLocation = () => {return new Promise((resolve, reject) => {if (navigator.geolocation) {navigator.geolocation.getCurrentPosition(resolve, reject);} else {reject(new Error("您的浏览器不支持地理位置"));}});};// 创建一个用于查找住宿的函数const findAccommodation = (location) => {// Google Places API 网址,其中包括您的 API 密钥const API_URL = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?key=YOUR_API_KEY&location=" + location + "&radius=5000&type=lodging";return fetch(API_URL).then(response => response.json()).then(data => {return data.results;}).catch(error => {console.error("错误:", error);alert("抱歉,查找住宿时出现问题。请重试。");});};// 创建一个用于显示住宿的函数const displayAccommodation = (results) => {// 清除之前的结果resultsElement.innerHTML = "";// 遍历结果并创建元素results.forEach(result => {const resultElement = document.createElement("li");resultElement.classList.add("result");const resultTitle = document.createElement("h3");resultTitle.classList.add("result-title");resultTitle.innerText = result.name;const resultAddress = document.createElement("p");resultAddress.classList.add("result-address");resultAddress.innerText = result.vicinity;const resultDistance = document.createElement("p");resultDistance.classList.add("result-distance");resultDistance.innerText = `${result.distance} 米`;// 将元素添加到结果列表中resultElement.appendChild(resultTitle);resultElement.appendChild(resultAddress);resultElement.appendChild(resultDistance);resultsElement.appendChild(resultElement);});};// 在提交表单时处理搜索searchForm.addEventListener("submit", (e) => {e.preventDefault();// 获取位置getLocation().then(position => {const location = `${position.coords.latitude},${position.coords.longitude}`;// 查找住宿findAccommodation(location).then(results => {// 显示住宿displayAccommodation(results);});}).catch(error => {alert("获取您的位置时出错。请检查您的浏览器设置并重试。");});});
    本文由:成都民宿于(2024-11-05)发表了关于查找附近住宿 (查找附近住宿的地方)的文章 。如转载请注明出处:http://www.cdcy-mail.com

如果您对此感兴趣,可以通过以下联系方式与我们联系:

点击这里给我发消息成都区销售    点击这里给我发消息重庆区销售    点击这里给我发消息云贵区销售    点击这里给我发消息华南区销售 点击这里给我发消息华北区销售 点击这里给我发消息华东区销售

华为企业邮箱销售热线:400-0828-083

蜀ICP备11006316号-8

Copyright © 2025

版权所有 成都畅邮信息 xml txt html 成都民宿 知识园地 行业新闻 流量卡 成都民宿