小程序 定位

本文深入探讨了小程序中如何实现精准定位,包括GPS、Wi-Fi和基站定位的原理及使用,同时讲解了如何处理定位权限问题和优化用户体验。通过实例代码展示在小程序中集成地图API进行实时位置显示和路径导航的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   // 引入SDK核心类--去官网下载qqmap-wx-jssdk.min.js
    var QQMapWX = require('./static/js/qqmap-wx-jssdk.min.js');

   /**
   * 微信获得经纬度
   */
  getLocation() {
    let vm = this;
    wx.getLocation({
      type: 'wgs84',
      success: function(res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function(res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },
/**
   * 获取当前地理位置
   */
  getLocal(latitude, longitude) {
    let vm = this;
    // 实例化API核心类
    var qqmapsdk = new QQMapWX({
      key: 'MOPBZ-2QSCW-OJZRG-R7QLR-AYVEV-2WF6B' // 必填
    });
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      get_poi: 1,
      poi_options: 'policy=2;radius=3000;page_size=20;page_index=1',
      success: function(res) {
        //自己拿需要的东西
      },
      fail: function(res) {
        console.log("解析失败");
      },
    });
  },

   /***
   * 授权地理位置登录判断---在需要定位的页面调用这个方法
   */
  getUserLocation() {
    var vm = this;
    wx.getSetting({
      success: res => {
        console.log("成功")
        console.log(res.authSetting['scope.userLocation'])
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '标题',
            content: '您的位置信息将用于小程序位置接口的效果展示',
            confirmColor: '#ee5002',
            success: function(res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function(dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      console.log(dataAu)
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      vm.getLocation()
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          vm.getLocation()
        } else {
          vm.getLocation()
        }


      },
      fail: (res) => {
        console.log("失败")
      }
    })

  },

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值