文章来源(hbsjsd.cn)湖北高端网站定制开发公司-速建时代
首先需要在配置文件中进行配置授权提示 点击源码视图 添加下面代码
/* 小程序特有相关 */ "mp-weixin" : { "appid" : "wxda33332553aa5", "setting" : { "urlCheck" : false }, "permission" : { "scope.userLocation" : { "desc" : "您的位置将用于绑定您的区域" } }, "usingComponents" : true },
主要添加
"permission" : { "scope.userLocation" : { "desc" : "您的位置将用于绑定您的区域" } },
如果获取当前位置需要多次使用可以封装在公共js文件中
//网络请求 map: function() { //对参数进行处理 return new Promise((reslove, reject) => { uni.chooseLocation({ success:(res)=> { reslove(res); }, fail:()=>{ uni.getSetting({ success: (res) => { var status = res.authSetting; if(!status['scope.userLocation']){ uni.showModal({ title:"是否授权当前位置", content:"需要获取您的地理位置,请确认授权,否则地图功能将无法使用", success:(tip)=>{ if(tip.confirm){ uni.openSetting({ success:(data)=>{ if(data.authSetting['scope.userLocation']===true){ uni.showToast({ title:"授权成功", icon:"success", duration:1000 }) uni.chooseLocation({ success: (res) => { reslove(res); // this.getRegionFn(res); } }) }else{ uni.showToast({ title:"授权失败", icon:"none", duration:1000 }) } } }) } } }) } }, fail: (res) => { uni.showToast({ title:"调用授权窗口失败", icon:"none", duration:1000 }) } }) } }); }); },
页面调用
<view @click="chooseLocation" class="iconfont icon-daohang"> </view>
//获取位置 chooseLocation(){ this.helper.map().then(res => { this.homeAddressStreet=res.address this.latitude=res.latitude this.name=res.name this.longitude=res.longitude }); },
[声明]原创不易,请转发者备注下文章来源(hbsjsd.cn)【速建时代】。