用騰訊地圖寫一個逆地址解析(很詳細)

首先說明以下代碼適合有前端基礎知識的同學。

以下是css和html部分

<!DOCTYPE html>

<html lang="zh-CN">

? ? <!-- lang是用來申明語言類型,這里申明為中文(zh)中國大陸(CN)

? ? ?補充中文繁體為zh-TW -->

<head>

? ? <meta charset="UTF-8">

? ? <meta name="viewport" content="width=device-width, initial-scale=1.0">

? ? <title>騰訊地圖逆地址解析</title>

? ? <script src="https://map.qq.com/api/gljs?v=1.exp&key=你的key&libraries=service"></script>

? ? <!--接口地址的引入,加 &libraries=service,為了避免location格式錯誤 ?-->

? ? <style>

? ? ? ? :root {

? ? ? ? ? ? /* 在根元素的偽類中自定義了一些顏色,方便后續引用 */

? ? ? ? ? ? /* 放在:root中,是因為它是全局屬性 */

? ? ? ? ? ? --primary: #1a2980;

? ? ? ? ? ? --secondary: #26d0ce;

? ? ? ? ? ? --light: #f8f9ff;

? ? ? ? ? ? --dark: #0f1a5c;

? ? ? ? ? ? --success: #2ecc71;

? ? ? ? ? ? --danger: #e74c3c;

? ? ? ? ? ? --warning: #f39c12;

? ? ? ? }

? ? ? ?

? ? ? ? * {

? ? ? ? ? ? box-sizing: border-box;

? ? ? ? ? ? /* 防止盒子被撐大 */

? ? ? ? ? ? margin: 0;

? ? ? ? ? ? padding: 0;

? ? ? ? ? ? /* 清除默認格式 */

? ? ? ? ? ? font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;

? ? ? ? }

? ? ? ?

? ? ? ? body {

? ? ? ? ? ? background: linear-gradient(135deg, var(--primary), var(--secondary));

? ? ? ? ? ? /* 漸變背景 */

? ? ? ? ? ? color: #333;

? ? ? ? ? ? min-height: 100vh;

? ? ? ? ? ? padding: 20px;

? ? ? ? ? ? display: flex;

? ? ? ? ? ? justify-content: center;

? ? ? ? ? ? align-items: center;

? ? ? ? }

? ? ? ?

? ? ? ? .container {

? ? ? ? ? ? width: 100%;

? ? ? ? ? ? max-width: 1000px;

? ? ? ? ? ? background: rgba(255, 255, 255, 0.95);

? ? ? ? ? ? border-radius: 16px;

? ? ? ? ? ? box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);

? ? ? ? ? ? overflow: hidden;

? ? ? ? ? ? display: flex;

? ? ? ? ? ? flex-direction: column;

? ? ? ? }

? ? ? ?

? ? ? ? header {

? ? ? ? ? ? background: var(--primary);

? ? ? ? ? ? /* 自定義引用變量 */

? ? ? ? ? ? background: linear-gradient(90deg, var(--primary), var(--secondary));

? ? ? ? ? ? color: white;

? ? ? ? ? ? padding: 25px;

? ? ? ? ? ? text-align: center;

? ? ? ? ? ? position: relative;

? ? ? ? ? ? overflow: hidden;

? ? ? ? }

? ? ? ?

? ? ? ? header::before {

? ? ? ? ? ? content: "";

? ? ? ? ? ? position: absolute;

? ? ? ? ? ? top: -50px;

? ? ? ? ? ? left: -50px;

? ? ? ? ? ? width: 150px;

? ? ? ? ? ? height: 150px;

? ? ? ? ? ? border-radius: 50%;

? ? ? ? ? ? background: rgba(255, 255, 255, 0.1);

? ? ? ? }

? ? ? ?

? ? ? ? header::after {

? ? ? ? ? ? content: "";

? ? ? ? ? ? position: absolute;

? ? ? ? ? ? bottom: -30px;

? ? ? ? ? ? right: -30px;

? ? ? ? ? ? width: 100px;

? ? ? ? ? ? height: 100px;

? ? ? ? ? ? border-radius: 50%;

? ? ? ? ? ? background: rgba(255, 255, 255, 0.1);

? ? ? ? }

? ? ? ?

? ? ? ? h1 {

? ? ? ? ? ? font-size: 32px;

? ? ? ? ? ? margin-bottom: 10px;

? ? ? ? ? ? position: relative;

? ? ? ? ? ? z-index: 2;

? ? ? ? }

? ? ? ?

? ? ? ? .subtitle {

? ? ? ? ? ? font-size: 17px;

? ? ? ? ? ? opacity: 0.9;

? ? ? ? ? ? position: relative;

? ? ? ? ? ? z-index: 2;

? ? ? ? }

? ? ? ?

? ? ? ? .content {

? ? ? ? ? ? display: flex;

? ? ? ? ? ? flex-wrap: wrap;

? ? ? ? ? ? /* flex-wrap: nowrap | wrap | wrap-reverse;

? ? ? ? ? ? 當一行排不下時,允許換行 */

? ? ? ? ? ? padding: 20px;

? ? ? ? }

? ? ? ?

? ? ? ? .input-section {

? ? ? ? ? ? flex: 1;

? ? ? ? ? ? min-width: 300px;

? ? ? ? ? ? padding: 20px;

? ? ? ? }

? ? ? ?

? ? ? ? .map-section {

? ? ? ? ? ? flex: 1;

? ? ? ? ? ? min-width: 300px;

? ? ? ? ? ? height: 400px;

? ? ? ? ? ? border-radius: 10px;

? ? ? ? ? ? overflow: hidden;

? ? ? ? ? ? background: #f0f0f0;

? ? ? ? ? ? position: relative;

? ? ? ? ? ? box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);

? ? ? ? }

? ? ? ?

? ? ? ? #map {

? ? ? ? ? ? width: 100%;

? ? ? ? ? ? height: 100%;

? ? ? ? }

? ? ? ?

? ? ? ? .input-group {

? ? ? ? ? ? margin-bottom: 25px;

? ? ? ? }

? ? ? ?

? ? ? ? label {

? ? ? ? ? ? display: block;

? ? ? ? ? ? margin-bottom: 8px;

? ? ? ? ? ? font-weight: 600;

? ? ? ? ? ? color: var(--primary);

? ? ? ? ? ? font-size: 16px;

? ? ? ? }

? ? ? ?

? ? ? ? input {

? ? ? ? ? ? width: 100%;

? ? ? ? ? ? padding: 14px 18px;

? ? ? ? ? ? border: 2px solid #dde4ff;

? ? ? ? ? ? border-radius: 10px;

? ? ? ? ? ? font-size: 17px;

? ? ? ? ? ? transition: all 0.3s;

? ? ? ? ? ? background: #f8f9ff;

? ? ? ? }

? ? ? ?

? ? ? ? input:focus {

? ? ? ? ? ? border-color: var(--primary);

? ? ? ? ? ? outline: none;

? ? ? ? ? ? box-shadow: 0 0 0 3px rgba(26, 41, 128, 0.2);

? ? ? ? }

? ? ? ?

? ? ? ? .button-group {

? ? ? ? ? ? display: flex;

? ? ? ? ? ? gap: 15px;

? ? ? ? ? ? margin-top: 30px;

? ? ? ? }

? ? ? ?

? ? ? ? button {

? ? ? ? ? ? flex: 1;

? ? ? ? ? ? padding: 15px;

? ? ? ? ? ? border: none;

? ? ? ? ? ? border-radius: 10px;

? ? ? ? ? ? font-size: 17px;

? ? ? ? ? ? font-weight: 600;

? ? ? ? ? ? cursor: pointer;

? ? ? ? ? ? transition: all 0.3s;

? ? ? ? ? ? display: flex;

? ? ? ? ? ? align-items: center;

? ? ? ? ? ? justify-content: center;

? ? ? ? ? ? gap: 8px;

? ? ? ? }

? ? ? ?

? ? ? ? button i {

? ? ? ? ? ? font-size: 20px;

? ? ? ? }

? ? ? ?

? ? ? ? #parseBtn {

? ? ? ? ? ? background: var(--primary);

? ? ? ? ? ? color: white;

? ? ? ? }

? ? ? ?

? ? ? ? #getLocationBtn {

? ? ? ? ? ? background: var(--secondary);

? ? ? ? ? ? color: white;

? ? ? ? }

? ? ? ?

? ? ? ? button:hover {

? ? ? ? ? ? transform: translateY(-3px);

? ? ? ? ? ? box-shadow: 0 6px 15px rgba(0, 0, 0, 0.15);

? ? ? ? }

? ? ? ?

? ? ? ? #parseBtn:hover {

? ? ? ? ? ? background: var(--dark);

? ? ? ? }

? ? ? ?

? ? ? ? #getLocationBtn:hover {

? ? ? ? ? ? background: #1db9b7;

? ? ? ? }

? ? ? ?

? ? ? ? .result-section {

? ? ? ? ? ? padding: 25px;

? ? ? ? ? ? background: var(--light);

? ? ? ? ? ? border-radius: 12px;

? ? ? ? ? ? margin-top: 25px;

? ? ? ? ? ? border: 1px solid #e0e7ff;

? ? ? ? ? ? box-shadow: 0 4px 10px rgba(0, 0, 0, 0.05);

? ? ? ? }

? ? ? ?

? ? ? ? .result-title {

? ? ? ? ? ? font-size: 22px;

? ? ? ? ? ? color: var(--primary);

? ? ? ? ? ? margin-bottom: 20px;

? ? ? ? ? ? padding-bottom: 12px;

? ? ? ? ? ? border-bottom: 3px solid var(--primary);

? ? ? ? ? ? display: flex;

? ? ? ? ? ? align-items: center;

? ? ? ? ? ? gap: 10px;

? ? ? ? }

? ? ? ?

? ? ? ? .result-grid {

? ? ? ? ? ? display: grid;

? ? ? ? ? ? grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));

? ? ? ? ? ? gap: 20px;

? ? ? ? }

? ? ? ?

? ? ? ? .result-item {

? ? ? ? ? ? background: white;

? ? ? ? ? ? padding: 20px;

? ? ? ? ? ? border-radius: 10px;

? ? ? ? ? ? box-shadow: 0 3px 8px rgba(0, 0, 0, 0.06);

? ? ? ? ? ? border-left: 4px solid var(--primary);

? ? ? ? ? ? transition: transform 0.3s;

? ? ? ? }

? ? ? ?

? ? ? ? .result-item:hover {

? ? ? ? ? ? transform: translateY(-5px);

? ? ? ? }

? ? ? ?

? ? ? ? .result-label {

? ? ? ? ? ? font-size: 15px;

? ? ? ? ? ? color: #666;

? ? ? ? ? ? margin-bottom: 8px;

? ? ? ? ? ? display: flex;

? ? ? ? ? ? align-items: center;

? ? ? ? ? ? gap: 6px;

? ? ? ? }

? ? ? ?

? ? ? ? .result-value {

? ? ? ? ? ? font-size: 19px;

? ? ? ? ? ? font-weight: 700;

? ? ? ? ? ? color: var(--primary);

? ? ? ? ? ? word-break: break-all;

? ? ? ? ? ? /* 用于控制文本換行規則的屬性,可將單詞拆開,避免溢出 */

? ? ? ? }

? ? ? ?

? ? ? ? .address-value {

? ? ? ? ? ? font-size: 22px;

? ? ? ? ? ? color: var(--danger);

? ? ? ? ? ? font-weight: 800;

? ? ? ? }

? ? ? ?

? ? ? ? .loading {

? ? ? ? ? ? text-align: center;

? ? ? ? ? ? padding: 25px;

? ? ? ? ? ? display: none;

? ? ? ? }

? ? ? ?

? ? ? ? .spinner {

? ? ? ? ? ? border: 5px solid rgba(0, 0, 0, 0.1);

? ? ? ? ? ? border-top: 5px solid var(--primary);

? ? ? ? ? ? border-radius: 50%;

? ? ? ? ? ? width: 50px;

? ? ? ? ? ? height: 50px;

? ? ? ? ? ? animation: spin 1s linear infinite;

? ? ? ? ? ? /* animation和@keyframes連用 */

? ? ? ? ? ? margin: 0 auto 20px;

? ? ? ? }

? ? ? ?

? ? ? ? @keyframes spin {

? ? ? ? ? ? /* spin是動畫名稱,用來實現自轉 */

? ? ? ? ? ? 0% { transform: rotate(0deg); }

? ? ? ? ? ? 100% { transform: rotate(360deg); }

? ? ? ? }

? ? ? ?

? ? ? ? .error {

? ? ? ? ? ? color: var(--danger);

? ? ? ? ? ? background: rgba(231, 76, 60, 0.1);

? ? ? ? ? ? padding: 18px;

? ? ? ? ? ? border-radius: 10px;

? ? ? ? ? ? margin-top: 20px;

? ? ? ? ? ? display: none;

? ? ? ? ? ? border-left: 4px solid var(--danger);

? ? ? ? ? ? font-weight: 500;

? ? ? ? }

? ? ? ?

? ? ? ? footer {

? ? ? ? ? ? text-align: center;

? ? ? ? ? ? padding: 20px;

? ? ? ? ? ? color: #666;

? ? ? ? ? ? font-size: 15px;

? ? ? ? ? ? border-top: 1px solid #eee;

? ? ? ? }

? ? ? ? ? ? ? ?

? ? ? ? @media (max-width: 768px) {

? ? ? ? ? ? .content {

? ? ? ? ? ? ? ? flex-direction: column;

? ? ? ? ? ? }

? ? ? ? ? ?

? ? ? ? ? ? .input-section, .map-section {

? ? ? ? ? ? ? ? width: 100%;

? ? ? ? ? ? }

? ? ? ? ? ?

? ? ? ? ? ? h1 {

? ? ? ? ? ? ? ? font-size: 28px;

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? /* 響應式設計 */

? ? </style>

? ? <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">

? ? <!-- Font Awesome 圖標庫 的 CSS 文件地址 -->

? ? ?<!-- 里面提供了大量可自定義圖標 -->

</head>

<body>

? ? <div class="container">

? ? ? ? <header>

? ? ? ? ? ? <h1><i class="fas fa-map-marked-alt"></i> 騰訊地圖逆地址解析</h1>

? ? ? ? ? ? <!-- 這里的i標簽用來包裹字體圖標(如 Font Awesome),成為圖標占位符 -->

? ? ? ? ? ? ?<!-- 一般斜體不用i,用em或者font-style: italic; -->

? ? ? ? ? ? ? <!-- 如果用i除了上面一種情況外,還有就是i標簽包裹的內容語氣或者性質與旁文不同 -->

? ? ? ? ? ? <div class="subtitle">GPS經緯度轉換為詳細地址信息</div>

? ? ? ? </header>

? ? ? ?

? ? ? ? <div class="content">

? ? ? ? ? ? <div class="input-section">

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? <div class="input-group">

? ? ? ? ? ? ? ? ? ? <label for="latitude"><i class="fas fa-latitude"></i> 緯度</label>

? ? ? ? ? ? ? ? ? ? <input type="text" id="latitude" placeholder="例如:39.908823">

? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? <div class="input-group">

? ? ? ? ? ? ? ? ? ? <label for="longitude"><i class="fas fa-longitude"></i> 經度</label>

? ? ? ? ? ? ? ? ? ? <input type="text" id="longitude" placeholder="例如:116.397470">

? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? <div class="button-group">

? ? ? ? ? ? ? ? ? ? <button id="parseBtn"><i class="fas fa-search-location"></i> 解析地址</button>

? ? ? ? ? ? ? ? ? ? <button id="getLocationBtn"><i class="fas fa-location-arrow"></i> 獲取當前位置</button>

? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? <div class="loading" id="loading">

? ? ? ? ? ? ? ? ? ? <div class="spinner"></div>

? ? ? ? ? ? ? ? ? ? <p>正在解析地址信息,請稍候...</p>

? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? <div class="error" id="error">

? ? ? ? ? ? ? ? ? ? <i class="fas fa-exclamation-circle"></i>

? ? ? ? ? ? ? ? ? ? <span id="errorMsg"></span>

? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? <div class="result-section" id="resultSection" style="display: none;">

? ? ? ? ? ? ? ? ? ? <div class="result-title">

? ? ? ? ? ? ? ? ? ? ? ? <i class="fas fa-map-marker-alt"></i>

? ? ? ? ? ? ? ? ? ? ? ? <span>解析結果</span>

? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? ? ? <div class="result-grid">

? ? ? ? ? ? ? ? ? ? ? ? <div class="result-item">

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="result-label"><i class="fas fa-road"></i> 完整地址</div>

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="result-value address-value" id="fullAddress">-</div>

? ? ? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? ? ? ? ? <div class="result-item">

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="result-label"><i class="fas fa-flag"></i> 國家</div>

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="result-value" id="nation">-</div>

? ? ? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? ? ? ? ? <div class="result-item">

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="result-label"><i class="fas fa-map"></i> 省份</div>

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="result-value" id="province">-</div>

? ? ? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? ? ? ? ? <div class="result-item">

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="result-label"><i class="fas fa-city"></i> 城市</div>

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="result-value" id="city">-</div>

? ? ? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? ? ? ? ? <div class="result-item">

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="result-label"><i class="fas fa-building"></i> 區縣</div>

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="result-value" id="district">-</div>

? ? ? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? ? ? ? ? <div class="result-item">

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="result-label"><i class="fas fa-street-view"></i> 街道</div>

? ? ? ? ? ? ? ? ? ? ? ? ? ? <div class="result-value" id="street">-</div>

? ? ? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? ? ? </div>

? ? ? ? ? ? </div>

? ? ? ? ? ?

? ? ? ? ? ? <div class="map-section">

? ? ? ? ? ? ? ? <div id="map"></div>

? ? ? ? ? ? </div>

? ? ? ? </div>

? ? ? ?

? ? ? ? <footer>

? ? ? ? ? ? <div>本服務使用騰訊地圖API | 逆地址解析功能</div>

? ? ? ? ? ? <div style="margin-top: 8px; font-size: 13px; color: #888;">

? ? ? ? ? ? ? ? <i class="fas fa-info-circle"></i> 請確保在HTTP/HTTPS協議下使用

? ? ? ? ? ? </div>

? ? ? ? </footer>

? ? </div>

以下是JavaScript部分

? ? <script>

? ? ? ?

? ? ? ?

? ? ? ? // 初始化地圖

? ? ? ? let map;

? ? ? ? let marker;

? ? ? ? let geocoder;

? ? ? ?

? ? ? ? // 頁面加載完成后初始化地圖

? ? ? ? window.onload = function() {

? ? ? ? ? ? // 檢查是否在HTTP/HTTPS協議下運行

? ? ? ? ? ? if (window.location.protocol === 'file:') {

? ? ? ? ? ? ? ? showError('騰訊地圖API不支持在本地文件協議(file://)下使用,請在HTTP/HTTPS協議下運行此頁面');

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ?

? ? ? ? ? ? // 初始化地圖

? ? ? ? ? ? initMap();

? ? ? ? };

? ? ? ?

? ? ? ? function initMap() {

? ? ? ? ? ? try {

? ? ? ? ? ? ? ? // try捕獲錯誤,無錯則跳過catch

? ? ? ? ? ? ? ? // 默認坐標 - 北京

? ? ? ? ? ? ? ? const center = new TMap.LatLng(39.908823, 116.397470);

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? // 初始化地圖

? ? ? ? ? ? ? ? map = new TMap.Map('map', {

? ? ? ? ? ? ? ? ? ? ?// 騰訊地圖創建地圖實例

? ? ? ? ? ? ? ? ? ? // 格式為map = new TMap.Map(container, options);

? ? ? ? ? ? ? ? ? ? // option除了以下還有rotation,pitch

? ? ? ? ? ? ? ? ? ? center: center,

? ? ? ? ? ? ? ? ? ? zoom: 15,

? ? ? ? ? ? ? ? ? ? // zoom:(3-20)表示地圖的縮放級別,數值越小地圖顯示范圍越大

? ? ? ? ? ? ? ? ? ? viewMode: '2D'

? ? ? ? ? ? ? ? });

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? // 添加標記

? ? ? ? ? ? ? ? marker = new TMap.MultiMarker({

? ? ? ? ? ? ? ? ? ? // 創建多個標記點實例

? ? ? ? ? ? ? ? ? ? map: map,

? ? ? ? ? ? ? ? ? ? // 創建的標記綁在map實例上

? ? ? ? ? ? ? ? ? ? styles: {

? ? ? ? ? ? ? ? ? ? ? ? "marker": new TMap.MarkerStyle({

? ? ? ? ? ? ? ? ? ? ? ? ? ? width: 25,

? ? ? ? ? ? ? ? ? ? ? ? ? ? height: 35,

? ? ? ? ? ? ? ? ? ? ? ? ? ? anchor: {x: 12.5, y: 35},

? ? ? ? ? ? ? ? ? ? ? ? ? ? // 定位點,用于指定標記圖片的哪個點與實際坐標(經緯度)對齊

? ? ? ? ? ? ? ? ? ? ? ? ? ? src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/marker.png'

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ? })

? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? geometries: [{

? ? ? ? ? ? ? ? ? ? ? ? id: 'location',

? ? ? ? ? ? ? ? ? ? ? ? styleId: 'marker',

? ? ? ? ? ? ? ? ? ? ? ? position: center

? ? ? ? ? ? ? ? ? ? }]

? ? ? ? ? ? ? ? });

? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? // 初始化地理編碼服務TMap.service.Geocoder()

? ? ? ? ? ? ? ? // 它是騰訊地圖 API 提供的一個服務類,封裝了地理編碼相關的功能

? ? ? ? ? ? ? ? geocoder = new TMap.service.Geocoder();

? ? ? ? ? ? } catch (e) {

? ? ? ? ? ? ? ? // catch處理錯誤

? ? ? ? ? ? ? ? showError(`地圖初始化失敗: ${e.message}`);

? ? ? ? ? ? }

? ? ? ? }

? ? ? ?

? ? ? ? // 獲取DOM元素

? ? ? ? const latitudeInput = document.getElementById('latitude');

? ? ? ? const longitudeInput = document.getElementById('longitude');

? ? ? ? const parseBtn = document.getElementById('parseBtn');

? ? ? ? // 解析地址btn

? ? ? ? const getLocationBtn = document.getElementById('getLocationBtn');

? ? ? ? // 獲取當前地址的btn

? ? ? ? const resultSection = document.getElementById('resultSection');

? ? ? ? // 解析結果

? ? ? ? const loading = document.getElementById('loading');

? ? ? ? const errorElement = document.getElementById('error');

? ? ? ? const errorMsg = document.getElementById('errorMsg');

? ? ? ?

? ? ? ? // 更新結果展示

? ? ? ? function updateResults(data) {

? ? ? ? ? ? document.getElementById('fullAddress').textContent = data.address || '未知';

? ? ? ? ? ? document.getElementById('nation').textContent = data.address_component.nation || '未知';

? ? ? ? ? ? document.getElementById('province').textContent = data.address_component.province || '未知';

? ? ? ? ? ? document.getElementById('city').textContent = data.address_component.city || '未知';

? ? ? ? ? ? document.getElementById('district').textContent = data.address_component.district || '未知';

? ? ? ? ? ? document.getElementById('street').textContent = data.address_component.street || '未知';

? ? ? ? ? ?

? ? ? ? ? ? resultSection.style.display = 'block';

? ? ? ? }

? ? ? ?

? ? ? ? // 顯示錯誤信息

? ? ? ? function showError(message) {

? ? ? ? ? ? errorMsg.textContent = message;

? ? ? ? ? ? errorElement.style.display = 'flex';

? ? ? ? ? ? setTimeout(() => {

? ? ? ? ? ? ? ? errorElement.style.display = 'none';

? ? ? ? ? ? }, 8000);

? ? ? ? }

? ? ? ?

? ? ? ? // 逆地址解析函數

? ? ? ? function reverseGeocode(lat, lng) {

? ? ? ? ? ? // 顯示加載狀態

? ? ? ? ? ? loading.style.display = 'block';

? ? ? ? ? ? resultSection.style.display = 'none';

? ? ? ? ? ? errorElement.style.display = 'none';

? ? ? ? ? ?

? ? ? ? ? ? // 確保地理編碼服務已初始化

? ? ? ? ? ? if (!geocoder) {

? ? ? ? ? ? ? ? try {

? ? ? ? ? ? ? ? ? ? geocoder = new TMap.service.Geocoder();

? ? ? ? ? ? ? ? } catch (e) {

? ? ? ? ? ? ? ? ? ? loading.style.display = 'none';

? ? ? ? ? ? ? ? ? ? showError(`地理編碼服務初始化失敗: ${e.message}`);

? ? ? ? ? ? ? ? ? ? return;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ?

? ? ? ? ? ? // 調用逆地址解析

? ? ? ? ? ? geocoder.getAddress({ location: new TMap.LatLng( lat, lng ) })

? ? ? ? ? ? // 接收經緯度地址

? ? ? ? ? ? // getAddress() 是一個異步方法,返回一個 Promise 對象,then() 用于處理請求成功的情況

? ? ? ? ? ? ? ? .then((result) => {

? ? ? ? ? ? ? ? ? ? // 隱藏加載狀態

? ? ? ? ? ? ? ? ? ? loading.style.display = 'none';

? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? if (result.status === 0) {

? ? ? ? ? ? ? ? ? ? ? ? // status === 0表示解析成功,是騰訊地圖api的規定

? ? ? ? ? ? ? ? ? ? ? ? // 更新結果展示

? ? ? ? ? ? ? ? ? ? ? ? updateResults(result.result);

? ? ? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? ? ? // 更新地圖位置

? ? ? ? ? ? ? ? ? ? ? ? const newPosition = new TMap.LatLng(lat, lng);

? ? ? ? ? ? ? ? ? ? ? ? map.setCenter(newPosition);

? ? ? ? ? ? ? ? ? ? ? ? marker.setGeometries([{

? ? ? ? ? ? ? ? ? ? ? ? ? ? // 更新標記點

? ? ? ? ? ? ? ? ? ? ? ? ? ? id: 'location',

? ? ? ? ? ? ? ? ? ? ? ? ? ? styleId: 'marker',

? ? ? ? ? ? ? ? ? ? ? ? ? ? position: newPosition

? ? ? ? ? ? ? ? ? ? ? ? }]);

? ? ? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? ? ? showError(`解析失敗: ${result.message}`);

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? })

? ? ? ? ? ? ? ? .catch((error) => {

? ? ? ? ? ? ? ? ? ? loading.style.display = 'none';

? ? ? ? ? ? ? ? ? ? showError(`請求失敗: ${error.message}`);

? ? ? ? ? ? ? ? });

? ? ? ? }

? ? ? ?

? ? ? ? // 解析按鈕點擊事件

? ? ? ? parseBtn.addEventListener('click', () => {

? ? ? ? ? ? const lat = parseFloat(latitudeInput.value.trim());

? ? ? ? ? ? const lng = parseFloat(longitudeInput.value.trim());

? ? ? ? ? ?

? ? ? ? ? ? if (isNaN(lat) || isNaN(lng)) {

? ? ? ? ? ? ? ? showError('請輸入有效的經緯度數值');

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ?

? ? ? ? ? ? if (lat < -90 || lat > 90) {

? ? ? ? ? ? ? ? showError('緯度范圍應在-90到90之間');

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ?

? ? ? ? ? ? if (lng < -180 || lng > 180) {

? ? ? ? ? ? ? ? showError('經度范圍應在-180到180之間');

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ?

? ? ? ? ? ? reverseGeocode(lat, lng);

? ? ? ? });

? ? ? ?

? ? ? ? // 獲取當前位置按鈕點擊事件

? ? ? ? getLocationBtn.addEventListener('click', () => {

? ? ? ? ? ? if (!navigator.geolocation) {

? ? ? ? ? ? ? ? showError('您的瀏覽器不支持地理位置服務');

? ? ? ? ? ? ? ? return;

? ? ? ? ? ? }

? ? ? ? ? ?

? ? ? ? ? ? // 顯示加載狀態

? ? ? ? ? ? loading.style.display = 'block';

? ? ? ? ? ? resultSection.style.display = 'none';

? ? ? ? ? ? errorElement.style.display = 'none';

? ? ? ? ? ?

? ? ? ? ? ? navigator.geolocation.getCurrentPosition(

? ? ? ? ? ? ? ? // 這是瀏覽器提供的 獲取用戶當前地理位置 的 API

? ? ? ? ? ? ? ? // 格式navigator.geolocation.getCurrentPosition(定位成功時執行的回調函數(必填),失敗時的,配置)

? ? ? ? ? ? ? ? (position) => {

? ? ? ? ? ? ? ? ? ? // coords是coordinate的縮寫,用到latitude這些表位置的參數時必須用到它

? ? ? ? ? ? ? ? ? ? const lat = position.coords.latitude;

? ? ? ? ? ? ? ? ? ? const lng = position.coords.longitude;

? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? // 更新輸入框

? ? ? ? ? ? ? ? ? ? latitudeInput.value = lat.toFixed(6);

? ? ? ? ? ? ? ? ? ? longitudeInput.value = lng.toFixed(6);

? ? ? ? ? ? ? ? ? ?

? ? ? ? ? ? ? ? ? ? // 進行逆地址解析

? ? ? ? ? ? ? ? ? ? reverseGeocode(lat, lng);

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? (error) => {

? ? ? ? ? ? ? ? ? ? loading.style.display = 'none';

? ? ? ? ? ? ? ? ? ? let errorMessage;

? ? ? ? ? ? ? ? ? ? switch(error.code) {

? ? ? ? ? ? ? ? ? ? ? ? case error.PERMISSION_DENIED:

? ? ? ? ? ? ? ? ? ? ? ? ? ? errorMessage = "用戶拒絕了位置請求";

? ? ? ? ? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? ? ? ? case error.POSITION_UNAVAILABLE:

? ? ? ? ? ? ? ? ? ? ? ? ? ? errorMessage = "位置信息不可用";

? ? ? ? ? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? ? ? ? case error.TIMEOUT:

? ? ? ? ? ? ? ? ? ? ? ? ? ? errorMessage = "獲取位置超時";

? ? ? ? ? ? ? ? ? ? ? ? ? ? break;

? ? ? ? ? ? ? ? ? ? ? ? default:

? ? ? ? ? ? ? ? ? ? ? ? ? ? errorMessage = "煩死了";

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? showError(`獲取位置失敗: ${errorMessage}`);

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? enableHighAccuracy: true,

? ? ? ? ? ? ? ? ? ? // 啟用高精度地位

? ? ? ? ? ? ? ? ? ? timeout: 10000,

? ? ? ? ? ? ? ? ? ? // 超時時間

? ? ? ? ? ? ? ? ? ? maximumAge: 300000

? ? ? ? ? ? ? ? ? ? // 允許緩存位置的最大事件

? ? ? ? ? ? ? ? }

? ? ? ? ? ? );

? ? ? ? });

? ? ? ?

? ? ? ? // 示例坐標(北京天安門)

? ? ? ? // latitudeInput.value = "39.908823";

? ? ? ? // longitudeInput.value = "116.397470";

? ? </script>

</body>

</html>

(編者水平有限,還請大家多多指教)

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/91916.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/91916.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/91916.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

在 Vue3+Vite+TypeScript 項目中使用 svg 文件并支持自定義樣式

參考文檔&#xff1a;vite-svg-loader 安裝與配置 安裝插件 pnpm add vite-svg-loader -D配置 // vite.config.ts import svgLoader from vite-svg-loaderexport default defineConfig({plugins: [vue(),svgLoader({defaultImport: component})] })使用 <script setup …

ShimetaPi M4-R1:國產高性能嵌入式平臺的異構計算架構與OpenHarmony生態實踐

在全球化芯片供應鏈波動及樹莓派等硬件持續漲價的背景下&#xff0c;ShimetaPi M4-R1 作為全棧國產化嵌入式開發平臺&#xff0c;以 高性能異構計算架構 和 開源鴻蒙原生支持 為核心突破點&#xff0c;填補了中高端邊緣設備開發的國產方案空白。其基于瑞芯微 RK3568B2 的硬件設…

zookeeper分布式鎖 -- 讀鎖和寫鎖實現方式

讀鎖和寫鎖讀鎖: 是共享鎖,讀鎖與讀鎖是可以兼容的,所以同時有多個請求都可以持有寫鎖: 是獨占鎖,寫鎖與任何鎖都互斥,所以只有一個請求持有,這個請求釋放寫鎖其他請求才能持有一旦持有寫鎖,說明數據在發送變化就不能讀了,自然一個請求就不能出現讀鎖和寫鎖共存的情況總結: 讀鎖…

第二篇:Linux 文件系統操作:從基礎到進階

目錄 一、文件與目錄管理基礎 創建文件 創建目錄 目錄結構查看 二、鏈接文件深入理解 創建軟鏈接 創建硬鏈接 核心區別對比 三、文件壓縮與解壓縮全攻略 1、壓縮命令對比 2、解壓縮命令 3、三種壓縮方式性能對比 4、通用解壓技巧 四、文件查找與搜索 1、按文件名…

嗶哩嗶哩招游戲內容產品運營

游戲內容產品運營【2026屆】&#xff08;崗位信息已獲jobleap.cn授權轉發到csdn&#xff09;嗶哩嗶哩集團 上海收錄時間&#xff1a; 2025年08月01日職位描述1、負責研究B站游戲創作者的創作過程、動機及遇到的問題&#xff0c;產出研究報告&#xff1b; 2、結合用研分析和相關…

談談Flutter中的Key

目錄 前言 一、什么是Key 1.StatelessWidget 2.StatefulWidget 3.加入Key后的效果 二、什么時候應該使用 Key&#xff1f; 1.Flutter判斷widget的邏輯 1.Flutter判斷組件身份的規則 1.Widget的類型&#xff08;runtimeType&#xff09;相同 2. Key相同&#xff08;ke…

重生之我在暑假學習微服務第八天《OpenFeign篇》

個人主頁&#xff1a;VON文章所屬專欄&#xff1a;微服務 微服務系列文章 重生之我在暑假學習微服務第一天《MybatisPlus-上篇》重生之我在暑假學習微服務第二天《MybatisPlus-下篇》重生之我在暑假學習微服務第三天《Docker-上篇》重生之我在暑假學習微服務第四天《Docker-下篇…

風光儲綜合能源系統雙層優化規劃設計【MATLAB模型實現】

本模型基于雙層優化框架&#xff0c;利用KKT條件、大M法、對偶理論求解&#xff0c;專注于綜合能源系統&#xff08;微電網&#xff09;多電源容量優化配置的模型介紹。代碼采用CPLEX求解器&#xff0c;注釋詳盡&#xff0c;非常適合新手學習該類問題的建模與求解思路。 模型總…

雪花算法重復id問題

原理解析 雪花算法實現簡單、適配性強&#xff0c;無論是電商訂單、日志追蹤還是分布式存儲&#xff0c;都能滿足 “唯一、有序、高效、可擴展” 的核心需求&#xff0c;因此成為分布式ID主流選擇。雪花算法生成的ID是一個64位的整數&#xff0c;由多段不同意義的數字拼接而成&…

MQTT 入門教程:三步從 Docker 部署到 Java 客戶端實現

在物聯網&#xff08;IoT&#xff09;與邊緣計算快速發展的今天&#xff0c;設備間的高效通信成為核心需求。MQTT 作為一種輕量級的發布 / 訂閱模式協議&#xff0c;憑借其低帶寬占用、強穩定性和靈活的消息路由能力&#xff0c;已成為物聯網通信的事實標準。無論是智能家居的設…

公網服務器上Nginx或者Openresty如何屏蔽IP直接掃描

0x01 背景云服務器很多時候為了通信需要設置公網訪問&#xff0c;但是網絡當中存在很多的掃描器&#xff0c;無時無刻在掃描&#xff0c;當80,443端口暴露時&#xff0c;成了這些掃描IP的攻擊對象&#xff0c;無時無刻收到威脅。0x02 掃描攻擊方式1.直接通過公網IP地址進行一些…

C語言(長期更新)第8講 函數遞歸

C語言&#xff08;長期更新&#xff09; 第8講:函數遞歸 跟著潼心走&#xff0c;輕松拿捏C語言&#xff0c;困惑通通走&#xff0c;一去不回頭~歡迎開始今天的學習內容&#xff0c;你的支持就是博主最大的動力。 目錄 C語言&#xff08;長期更新&#xff09; 第8講 函數遞歸…

[硬件電路-129]:模擬電路 - 繼電器的工作原理、關鍵指標、常用芯片與管腳定義

一、工作原理繼電器是一種基于電磁感應原理的自動開關裝置&#xff0c;通過控制小電流電路實現大電流電路的通斷。其核心結構包括&#xff1a;電磁鐵&#xff08;線圈鐵芯&#xff09;&#xff1a;通電時產生磁場&#xff0c;吸引銜鐵動作。觸點系統&#xff1a;包含常開觸點&a…

Haproxy調度算法 - 靜態算法介紹與使用

文章目錄一、概述二、socat工具三、static-rr四、firstHAProxy通過固定參數 balance 指明對后端服務器的調度算法&#xff0c;該參數可以配置在listen或backend選項中。HAProxy的調度算法分為靜態和動態調度算法&#xff0c;但是有些算法可以根據參數在靜態和動態算法中相互轉換…

模擬激光相機工作站版本6.0 5.2.32 6.0.44 6.031 5.2.20

模擬激光相機工作站版本6.0 5.2.32 6.0.44 6.031 5.2.20

AWS Blockchain Templates:快速部署企業級區塊鏈網絡的終極解決方案

無需精通底層架構&#xff0c;一鍵搭建Hyperledger Fabric或以太坊網絡&#xff01;AWS Blockchain Templates 可幫助您快速基于不同的區塊鏈框架在 AWS 上創建和部署區塊鏈網絡。區塊鏈是一種分布式數據庫技術&#xff0c;用于維護不斷增長的交易記錄和智能合約集合&#xff0…

Vue 服務端渲染 Nuxt 使用詳解

Nuxt 是基于 Vue 的高層框架&#xff0c;專注于服務器端渲染應用開發。它封裝了繁瑣的配置和通用模式&#xff0c;提供了開箱即用的 SSR 功能&#xff0c;使開發者能夠專注于編寫業務邏輯。 1. Nuxt 的核心特性 SSR 支持&#xff1a;默認支持服務端渲染&#xff0c;提高應用性…

使用ACK Serverless容器化部署大語言模型FastChat

核心概念 阿里云ACK Serverless&#xff1a;是一種基于 Kubernetes 的無服務器容器服務。用戶無需管理底層節點和服務器&#xff0c;即可快速部署容器化應用&#xff0c;并根據實際使用的 CPU 和內存資源按需付費&#xff0c;只專注于應用本身而非基礎設施管理。 FastChat&…

最新Android Studio漢化教程--兼容插件包

[ ] 軟件版本&#xff1a;Android Studio Meerkat Feature Drop | 2024.3.2 Build #AI-243.25659.59.2432.13423653, built on April 30, 2025 Runtime version: 21.0.613368085-b895.109 amd64 VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o. Toolkit: sun.awt.windows.WT…

Unity_數據持久化_IXmlSerializable接口

Unity數據持久化 三、XML數據持久化 3.5 IXmlSerializable接口 3.5.1 IXmlSerializable接口基礎概念 什么是IXmlSerializable接口&#xff1a; IXmlSerializable 是.NET框架提供的一個接口&#xff0c;允許類自定義XML序列化和反序列化的過程。當默認的XML序列化行為無法滿足需…