Vue 使用高德地圖添加點(diǎn)標(biāo)記 + 點(diǎn)擊地圖獲取坐標(biāo) + 帶搜索(即地理編碼 + 逆地理編碼) 附完整示例
高德地圖:
與真實(shí)世界聯(lián)通 - 高德開(kāi)放平臺(tái)為開(kāi)發(fā)者賦能,將地圖精致地呈現(xiàn)在您的應(yīng)用中
無(wú)論基于哪種平臺(tái),都可以通過(guò)高德開(kāi)放平臺(tái)API和SDK,輕松地完成地圖的構(gòu)建工作
效果

一、介紹
1、官方文檔:地圖 | 高德地圖API
地圖 | 高德地圖API地圖,地圖sdk,地圖JS API,地圖定制,自定義地圖,地圖覆蓋物,地圖繪制,路線規(guī)劃,坐標(biāo)轉(zhuǎn)換,距離/面積計(jì)算,距離測(cè)量,室內(nèi)地圖,地圖顯示,地圖個(gè)性化,地圖開(kāi)發(fā),室內(nèi)定位
https://lbs.amap.com/product/map#/
二、準(zhǔn)備工作
1、注冊(cè)/登錄賬號(hào)

2、點(diǎn)擊控制臺(tái)

3、創(chuàng)建應(yīng)用

4、獲取key和密鑰,如上圖所示
注:使用web服務(wù)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:'', //設(shè)置您的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, //是否使用高精度定位,默認(rèn):true
timeout: 10000, //超過(guò)10秒后停止定位,默認(rèn):5s
position: 'RT', //定位按鈕的??课恢?
buttonOffset: new AMap.Pixel(10, 20), //定位按鈕與設(shè)置的??课恢玫钠屏?,默認(rèn):Pixel(10, 20)
zoomToAccuracy: true, //定位成功后是否自動(dòng)調(diào)整地圖視野到定位點(diǎn)
});
geocoder = new AMap.Geocoder({
city: '全國(guó)',
});
map.value.addControl(geolocation);
map.value.addControl(toolbar);
map.value.addControl(scale);
placeSearch = new AMap.PlaceSearch({
// map: map.value,
city: '全國(guó)',
pageSize: 10, // 單頁(yè)顯示結(jié)果條數(shù)
pageIndex: 1, // 頁(yè)碼
citylimit: false, // 是否強(qiáng)制限制在設(shè)置的城市內(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">點(diǎn)擊復(fù)制</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('復(fù)制成功');
e.clearSelection();
});
clipboard.on('error', function(e) {
if(!e.text) message.error('暫無(wú)可復(fù)制的內(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: '', //設(shè)置您的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, // 是否使用高精度定位,默認(rèn):true
timeout: 10000, // 超過(guò)10秒后停止定位,默認(rèn):5s
position: 'RT', // 定位按鈕的??课恢?
buttonOffset: new AMap.Pixel(10, 20), // 定位按鈕與設(shè)置的??课恢玫钠屏?,默認(rèn):Pixel(10, 20)
zoomToAccuracy: true, // 定位成功后是否自動(dòng)調(diào)整地圖視野到定位點(diǎn)
});
geocoder = new AMap.Geocoder({
city: '全國(guó)',
});
map.value.addControl(geolocation);
map.value.addControl(toolbar);
map.value.addControl(scale);
placeSearch = new AMap.PlaceSearch({
city: '全國(guó)',
pageSize: 10, // 單頁(yè)顯示結(jié)果條數(shù)
pageIndex: 1, // 頁(yè)碼
citylimit: false, // 是否強(qiáng)制限制在設(shè)置的城市內(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
}
});
}
// 點(diǎn)擊地圖
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為對(duì)應(yīng)的地理位置詳細(xì)信息
keyword.value = result.regeocode.formattedAddress
}
})
drawMarker(e.lnglat)
}
// 點(diǎn)擊搜索項(xiàng)
function handleSelect(item) {
console.log(item);
drawMarker(item.location)
if (item.location != null) {
coord.value = item.location.lng + ',' + item.location.lat
}
}
// 繪制地點(diǎn)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一鍵復(fù)制的詳細(xì)使用方法參考地址
到此這篇關(guān)于Vue 使用高德地圖添加點(diǎn)標(biāo)記 + 點(diǎn)擊地圖獲取坐標(biāo) + 帶搜索(即地理編碼 + 逆地理編碼) 附完整示例的文章就介紹到這了,更多相關(guān)vue使用高德地圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)自定義"模態(tài)彈窗"組件實(shí)例代碼
頁(yè)面中會(huì)有很多時(shí)候需要彈窗提示,我們可以寫(xiě)一個(gè)彈窗組件,下面這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)自定義"模態(tài)彈窗"組件的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12
webpack配置導(dǎo)致字體圖標(biāo)無(wú)法顯示的解決方法
下面小編就為大家分享一篇webpack配置導(dǎo)致字體圖標(biāo)無(wú)法顯示的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
Vue實(shí)施重新發(fā)布和軟件熱更新的經(jīng)驗(yàn)分享
在Web應(yīng)用的開(kāi)發(fā)周期中,版本管理和軟件熱更新是一個(gè)不可或缺的話題,隨著Vue.js框架的流行,越來(lái)越多的應(yīng)用程序選擇使用Vue來(lái)構(gòu)建前端,本文將探討在Vue應(yīng)用中實(shí)施重新發(fā)布和熱更新的最佳實(shí)踐,需要的朋友可以參考下2024-09-09
axios解決高并發(fā)的方法:axios.all()與axios.spread()的操作
這篇文章主要介紹了axios解決高并發(fā)的方法:axios.all()與axios.spread()的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Vue在echarts?tooltip中添加點(diǎn)擊事件案例詳解
本文主要介紹了Vue項(xiàng)目中在echarts?tooltip添加點(diǎn)擊事件的案例詳解,代碼具有一定的價(jià)值,感興趣的小伙伴可以來(lái)學(xué)習(xí)一下2021-11-11
vue-element內(nèi)table插入超鏈接a標(biāo)簽的使用
在Vue Element的table組件中插入超鏈接,可以使用<el-link>標(biāo)簽替代傳統(tǒng)的<a>標(biāo)簽,實(shí)現(xiàn)更加整潔的UI設(shè)計(jì),具體操作是替換原有的<span>標(biāo)簽,直接使用<el-link>進(jìn)行超鏈接的插入,使得鏈接樣式與Element UI保持一致2024-09-09

