微信小程序纯前台获取当前具体位置

男娘i 2022-12-26 10:18 446阅读 0赞

微信小程序纯前台获取当前具体位置

    • 准备工作
    • 1.获取当前位置,拉起授权
    • 2.坐标转换,精确定位
    • 3.地址逆解析,坐标转换为文字地址

准备工作

腾讯地图微信小程序开发指南:
https://lbs.qq.com/miniProgram/jsSdk/jsSdkGuide/jsSdkOverview

  1. 申请开发者密钥(key):申请密钥
  2. 开通webserviceAPI服务:控制台 -> key管理 -> 设置(使用该功能的key)-> 勾选webserviceAPI -> 保存(小程序SDK需要用到webserviceAPI的部分服务,所以使用该功能的KEY需要具备相应的权限)
  3. 下载微信小程序JavaScriptSDK,微信小程序JavaScriptSDK v1.2
  4. 安全域名设置,在“设置” -> “开发设置”中设置request合法域名,添加https://apis.map.qq.com
    ps:说实话没找到‘设置’在哪里。。。
    微信开发者工具右上角==》详情-本地设置-不检验合法域名…
    貌似也可以,不知道是不是一个意思,目前运行起来没有问题,暂且先这么用吧,有知道的大佬可以指点一下

1.获取当前位置,拉起授权

1.在app.json中声明permission字段

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

ps:微信小程序getLocation() 需要在app.json中声明permission字段
参考:https://blog.csdn.net/guochanof/article/details/89669839

2.在当前页面js文件中引入qqmap-wx-jssdk.js(JavaScriptSDK v1.2)

  1. const QQMapWX = require('../../../utils/qqmap-wx-jssdk.js')

3.获取当前地理位置 授权验证

  1. // 获取当前地理位置 授权验证
  2. getCurrentLocal(){
  3. let that = this;
  4. wx.getSetting({
  5. success(res) {
  6. if (res.authSetting['scope.userLocation'] == false){// 如果已拒绝授权,则打开设置页面
  7. wx.openSetting({
  8. success(res) {}
  9. })
  10. } else { // 第一次授权,或者已授权,直接调用相关api
  11. that.getMyLocation()
  12. }
  13. }
  14. })
  15. },

4.获取当前地理位置

  1. //获取当前地理位置
  2. getMyLocation(){
  3. let that = this
  4. wx.getLocation({
  5. type: 'wgs84',
  6. success(res) {
  7. ...
  8. }
  9. })
  10. }

参考:https://www.cnblogs.com/Mrrabbit/p/11776332.html

2.坐标转换,精确定位

WebService API——坐标转换
在这里插入图片描述

  1. getMyLocation(){
  2. let that = this
  3. wx.getLocation({
  4. type: 'wgs84',
  5. success(res) {
  6. console.log('location', res);
  7. var locationString = res.latitude + "," + res.longitude;
  8. //坐标转换
  9. wx.request({
  10. url: 'https://apis.map.qq.com/ws/coord/v1/translate?',
  11. data: {
  12. locations:locationString,
  13. type:1,
  14. key:'此处填自己申请好的key'
  15. },
  16. method: 'GET',
  17. success: function (request) {
  18. console.log(request);
  19. var locationString_new = request.data.locations[0].lat + "," + request.data.locations[0].lng;
  20. ...
  21. }
  22. });
  23. }
  24. })
  25. },

3.地址逆解析,坐标转换为文字地址

1.第一种,reverseGeocoder方式:
reverseGeocoder逆地址解析(坐标位置描述)
在这里插入图片描述

  1. getMyLocation(){
  2. let that = this
  3. wx.getLocation({
  4. type: 'wgs84',
  5. success(res) {
  6. console.log(res)
  7. that.getAddress(res.latitude, res.longitude)
  8. }
  9. })
  10. },
  11. getAddress(latitude, longitude) {
  12. let that = this
  13. // 生成 QQMapWX 实例
  14. let qqmapsdk = new QQMapWX({
  15. key: '此处填自己申请好的key'
  16. })
  17. // reverseGeocoder 为 QQMapWX 解析 经纬度的方法
  18. qqmapsdk.reverseGeocoder({
  19. location: {latitude,longitude},
  20. success(res) {
  21. console.log('success', res)
  22. var current_location = res.result.address_component.street_number
  23. that.setData({
  24. current_location: current_location,
  25. flag: true
  26. })
  27. }
  28. })
  29. },

2.第二种,网络请求方式:

  1. getMyLocation(){
  2. let that = this
  3. wx.getLocation({
  4. type: 'wgs84',
  5. success(res) {
  6. console.log('location', res);
  7. var locationString = res.latitude + "," + res.longitude;
  8. wx.request({
  9. url: 'https://apis.map.qq.com/ws/coord/v1/translate?',
  10. data: {
  11. locations:locationString,
  12. type:1,
  13. key:'此处填自己申请好的key'
  14. },
  15. method: 'GET',
  16. success: function (request) {
  17. console.log(request);
  18. var locationString_new = request.data.locations[0].lat + "," + request.data.locations[0].lng;
  19. //逆解析主要代码
  20. wx.request({
  21. url: 'http://apis.map.qq.com/ws/geocoder/v1/',
  22. data: {
  23. "key": "IR7BZ-ZMHLD-6WK4Z-PPNOE-QS7SV-BSBW6",
  24. "location": locationString_new
  25. },
  26. method: 'GET',
  27. success: function (r) {
  28. console.log(r);
  29. //输出一下位置信息
  30. console.log('当前位置',r.data.result.formatted_addresses.recommend)
  31. var current_location = r.data.result.formatted_addresses.recommend
  32. that.setData({
  33. current_location: current_location,
  34. flag: true
  35. })
  36. //这步是将位置信息保存到本地缓存中,key = value的形式
  37. try {
  38. wx.setStorageSync('locationInfo', r.data.result.formatted_addresses.recommend)
  39. } catch (e) {
  40. console.log(e)
  41. }
  42. }
  43. });
  44. }
  45. });
  46. }
  47. })
  48. },

参考:https://www.cnblogs.com/wangshengli520/p/10286591.html
*
*
*
*
*

至此,效果基本实现
在这里插入图片描述

完整代码
app.json

  1. {
  2. "pages":[
  3. "pages/homepage/test/test",
  4. ],
  5. "window":{
  6. "backgroundTextStyle":"light",
  7. "navigationBarBackgroundColor": "#fff",
  8. "navigationBarTitleText": "",
  9. "navigationBarTextStyle":"black"
  10. },
  11. "style": "v2",
  12. "sitemapLocation": "sitemap.json",
  13. "permission": {
  14. "scope.userLocation": {
  15. "desc": "你的位置信息将用于小程序位置接口的效果展示"
  16. }
  17. }
  18. }

wxml:

  1. <view class="flex">
  2. <view class="">当前位置:</view>
  3. <view class="" wx:if="{ {flag}}">{
  4. {current_location}}</view>
  5. <view class="" wx:else bindtap="getCurrentLocal"><button style="font-size:28rpx;">获取地理位置</button></view>
  6. </view>

js:

  1. const QQMapWX = require('../../../utils/qqmap-wx-jssdk.js')
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. current_location:'',
  8. flag: false
  9. },
  10. /**
  11. * 生命周期函数--监听页面加载
  12. */
  13. onLoad: function (options) {
  14. let that = this
  15. that.getCurrentLocal()
  16. },
  17. // 获取当前地理位置 授权验证
  18. getCurrentLocal(){
  19. let that = this;
  20. wx.getSetting({
  21. success(res) {
  22. if (res.authSetting['scope.userLocation'] == false){// 如果已拒绝授权,则打开设置页面
  23. wx.openSetting({
  24. success(res) {}
  25. })
  26. } else { // 第一次授权,或者已授权,直接调用相关api
  27. that.getMyLocation()
  28. }
  29. }
  30. })
  31. },
  32. //获取当前地理位置
  33. getMyLocation(){
  34. let that = this
  35. wx.getLocation({
  36. type: 'wgs84',
  37. success(res) {
  38. console.log('location', res);
  39. var locationString = res.latitude + "," + res.longitude;
  40. wx.request({
  41. url: 'https://apis.map.qq.com/ws/coord/v1/translate?',
  42. data: {
  43. locations:locationString,
  44. type:1,
  45. key:'IR7BZ-ZMHLD-6WK4Z-PPNOE-QS7SV-BSBW6'
  46. },
  47. method: 'GET',
  48. success: function (request) {
  49. console.log(request);
  50. var locationString_new = request.data.locations[0].lat + "," + request.data.locations[0].lng;
  51. wx.request({
  52. url: 'http://apis.map.qq.com/ws/geocoder/v1/',
  53. data: {
  54. "key": "IR7BZ-ZMHLD-6WK4Z-PPNOE-QS7SV-BSBW6",
  55. "location": locationString_new
  56. },
  57. method: 'GET',
  58. success: function (r) {
  59. console.log(r);
  60. //输出一下位置信息
  61. console.log('当前位置',r.data.result.formatted_addresses.recommend)
  62. var current_location = r.data.result.formatted_addresses.recommend
  63. that.setData({
  64. current_location: current_location,
  65. flag: true
  66. })
  67. //这步是将位置信息保存到本地缓存中,key = value的形式
  68. try {
  69. wx.setStorageSync('locationInfo', r.data.result.formatted_addresses.recommend)
  70. } catch (e) {
  71. console.log(e)
  72. }
  73. }
  74. });
  75. }
  76. });
  77. }
  78. })
  79. },
  80. // // 获取当前地理位置(reverseGeocoder方式)
  81. // getMyLocation(){
  82. // let that = this
  83. // wx.getLocation({
  84. // type: 'wgs84',
  85. // success(res) {
  86. // console.log(res)
  87. // that.getAddress(res.latitude, res.longitude)
  88. // }
  89. // })
  90. // },
  91. // getAddress(latitude, longitude) {
  92. // let that = this
  93. // // 生成 QQMapWX 实例
  94. // let qqmapsdk = new QQMapWX({
  95. // key: 'IR7BZ-ZMHLD-6WK4Z-PPNOE-QS7SV-BSBW6'
  96. // })
  97. // // reverseGeocoder 为 QQMapWX 解析 经纬度的方法
  98. // qqmapsdk.reverseGeocoder({
  99. // location: {latitude,longitude},
  100. // success(res) {
  101. // console.log('success', res)
  102. // var current_location = res.result.address_component.street_number
  103. // that.setData({
  104. // current_location: current_location,
  105. // flag: true
  106. // })
  107. // }
  108. // })
  109. // },
  110. })

PS:测试发现 微信开发者工具和真机正常, 预览和发布需要开启手机调试才能看到效果,不开可能看不到效果

发表评论

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

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

相关阅读