uniapp的微信小程序,获取授权,获取中文街道地理位置

本是古典 何须时尚 2023-02-23 05:06 107阅读 0赞

uniapp的微信小程序,获取授权,获取中文街道地理位置

添加小程序,兑换各种视频教程/数据资源

imgimg

  1. 详细见代码:在需要.vue页面调用如下方法。

    onReady(){

    1. this.isGetLocation();

    },
    methods: {

    1. getAuthorizeInfo(a="scope.userLocation"){ //1. uniapp弹窗弹出获取授权(地理,个人微信信息等授权信息)弹窗
    2. var _this=this;
    3. uni.authorize({
    4. scope: a,
    5. success() { //1.1 允许授权
    6. _this.getLocationInfo();
    7. },
    8. fail(){ //1.2 拒绝授权
    9. console.log("你拒绝了授权,无法获得周边信息")
    10. }
    11. })
    12. },
    13. getLocationInfo(){ //2. 获取地理位置
    14. var _this=this;
    15. uni.getLocation({
    16. type: 'wgs84',
    17. success (res) {
    18. console.log("你当前经纬度是:")
    19. console.log(res)
    20. let latitude,longitude;
    21. latitude = res.latitude.toString();
    22. longitude = res.longitude.toString();
    23. uni.request({
    24. header:{
    25. "Content-Type": "application/text"
    26. },
    27. url:'http://apis.map.qq.com/ws/geocoder/v1/?location='+latitude+','+longitude+'&key=MVGBZ-R2U3U-W5CVY-2PQID-AT4VZ-PDF35',
    28. success(re) {
    29. console.log("中文位置")
    30. console.log(re)
    31. if(re.statusCode===200){
    32. console.log("获取中文街道地理位置成功")
    33. }else{
    34. console.log("获取信息失败,请重试!")
    35. }
    36. }
    37. });
    38. }
    39. });
    40. },
    41. isGetLocation(a="scope.userLocation"){ // 3. 检查当前是否已经授权访问scope属性,参考下截图
    42. var _this=this;
    43. uni.getSetting({
    44. success(res) {
    45. if (!res.authSetting[a]) { //3.1 每次进入程序判断当前是否获得授权,如果没有就去获得授权,如果获得授权,就直接获取当前地理位置
    46. _this.getAuthorizeInfo()
    47. }else{
    48. _this.getLocationInfo()
    49. }
    50. }
    51. });
    52. }

    }

img

  1. 微信小程序中,目前版本无法自动直接弹窗用户用户信息scope.userInfo信息,需要按钮点击主动授权引导。

    methods:{

    1. bindGetUserInfo(e) {
    2. if (e.detail.userInfo){
    3. //用户按了允许授权按钮
    4. } else {
    5. //用户按了拒绝按钮
    6. }
    7. }

    }

  2. uni-app配置微信小程序的appid: 开发过程中,需要在unpackage>>dist>>dev>>mp-weixin>>app.json中加入如下配置:

    “permission”: {

    1. "scope.userLocation": {
    2. "desc": "你的位置信息将用于小程序位置接口的效果展示"
    3. }

    }

或者在manifest.json的源码视图中配置:配置appid和地理位置

  1. "mp-weixin": { /* 小程序特有相关 */
  2. "appid": "", //需要配置appid
  3. "setting": {
  4. "urlCheck": false
  5. },
  6. "usingComponents": true,
  7. "permission": {
  8. "scope.userLocation": {
  9. "desc": "你的位置信息将用于小程序位置接口的效果展示"
  10. }
  11. }
  12. }

发表评论

表情:
评论列表 (有 0 条评论,107人围观)

还没有评论,来说两句吧...

相关阅读