Vue 使用高德地圖添加點標記 + 點擊地圖獲取坐標 + 帶搜索(即地理編碼 + 逆地理編碼) 附完整示例
高德地圖:
與真實世界聯(lián)通 - 高德開放平臺為開發(fā)者賦能,將地圖精致地呈現(xiàn)在您的應用中
無論基于哪種平臺,都可以通過高德開放平臺API和SDK,輕松地完成地圖的構(gòu)建工作
效果
一、介紹
1、官方文檔:地圖 | 高德地圖API
地圖 | 高德地圖API地圖,地圖sdk,地圖JS API,地圖定制,自定義地圖,地圖覆蓋物,地圖繪制,路線規(guī)劃,坐標轉(zhuǎn)換,距離/面積計算,距離測量,室內(nèi)地圖,地圖顯示,地圖個性化,地圖開發(fā),室內(nèi)定位
https://lbs.amap.com/product/map#/
二、準備工作
1、注冊/登錄賬號
2、點擊控制臺
3、創(chuàng)建應用
4、獲取key和密鑰,如上圖所示
注:使用web服務API,如下圖所示
三、安裝依賴包
1、安裝命令
npm i @amap/amap-jsapi-loader --save
2、這是我的版本
"@amap/amap-jsapi-loader": "^1.0.1",
四、使用步驟
1、在index.html文件中引入密鑰
代碼如下(示例):
<script type="text/javascript"> window._AMapSecurityConfig = { securityJsCode: '', // 你的密鑰 } </script>
2、在vue文件中引入依賴包
代碼如下(示例):
import AMapLoader from "@amap/amap-jsapi-loader"
3、申明變量并初始化調(diào)用
代碼如下(示例):
import { shallowRef, defineEmits, ref, onBeforeUnmount } from 'vue'; const map = shallowRef(null); let AMapObj, placeSearch, marker, geocoder; function initMap(){ AMapLoader.load({ key:'', //設置您的key version:"2.0", plugins:['AMap.ToolBar','AMap.Driving'], AMapUI:{ version:"1.1", plugins:[], }, Loca:{ version:"2.0.0" }, }).then((AMap)=>{ // console.log(AMap); AMapObj = AMap; map.value = new AMap.Map("map-box",{ viewMode:"3D", zoom:10, zooms:[2,22], center: [105.602725,37.076636], }); map.value.on('click', onMapClick); AMap.plugin( ['AMap.ToolBar','AMap.Scale','AMap.Geolocation','AMap.PlaceSearch', 'AMap.Geocoder','AMap.AutoComplete'], () => { // 縮放條 const toolbar = new AMap.ToolBar(); // 比例尺 const scale = new AMap.Scale(); // 定位 const geolocation = new AMap.Geolocation({ enableHighAccuracy: true, //是否使用高精度定位,默認:true timeout: 10000, //超過10秒后停止定位,默認:5s position: 'RT', //定位按鈕的停靠位置 buttonOffset: new AMap.Pixel(10, 20), //定位按鈕與設置的??课恢玫钠屏?,默認:Pixel(10, 20) zoomToAccuracy: true, //定位成功后是否自動調(diào)整地圖視野到定位點 }); geocoder = new AMap.Geocoder({ city: '全國', }); map.value.addControl(geolocation); map.value.addControl(toolbar); map.value.addControl(scale); placeSearch = new AMap.PlaceSearch({ // map: map.value, city: '全國', pageSize: 10, // 單頁顯示結(jié)果條數(shù) pageIndex: 1, // 頁碼 citylimit: false, // 是否強制限制在設置的城市內(nèi)搜索 autoFitView: true, }); }); }).catch(e=>{ console.log(e); }) } initMap();
五、完整示例
Index.html
<!DOCTYPE html> <html lang="zh_CN" id="htmlRoot"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> <meta name="renderer" content="webkit" /> <meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=0" /> <title> <%= title %> </title> <script type="text/javascript"> window._AMapSecurityConfig = { securityJsCode: '', // 你的密鑰 } </script> </head> <body> <div id="app"> </div> </body> </html>
Map.vue
<template> <div class="home"> <div id="map-box"></div> <div class="info-box"> <a-select v-model:value="keyword" show-search placeholder="輸入關(guān)鍵字搜索" style="width: 300px" :default-active-first-option="false" :show-arrow="false" :filter-option="false" :not-found-content="null" @search="handleSearch" > <a-select-option v-for="item in data" :key="item.id" :value="item.id" @click="handleSelect(item)"> <div class="d-flex flex-column"> <span>{{item.name}}</span> <span style="font-size: '10px'; color: #999999">{{item.address}}</span> </div> </a-select-option> </a-select> <a-tooltip> <template #title v-if="coord">點擊復制</template> <span style="margin: 5px 8px;"> 經(jīng)緯度:<span class="copy" style="cursor: pointer;" :data-clipboard-text="coord">{{coord}}</span> </span> </a-tooltip> </div> </div> </template> <script lang="ts" setup> import { shallowRef, ref, onBeforeUnmount } from 'vue'; import AMapLoader from "@amap/amap-jsapi-loader"; import { message } from 'ant-design-vue'; import ClipboardJS from 'clipboard'; const clipboard = new ClipboardJS('.copy'); clipboard.on('success', function(e) { console.log(e); console.info('Text:', e.text); message.info('復制成功'); e.clearSelection(); }); clipboard.on('error', function(e) { if(!e.text) message.error('暫無可復制的內(nèi)容') }); onBeforeUnmount(() => { clipboard.destroy(); }) const map = shallowRef(null); const keyword = ref(null); const data = ref([]); const coord = ref(''); let AMapObj, placeSearch, marker, geocoder; function initMap(){ AMapLoader.load({ key: '', //設置您的key version: "2.0", plugins: ['AMap.ToolBar','AMap.Driving'], AMapUI: { version: "1.1", plugins: [], }, Loca:{ version: "2.0.0" }, }).then((AMap)=>{ // console.log(AMap); AMapObj = AMap; map.value = new AMap.Map("map-box",{ viewMode:"3D", zoom:10, zooms:[2,22], center: [105.602725,37.076636], }); map.value.on('click', onMapClick); AMap.plugin( ['AMap.ToolBar','AMap.Scale','AMap.Geolocation','AMap.PlaceSearch', 'AMap.Geocoder','AMap.AutoComplete'], () => { // 縮放條 const toolbar = new AMap.ToolBar(); // 比例尺 const scale = new AMap.Scale(); // 定位 const geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默認:true timeout: 10000, // 超過10秒后停止定位,默認:5s position: 'RT', // 定位按鈕的??课恢? buttonOffset: new AMap.Pixel(10, 20), // 定位按鈕與設置的??课恢玫钠屏?,默認:Pixel(10, 20) zoomToAccuracy: true, // 定位成功后是否自動調(diào)整地圖視野到定位點 }); geocoder = new AMap.Geocoder({ city: '全國', }); map.value.addControl(geolocation); map.value.addControl(toolbar); map.value.addControl(scale); placeSearch = new AMap.PlaceSearch({ city: '全國', pageSize: 10, // 單頁顯示結(jié)果條數(shù) pageIndex: 1, // 頁碼 citylimit: false, // 是否強制限制在設置的城市內(nèi)搜索 autoFitView: true, }); }); }).catch(e=>{ console.log(e); }) } // 搜索地圖 function handleSearch(str) { placeSearch.search(str, (status, result) => { console.log(result); if (result && typeof result === 'object' && result.poiList) { const list = result.poiList.pois; list.forEach(item => { item.value = item.name; item.label = item.name; }); data.value = list } }); } // 點擊地圖 function onMapClick(e) { coord.value = e.lnglat.lng + ',' + e.lnglat.lat geocoder.getAddress([e.lnglat.lng, e.lnglat.lat], function(status, result) { if (status === 'complete' && result.info === 'OK') { // result為對應的地理位置詳細信息 keyword.value = result.regeocode.formattedAddress } }) drawMarker(e.lnglat) } // 點擊搜索項 function handleSelect(item) { console.log(item); drawMarker(item.location) if (item.location != null) { coord.value = item.location.lng + ',' + item.location.lat } } // 繪制地點marker function drawMarker(location) { if (location == null) return let longitude = location.lng, latitude = location.lat if (marker) { marker.setMap(null); } marker = new AMapObj.Marker({ position: new AMapObj.LngLat(longitude, latitude), anchor: 'bottom-center', }); marker.on('click', () => { coord.value = location; }) map.value.add(marker); map.value.setZoomAndCenter(16, [longitude, latitude]); } initMap(); </script> <style lang="less" scoped> .home{ height: 500px; width: 100%; padding: 0px; margin: 0px; position: relative; .info-box { position: absolute; top: 8px; right: 8px; width: 300px; background-color: #1f1f1f; display: flex; flex-direction: column; } } #map-box{ height: 100%; width: 100%; padding: 0px; margin: 0px; } </style> <style scoped> :deep() .amap-logo { display: none !important; } :deep() .amap-copyright { display: none !important; } </style>
注:clipboard一鍵復制的詳細使用方法參考地址
到此這篇關(guān)于Vue 使用高德地圖添加點標記 + 點擊地圖獲取坐標 + 帶搜索(即地理編碼 + 逆地理編碼) 附完整示例的文章就介紹到這了,更多相關(guān)vue使用高德地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實現(xiàn)自定義"模態(tài)彈窗"組件實例代碼
頁面中會有很多時候需要彈窗提示,我們可以寫一個彈窗組件,下面這篇文章主要給大家介紹了關(guān)于vue實現(xiàn)自定義"模態(tài)彈窗"組件的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2021-12-12Vue實施重新發(fā)布和軟件熱更新的經(jīng)驗分享
在Web應用的開發(fā)周期中,版本管理和軟件熱更新是一個不可或缺的話題,隨著Vue.js框架的流行,越來越多的應用程序選擇使用Vue來構(gòu)建前端,本文將探討在Vue應用中實施重新發(fā)布和熱更新的最佳實踐,需要的朋友可以參考下2024-09-09axios解決高并發(fā)的方法:axios.all()與axios.spread()的操作
這篇文章主要介紹了axios解決高并發(fā)的方法:axios.all()與axios.spread()的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11Vue在echarts?tooltip中添加點擊事件案例詳解
本文主要介紹了Vue項目中在echarts?tooltip添加點擊事件的案例詳解,代碼具有一定的價值,感興趣的小伙伴可以來學習一下2021-11-11vue-element內(nèi)table插入超鏈接a標簽的使用
在Vue Element的table組件中插入超鏈接,可以使用<el-link>標簽替代傳統(tǒng)的<a>標簽,實現(xiàn)更加整潔的UI設計,具體操作是替換原有的<span>標簽,直接使用<el-link>進行超鏈接的插入,使得鏈接樣式與Element UI保持一致2024-09-09