Vue3管理后臺項目使用高德地圖選點的實現(xiàn)
前言
最近在做的管理后臺項目中有一個業(yè)務(wù)場景是添加門店的地址和經(jīng)緯度,地址可以輸入,但經(jīng)緯度這樣處理卻不合適,最好是讓用戶在地圖上搜索或者直接點擊獲取點的經(jīng)緯度等詳細信息。因為我們的app中使用了高德地圖,所以管理后臺我也選用高德地圖來實現(xiàn)上面的業(yè)務(wù)需求,下面來看一下具體的使用流程吧。
獲取地圖Key
- 登錄高德開放平臺
- 創(chuàng)建應(yīng)用,添加Key,選擇Web端(JS API),生成Key和安全密鑰
引入地圖 JSAPI
項目中使用了官方推薦的 JSAPI Loader 來加載地圖
- 安裝官方 npm 包 @amap/amap-jsapi-loader
- 配置安全密鑰(不安全的方式),其它配置方式在這里
<script setup>
import AMapLoader from '@amap/amap-jsapi-loader';
window._AMapSecurityConfig = {
securityJsCode: '你申請的安全密鑰',
};
</script>初始化地圖
- 創(chuàng)建一個id為mapContainer的div元素
- 調(diào)用initMap方法初始化相關(guān)地圖插件
<script setup>
const map = shallowRef(null);
let AMapObj;
function initMap() {
AMapLoader.load({
key: '你申請的Key',
version: '2.0',
}).then(AMap => {
AMapObj = AMap;
map.value = new AMap.Map('mapContainer');
})
}
</script>地圖選點
項目中提供搜索選點和直接點擊地圖選點兩種方法
- 搜索選點:使用 element-plus 的 autocomplete 組件結(jié)合地圖搜索服務(wù)實現(xiàn)下拉選擇地點
- 點擊選點:借助地圖點擊事件獲取地點的經(jīng)緯度信息,然后使用地圖逆地理編碼api解析出地址信息 選擇地點之后同步繪制 marker 標(biāo)記,同時將 marker 移動到地圖中心點并設(shè)置縮放比例
組件化使用
為了方便一整套邏輯的復(fù)用,我將以上流程全部封裝到一個組件中,通過 v-model 綁定所選地點的詳細信息,方便選擇地點之后將信息同步到父組件。
下面貼出組件全部的代碼
<template>
<div class="map-wrapper">
<div id="mapcontainer"></div>
<div class="search-box">
<el-autocomplete
v-model="keyword"
:fetch-suggestions="handleSearch"
:trigger-on-focus="false"
clearable
placeholder="輸入城市+關(guān)鍵字搜索"
@select="handleSelect"
style="width: 300px"
/>
<el-input
v-model="location.longitude"
placeholder="點擊地圖選擇經(jīng)度"
maxlength="15"
readonly
style="width: 150px; margin: 0 5px"
></el-input>
<el-input
v-model="location.latitude"
placeholder="點擊地圖選擇緯度"
maxlength="15"
readonly
style="width: 150px"
></el-input>
</div>
</div>
</template>
<script setup>
import AMapLoader from '@amap/amap-jsapi-loader';
window._AMapSecurityConfig = {
securityJsCode: '你申請的安全密鑰',
};
const props = defineProps({
modelValue: {
type: Object,
default() {
return {};
},
},
});
const emit = defineEmits(['update:modelValue']);
const map = shallowRef(null);
// 地點
const location = computed({
get() {
return props.modelValue;
},
set(val) {
emit('update:modelValue', val);
},
});
watch(location, (val) => {
if (val.longitude && val.latitude) {
drawMarker();
}
}
);
const keyword = ref('');
let placeSearch, AMapObj, marker, geocoder;
function initMap() {
AMapLoader.load({
key: '', // 申請好的Web端Key,首次調(diào)用 load 時必填
version: '2.0'
}).then(AMap => {
AMapObj = AMap;
map.value = new AMap.Map('mapcontainer');
// 添加點擊事件
map.value.on('click', onMapClick);
if (location.value.longitude) {
drawMarker();
}
AMap.plugin(
['AMap.ToolBar','AMap.Scale','AMap.Geolocation','AMap.PlaceSearch', 'AMap.Geocoder'],
() => {
// 縮放條
const toolbar = new AMap.ToolBar();
// 比例尺
const scale = new AMap.Scale();
// 定位
const geolocation = new AMap.Geolocation({
enableHighAccuracy: true, //是否使用高精度定位,默認(rèn):true
timeout: 10000, //超過10秒后停止定位,默認(rèn):5s
position: 'RT', //定位按鈕的??课恢?
buttonOffset: new AMap.Pixel(10, 20), //定位按鈕與設(shè)置的停靠位置的偏移量,默認(rèn):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: 30, // 單頁顯示結(jié)果條數(shù)
pageIndex: 1, // 頁碼
citylimit: false, // 是否強制限制在設(shè)置的城市內(nèi)搜索
autoFitView: true,
});
}
);
})
}
onMounted(() => {
initMap();
});
// 搜索地圖
function handleSearch(queryString, cb) {
placeSearch.search(queryString, (status, result) => {
if (result && typeof result === 'object' && result.poiList) {
const list = result.poiList.pois;
list.forEach(item => {
item.value = item.name;
item.label = item.name;
});
cb(list);
} else {cb([])}
});
}
// 點擊地圖
function onMapClick(e) {
const { lng, lat } = e.lnglat;
// 逆地理編碼
geocoder.getAddress([lng, lat], (status, result) => {
if (status === 'complete' && result.info === 'OK') {
const { addressComponent, formattedAddress } = result.regeocode;
let { city, province, district } = addressComponent;
if (!city) {
// 直轄市
city = province;
}
location.value = {
longitude: lng,
latitude: lat,
address: formattedAddress,
zone: [province, city, district],
};
}
});
}
// 點擊搜索項
function handleSelect(item) {
const { pname, cityname, adname, address, name } = item;
const { lng, lat } = item.location;
location.value = {
longitude: lng,
latitude: lat,
address,
zone: [pname, cityname, adname],
name,
};
map.value.setZoomAndCenter(16, [lng, lat]);
}
// 繪制地點marker
function drawMarker(val) {
const { longitude, latitude } = location.value || val;
if (marker) {
marker.setMap(null);
}
marker = new AMapObj.Marker({
position: new AMapObj.LngLat(longitude, latitude),
anchor: 'bottom-center',
});
map.value.add(marker);
map.value.setZoomAndCenter(16, [longitude, latitude]);
}
</script>
<style lang="scss" scoped>
.map-wrapper {
position: relative;
width: 100%;
height: 400px;
#mapcontainer {
width: 100%;
height: 100%;
}
.search-box {
position: absolute;
top: 10px;
left: 10px;
z-index: 1;
display: flex;
align-items: center;
}
}
</style>拓展
如果系統(tǒng)適配了暗黑模式,可以通過監(jiān)聽當(dāng)前暗黑模式狀態(tài),來動態(tài)切換地圖淺色主題和深色主題,從而實現(xiàn)地圖暗黑模式的適配,這就留給大家自行探索了。
到此這篇關(guān)于Vue3管理后臺項目使用高德地圖選點的實現(xiàn)的文章就介紹到這了,更多相關(guān)Vue3 高德地圖選點內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決Vue中引入swiper,在數(shù)據(jù)渲染的時候,發(fā)生不滑動的問題
今天小編就為大家分享一篇解決Vue中引入swiper,在數(shù)據(jù)渲染的時候,發(fā)生不滑動的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
vue項目使用$router.go(-1)返回時刷新原來的界面操作
這篇文章主要介紹了vue項目使用$router.go(-1)返回時刷新原來的界面操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

