uniapp使用uni.chooseLocation()打开地图选择位置
时间:2024-03-29 20:00:30 来源:网络cs 作者:康由 栏目:培训机构 阅读:
使用uni.chooseLocation()打开地址选择位置
1、打开微信开发平台申请权限
【开发】–【开发管理】–【接口设置】–点击去开通,开通之后才可以使用。
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>
本文链接:https://www.kjpai.cn/news/2024-03-29/150544.html,文章来源:网络cs,作者:康由,版权归作者所有,如需转载请注明来源和作者,否则将追究法律责任!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。