vue3+uniapp中使用高德地圖實(shí)現(xiàn)撒點(diǎn)效果(操作步驟)
前言:vue3+uniapp中使用高德地圖實(shí)現(xiàn)撒點(diǎn)效果
實(shí)現(xiàn)效果:
操作步驟:
1、引入高德插件,并生成js配置插件,詳情步驟請點(diǎn)我
import amapFile from '../../libs/amap-wx.js'
2、頁面上配置我們的map標(biāo)簽
<template> <!-- 地圖控件 --> <view> <map id="map" :longitude="mapObj.longitude" :latitude="mapObj.latitude" :scale="mapObj.scale" :markers="mapObj.markers" @markertap="markertap" @click="mapClick" ></map> </view> </template>
3、js部分,定義我們相關(guān)變量
let mapObj = reactive({
longitude:116.481028, //經(jīng)度
latitude:39.989643, //維度
scale:17, //地圖默認(rèn)縮放等級
markers: [], //點(diǎn)位數(shù)據(jù)
})
let locationListener = ref('')
let initMap = function(){
const myAmapFun = new amapFile.AMapWX({
key: 'bb****', // 你的高德地圖API Key
});
console.log('myAmapFun',myAmapFun)
}
onShow(()=>{
initMap()
initMapWZ()
})4、獲取我們當(dāng)前位置
// 獲取當(dāng)前位置信息
let initMapWZ = function(){
console.log('init')
// uni.getLocation uniapp官網(wǎng)提供的獲取定位的方法,調(diào)用過多會導(dǎo)致無法使用,需要使用監(jiān)聽方法
uni.getLocation({
type: 'gcj02', //國測局坐標(biāo) gcj02,要使用地圖map必須使用這個
success: res=> {
getNowDWBackFun(res)
},
fail:err=>{
//getLocation:fail 頻繁調(diào)用會增加電量損耗,可考慮使用 wx.onLocationChange 監(jiān)聽地理位置變化
console.log(err)
startLocationWatch()
},
complete:()=>{
console.log('complete')
}
})
}5、更新我們當(dāng)前實(shí)時位置
let startLocationWatch = ()=> {
// 1. 檢查權(quán)限
uni.authorize({
scope: 'scope.userLocation',
success: () => {
// 2. 開啟位置更新
uni.startLocationUpdate({
success: () => {
// 3. 監(jiān)聽位置變化
locationListener = uni.onLocationChange((res) => {
// 在此更新地圖或處理位置數(shù)據(jù)
getNowDWBackFun(res)
})
},
fail: (err) => {
console.error('啟動位置更新失敗:', err)
}
})
},
fail: () => {
uni.showModal({
title: '權(quán)限提示',
content: '需要位置權(quán)限以持續(xù)獲取位置',
success: (res) => {
if (res.confirm) uni.openSetting()
}
})
}
})
}6、將我們當(dāng)前位置,用圖片展示在地圖上
// 拿到當(dāng)前最新位置以后的回調(diào)方法
let getNowDWBackFun = res=>{
console.log('當(dāng)前位置的經(jīng)度:' + res.longitude);
console.log('當(dāng)前位置的緯度:' + res.latitude);
mapObj.longitude = res.longitude
mapObj.latitude = res.latitude
mapObj.markers = [{
id: 1,
longitude:res.longitude,
latitude: res.latitude,
iconPath: '../../static/now.png',
title: '當(dāng)前位置',
width:25,
height:25
}]
}7、當(dāng)我們的界面關(guān)閉時候,停止我們的實(shí)時更新位置方法
// 停止監(jiān)聽
let stopLocationWatch = ()=>{
if (locationListener.value) {
uni.stopLocationUpdate() // 停止位置更新
}
}
onHide(()=>{
stopLocationWatch()
})到此這篇關(guān)于vue3+uniapp中使用高德地圖實(shí)現(xiàn)撒點(diǎn)效果的文章就介紹到這了,更多相關(guān)vue3 uniapp高德地圖撒點(diǎn)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解vue項目優(yōu)化之按需加載組件-使用webpack require.ensure
本篇文章主要介紹了詳解vue項目優(yōu)化之按需加載組件-使用webpack require.ensure,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-06-06
vue中ref()和reactive()區(qū)別小結(jié)
本文主要介紹了vue中ref()和reactive()區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-07-07
vue3之Suspense加載異步數(shù)據(jù)的使用
本文主要介紹了vue3之Suspense加載異步數(shù)據(jù)的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
proxy代理不生效以及vue?config.js不生效解決方法
在開發(fā)Vue項目過程中,使用了Proxy代理進(jìn)行數(shù)據(jù)劫持,但是在實(shí)際運(yùn)行過程中發(fā)現(xiàn)代理并沒有生效,也就是說數(shù)據(jù)并沒有被劫持,這篇文章主要給大家介紹了關(guān)于proxy代理不生效以及vue?config.js不生效解決方法的相關(guān)資料,需要的朋友可以參考下2023-11-11
Vue3中關(guān)于ref和reactive的區(qū)別分析
這篇文章主要介紹了vue3關(guān)于ref和reactive的區(qū)別分析,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-06-06
vuejs在解析時出現(xiàn)閃爍的原因及防止閃爍的方法
這篇文章主要介紹了vuejs在解析時出現(xiàn)閃爍的原因及防止閃爍的方法,本文介紹的非常詳細(xì),具有參考借鑒價值,感興趣的朋友一起看看吧2016-09-09

