01. 在uniapp項(xiàng)目的manifest.json中的位置接口,描述文字就是提示文字:
02. 在 uniapp 項(xiàng)目源碼視圖的微信小程序代碼中添加如下:
/* 小程序特有相關(guān) */
"mp-weixin" : {
...... // 原有的
// 新增的
"permission" : {
"scope.userLocation" : {
"desc" : "位置測(cè)試"
}
},
"requiredPrivateInfos" : [ "getLocation" ]
},
03. 在需要獲取定位的頁(yè)面寫如下方法,然后onload里面調(diào)用此方法
getlocation() {
uni.getLocation({
type: 'wgs84',
success: (res) => {
console.log('當(dāng)前位置的經(jīng)度:' + res.longitude);
console.log('當(dāng)前位置的緯度:' + res.latitude);
this.longitude = res.longitude
this.latitude = res.latitude
// 調(diào)用后端接口根據(jù)得到的經(jīng)緯度獲取地址
console.log(res, "根據(jù)經(jīng)緯度獲取地址");
},
// 若用戶點(diǎn)擊拒絕獲取位置則彈出提示
fail: (err) => {
uni.showModal({
content: '檢測(cè)到您沒(méi)打開獲取位置功能權(quán)限,是否去設(shè)置打開?',
confirmText: "確認(rèn)",
cancelText: '取消',
success: (res) => {
if (res.confirm) {
uni.openSetting({
success: (res) => {
uni.showToast({
title: '授權(quán)后請(qǐng)重新打開此頁(yè)面',
icon: 'none'
})
},
fail: (err) => {
console.log(err)
}
})
} else {
uni.showToast({
title: '獲取地理位置授權(quán)失敗',
icon: 'none',
success: () => {
// 返回上一頁(yè)
setTimeout(() => {
uni.showToast({
title: "返回上一頁(yè)",
icon: 'none'
})
// uni.navigateBack({
// delta: 1
// })
}, 500)
}
})
}
}
})
},
})
},
進(jìn)入定位頁(yè)面就會(huì)彈出,如果沒(méi)有彈出,說(shuō)明小程序的位置服務(wù)不可用,去小程序后臺(tái)查看下
![](/files/attmgn/2024/7/admin20240730170034098_1.jpg)
如果點(diǎn)擊允許按鈕,那么就會(huì)獲得當(dāng)前位置的經(jīng)緯度
![](/files/attmgn/2024/7/admin20240730170034133_2.jpg)
否則如果點(diǎn)擊拒絕,就會(huì)彈窗提示打開定位
![](/files/attmgn/2024/7/admin20240730170034167_3.jpg)
如果點(diǎn)擊取消,說(shuō)明用戶不需要獲取位置,那么就返回上一頁(yè)
如果點(diǎn)擊確認(rèn)就會(huì)彈出設(shè)置權(quán)限:
![](/files/attmgn/2024/7/admin20240730170034230_4.jpg)
這一步在模擬器上管用,切換為允許就會(huì)正常獲取到經(jīng)緯度了,但是在真機(jī)上允許還是不行,你必須手動(dòng)打開手機(jī)的定位才可以
標(biāo)記位置和拖動(dòng)地圖
獲取到定位之后,將定位展示在地圖上,使用自帶的map組件,實(shí)現(xiàn)拖動(dòng)地圖標(biāo)記出當(dāng)前地圖的中心點(diǎn)位置,此時(shí)要修改下map標(biāo)簽
<map :latitude="latitude" :longitude="longitude" id="map" ref="map" :markers="markers"
@regionchange='regionchange' style="width: 100%; height: 1200rpx;"></map>
data里面配置下地圖標(biāo)記點(diǎn)
markers: [{
id: 0, // 使用 marker點(diǎn)擊事件 需要填寫id
latitude: 0,
longitude: 0,
iconPath: 'https://bpic.51yuansu.com/pic3/cover/00/77/39/58c6caf1c78e3_610.jpg',
// 設(shè)置markers的寬高
width: 30,
height: 40,
}]
拖動(dòng)地圖時(shí)獲取地圖的中心點(diǎn)的經(jīng)緯度
regionchange(e) {
// 更新當(dāng)前搜索周邊的經(jīng)緯度
if (e.type == 'end') {
console.log(e, "拖動(dòng)后的經(jīng)緯度");
// 需要獲取地圖中心的經(jīng)緯度 否則無(wú)法確定唯一經(jīng)緯度
this.getCenterLanLat()
}
},
// 獲取當(dāng)前地圖中心的經(jīng)緯度
getCenterLanLat() {
uni.createMapContext("map", this).getCenterLocation({
type: 'gcj02',
success: (res) => {
console.log("地圖中心點(diǎn)經(jīng)緯度:", res);
// 把當(dāng)前經(jīng)緯度設(shè)置給標(biāo)記點(diǎn)
this.markers[0].latitude = res.latitude
this.markers[0].longitude = res.longitude
},
fail: (err) => {}
})
},
<template>
<view>
<map :latitude="latitude" :longitude="longitude" id="map" ref="map" :markers="markers"
@regionchange='regionchange' style="width: 100%; height: 1200rpx;"></map>
</view>
</template>
<script>
export default {
data() {
return {
longitude: '',
latitude: '',
// 地圖中心標(biāo)記點(diǎn)
markers: [{
id: 0, // 使用 marker點(diǎn)擊事件 需要填寫id
latitude: 0,
longitude: 0,
iconPath: 'https://bpic.51yuansu.com/pic3/cover/00/77/39/58c6caf1c78e3_610.jpg',
// 設(shè)置markers的寬高
width: 30,
height: 40,
}]
}
},
onLoad() {
this.getlocation()
},
methods: {
getlocation() {
uni.getLocation({
type: 'wgs84',
success: (res) => {
console.log('當(dāng)前位置的經(jīng)度:' + res.longitude);
console.log('當(dāng)前位置的緯度:' + res.latitude);
this.longitude = res.longitude
this.latitude = res.latitude
// 調(diào)用后端接口根據(jù)得到的經(jīng)緯度獲取地址
console.log(res, "根據(jù)經(jīng)緯度獲取地址");
},
// 若用戶點(diǎn)擊拒絕獲取位置則彈出提示
fail: (err) => {
uni.showModal({
content: '檢測(cè)到您沒(méi)打開獲取位置功能權(quán)限,是否去設(shè)置打開?',
confirmText: "確認(rèn)",
cancelText: '取消',
success: (res) => {
if (res.confirm) {
uni.openSetting({
success: (res) => {
uni.showToast({
title: '授權(quán)后請(qǐng)重新打開此頁(yè)面',
icon: 'none'
})
},
fail: (err) => {
console.log(err)
}
})
} else {
uni.showToast({
title: '獲取地理位置授權(quán)失敗',
icon: 'none',
success: () => {
// 返回上一頁(yè)
setTimeout(() => {
uni.showToast({
title: "返回上一頁(yè)",
icon: 'none'
})
// uni.navigateBack({
// delta: 1
// })
}, 500)
}
})
}
}
})
},
})
},
regionchange(e) {
// 更新當(dāng)前搜索周邊的經(jīng)緯度
if (e.type == 'end') {
console.log(e, "拖動(dòng)后的經(jīng)緯度");
// 需要獲取地圖中心的經(jīng)緯度 否則無(wú)法確定唯一經(jīng)緯度
this.getCenterLanLat()
}
},
// 獲取當(dāng)前地圖中心的經(jīng)緯度
getCenterLanLat() {
uni.createMapContext("map", this).getCenterLocation({
type: 'gcj02',
success: (res) => {
console.log("地圖中心點(diǎn)經(jīng)緯度:", res);
// 把當(dāng)前經(jīng)緯度設(shè)置給標(biāo)記點(diǎn)
this.markers[0].latitude = res.latitude
this.markers[0].longitude = res.longitude
},
fail: (err) => {}
})
},
}
}
</script>
<style>
</style>
效果:
![](/files/attmgn/2024/7/admin20240730170034265_5.jpg)
該文章在 2024/7/30 17:01:51 編輯過(guò)