小程序获取当前位置所在的城市

╰半夏微凉° 2022-05-21 03:57 471阅读 0赞

1、话不多说,直接上干货

先来张目录结构

70

① index.wxml

  1. <view class="retailStore">
  2. <view class="cnaps borderBottom">
  3. <text>所在城市:</text>
  4. <text class='m-bbt'>{
  5. {province}} {
  6. {city}}</text>
  7. </view>
  8. </view>

②index.js

插入提示:

  1. 申请开发者密钥(key):申请密钥

  2. 下载微信小程序JavaScriptSDK,微信小程序JavaScriptSDK v1.0 下载完成后放入utils文件夹下引用即可

  3. 安全域名设置,在“设置” -> “开发设置”中设置request合法域名,添加https://apis.map.qq.com

测试用的话可以在微信开发工具中选择详情,如下图

70 1

  1. //index.js
  2. //获取应用实例
  3. const app = getApp();
  4. var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
  5. var qqmapsdk;
  6. Page({
  7. data: {
  8. province: '',
  9. city: '',
  10. latitude: '',
  11. longitude: ''
  12. },
  13. onLoad: function () {
  14. qqmapsdk = new QQMapWX({
  15. key: 'xxxx-xxxx-xxxx-xxxx' //自己的key秘钥 http://lbs.qq.com/console/mykey.html 在这个网址申请
  16. });
  17. },
  18. onShow: function () {
  19. let vm = this;
  20. vm.getUserLocation();
  21. },
  22. getUserLocation: function () {
  23. let vm = this;
  24. wx.getSetting({
  25. success: (res) => {
  26. console.log(JSON.stringify(res))
  27. // res.authSetting['scope.userLocation'] == undefined 表示 初始化进入该页面
  28. // res.authSetting['scope.userLocation'] == false 表示 非初始化进入该页面,且未授权
  29. // res.authSetting['scope.userLocation'] == true 表示 地理位置授权
  30. if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
  31. wx.showModal({
  32. title: '请求授权当前位置',
  33. content: '需要获取您的地理位置,请确认授权',
  34. success: function (res) {
  35. if (res.cancel) {
  36. wx.showToast({
  37. title: '拒绝授权',
  38. icon: 'none',
  39. duration: 1000
  40. })
  41. } else if (res.confirm) {
  42. wx.openSetting({
  43. success: function (dataAu) {
  44. if (dataAu.authSetting["scope.userLocation"] == true) {
  45. wx.showToast({
  46. title: '授权成功',
  47. icon: 'success',
  48. duration: 1000
  49. })
  50. //再次授权,调用wx.getLocation的API
  51. vm.getLocation();
  52. } else {
  53. wx.showToast({
  54. title: '授权失败',
  55. icon: 'none',
  56. duration: 1000
  57. })
  58. }
  59. }
  60. })
  61. }
  62. }
  63. })
  64. } else if (res.authSetting['scope.userLocation'] == undefined) {
  65. //调用wx.getLocation的API
  66. vm.getLocation();
  67. }
  68. else {
  69. //调用wx.getLocation的API
  70. vm.getLocation();
  71. }
  72. }
  73. })
  74. },
  75. // 微信获得经纬度
  76. getLocation: function () {
  77. let vm = this;
  78. wx.getLocation({
  79. type: 'wgs84',
  80. success: function (res) {
  81. console.log(JSON.stringify(res))
  82. var latitude = res.latitude
  83. var longitude = res.longitude
  84. var speed = res.speed
  85. var accuracy = res.accuracy;
  86. vm.getLocal(latitude, longitude)
  87. },
  88. fail: function (res) {
  89. console.log('fail' + JSON.stringify(res))
  90. }
  91. })
  92. },
  93. // 获取当前地理位置
  94. getLocal: function (latitude, longitude) {
  95. let vm = this;
  96. qqmapsdk.reverseGeocoder({
  97. location: {
  98. latitude: latitude,
  99. longitude: longitude
  100. },
  101. success: function (res) {
  102. // console.log(JSON.stringify(res));
  103. let province = res.result.ad_info.province
  104. let city = res.result.ad_info.city
  105. vm.setData({
  106. province: province,
  107. city: city,
  108. latitude: latitude,
  109. longitude: longitude
  110. })
  111. },
  112. fail: function (res) {
  113. console.log(res);
  114. },
  115. complete: function (res) {
  116. // console.log(res);
  117. }
  118. });
  119. }
  120. })

效果图:

70 2

谢谢观看,随便转载!!~~~(任性 蜜汁微笑)

仓库地址:项目地址

发表评论

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

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

相关阅读