• 注册
当前位置:1313e > 小程序 >正文

微信小程序获取当前地理位置中文_微信小程序位置授权并获取具体地理位置

一 获取用户的当前设置及二次授权

//首次进入,点击‘选择地址’

//弹出询问框

拒绝授权后,再次进入该页面或者点击页面某按钮(获取位置)绑定JS,再打开授权。

getSetting:function(){ //获取用户的当前设置

const _this = this;

wx.getSetting({

success: (res) => {

// res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面

// res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权

// res.authSetting['scope.userLocation'] == true 表示 地理位置授权

if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {

//未授权

wx.showModal({

title: '请求授权当前位置',

content: '需要获取您的地理位置,请确认授权',

success: function (res) {

if (res.cancel) {

//取消授权

wx.showToast({

title: '拒绝授权',

icon: 'none',

duration: 1000

})

} else if (res.confirm) {

//确定授权,通过wx.openSetting发起授权请求

wx.openSetting({

success: function (res) {

if (res.authSetting["scope.userLocation"] == true) {

wx.showToast({

title: '授权成功',

icon: 'success',

duration: 1000

})

//再次授权,调用wx.getLocation的API

_this.goAddress();

} else {

wx.showToast({

title: '授权失败',

icon: 'none',

duration: 1000

})

}

}

})

}

}

})

} else if (res.authSetting['scope.userLocation'] == undefined) {

//用户首次进入页面,调用wx.getLocation的API

_this.goAddress();

}

else {

// console.log('授权成功')

//调用wx.getLocation的API

_this.goAddress();

}

}

})

},

第二步:在需要获取地址的页面中:

//加载腾讯位置服务js文件(必须)

var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');

var qqmapsdk = new QQMapWX({

key: '开发密钥(key)' // 必填

});

wx.getLocation({

type: "wgs84",

success: function (res) {

_this.setData({

longitude: options.lat ? Number(options.lng) : res.longitude,

latitude: options.lng ? Number(options.lat) : res.latitude,

markers: [{

latitude: options.lat ? Number(options.lat) : res.latitude,

longitude: options.lng ? Number(options.lng) : res.longitude,

}]

})

if (options.lat){

_this.setData({

address: options.address,

})

} else {

//根据坐标获取当前位置名称,显示在顶部:腾讯地图逆地址解析,前面已引入SDK

qqmapsdk.reverseGeocoder({

location: {

latitude: res.latitude,

longitude: res.longitude

},

success: function (addressRes) {

const result = addressRes.result;

_this.setData({

address: result.address,

})

}

})

}

},

fail:function(){

wx.navigateBack({

delta:1

})

}

})

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 162202241@qq.com 举报,一经查实,本站将立刻删除。

最新评论

欢迎您发表评论:

请登录之后再进行评论

登录