微信小程序获取地理位置

微信小程序获取地理位置,第1张

概述小程序只支持获取当前位置的经纬度,并不能直接获取到地理名称,需要通过第三方来逆地址解析,这里我选择的是腾讯位置服务 在使用前需要去申请key,这里是地址:https://lbs.qq.com/console/mykey.html?console=mykey 下面上栗子: <view class="hotcity-common thisCity">当前选择城市</view> <view clas

小程序只支持获取当前位置的经纬度,并不能直接获取到地理名称,需要通过第三方来逆地址解析,这里我选择的是腾讯位置服务

在使用前需要去申请key,这里是地址:https://lbs.qq.com/console/mykey.html?console=mykey
下面上栗子:

<vIEw >当前选择城市</vIEw>  <vIEw >{{city}}</vIEw>
var QQMapWX = require('../../utils/qqmap-wx-Jssdk.Js');var qqmapsdk;page({    data: {    city: "",latitude: '',longitude: '',}    onLoad: function() {    qqmapsdk = new QQMapWX({      key: 'FD2BZ-R34KJ-4P4FW-KAW2S-ZASLV-GCFBR' //自己的key秘钥     });    this.getUserLocation();  },getUserLocation: function() {    let vm = this;    wx.getSetting({      success: (res) => {        console.log("设置信息"+JsON.stringify(res))        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({                  success: function(dataAu) {                    if (dataAu.authSetting["scope.userLocation"] == true) {                      wx.showToast({                        Title: '授权成功',icon: 'success',duration: 1000                      })                      //再次授权,调用wx.getLocation的API                      vm.getLocation();                    } else {                      wx.showToast({                        Title: '授权失败',duration: 1000                      })                    }                  }                })              }            }          })        } else if (res.authSetting['scope.userLocation'] == undefined) {          //调用wx.getLocation的API          vm.getLocation();        } else {          //调用wx.getLocation的API          vm.getLocation();        }      }    })  },// 微信获得经纬度  getLocation: function() {    let vm = this;    wx.getLocation({      type: 'wgs84',success: function(res) {        console.log(JsON.stringify(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: function(latitude,longitude) {    let vm = this;    qqmapsdk.reverseGeocoder({      location: {        latitude: latitude,longitude: longitude      },success: function(res) {        let province = res.result.ad_info.province        let city = res.result.ad_info.city        vm.setData({          province: province,city: city,latitude: latitude,longitude: longitude        })       }    });  },})

以上就是小程序获取地理位置的全部方法

总结

以上是内存溢出为你收集整理的微信小程序获取地理位置全部内容,希望文章能够帮你解决微信小程序获取地理位置所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1065389.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存