uniapp使用uni.chooseLocation()打開地圖選擇位置詳解
更新時間:2023年06月30日 09:28:43 作者:哇,女前端哎!
這篇文章主要給大家介紹了關于uniapp使用uni.chooseLocation()打開地圖選擇位置的相關資料,因為最近在項目中遇到一個要用戶授權位置且可以用戶自己選擇位置的功能,需要的朋友可以參考下
使用uni.chooseLocation()打開地址選擇位置
1、打開微信開發(fā)平臺申請權限
【開發(fā)】–【開發(fā)管理】–【接口設置】–點擊去開通,開通之后才可以使用。
2、對小程序進行設置
“requiredPrivateInfos”:[“chooseLocation”]
1.第一種在 uniapp進行設置
2.第二種在原生微信小程序上設置
3、在app.vue里添加微信用戶授權
onLaunch: function() { uni.authorize({ scope: 'scope.userLocation', success: function () { console.log('用戶同意了授權') } }) },
4、在頁面調起地圖選擇
<template> <view class="content"> <button @tap="authVerification">請選擇位置</button> <template v-if="currentLocation.address"> <div>name:{{currentLocation.name}}</div> <div>address:{{currentLocation.address}}</div> <div>latitude:{{currentLocation.latitude}}</div> <div>longitude:{{currentLocation.longitude}}</div> </template> </view> </template> <script> export default { data() { return { currentLocation:{}, } }, onShow () { uni.getStorage({ key: 'currentLocation', success: (res) => { this.currentLocation = res.data } }) }, methods: { authVerification () { uni.getSetting({ success: (res) => { if (res.authSetting['scope.userLocation']) { /* 用戶授權成功時走這里 */ this.handerChooseLocation() } else if (res.authSetting['scope.userLocation'] === undefined) { /* 用戶未授權時走這里 */ console.log('沒有授權') this.handleOpenSetting() } else { /* 用戶拒絕了授權后走這里 */ console.log('拒絕了授權 false') this.handleOpenSetting() } }, }) }, handerChooseLocation (latitude, longitude) { uni.chooseLocation({ latitude: latitude || '', longitude: longitude || '', success: (res) => { console.log('wx.chooseLocation res=', res) uni.setStorageSync('currentLocation', res) }, fail: function (err) { console.log('取消按鈕', err) } }) }, handleOpenSetting () { wx.openSetting({ success: (res) => { console.log('定位 openSetting', res) if (res.authSetting["scope.userLocation"]) { this.handerChooseLocation() } } }) } } } </script>
總結
到此這篇關于uniapp使用uni.chooseLocation()打開地圖選擇位置的文章就介紹到這了,更多相關uniapp打開地圖選擇位置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!