uniapp 微信小程序用户授权获取当前位置信息 腾讯地图

男娘i 2022-10-16 03:58 624阅读 0赞

获取微信小程序的AppID

官方文档
https://lbs.qq.com/qqmap_wx_jssdk/index.html
在uni-app项目中的 manifest.json 文件中的微信小程序获取AppID
以及开启位置接口
在这里插入图片描述

获取腾讯位置服务的Key

搜索腾讯地图api进去并登录https://lbs.qq.com/
在这里插入图片描述

下载sdk

下载微信小程序JavaScriptSDK ,也可以通过访问腾讯地图文档https://lbs.qq.com/qqmap_wx_jssdk/index.html查看教程
在这里插入图片描述
将下载的压缩包解压放入项目静态文件夹中
在这里插入图片描述
复制刚刚配置的Key填入代码中
在这里插入图片描述

实现定位服务:

vuex配置:
store 》index.js

  1. import Vue from "vue"
  2. import Vuex from "vuex"
  3. // 引入腾讯地图jssdk文件
  4. import QQMapWX from "../static/js/qqmap-wx-jssdk.min.js"
  5. Vue.use(Vuex)
  6. const store = new Vuex.Store({
  7. state: {
  8. // 默认城市
  9. addressInfo:{
  10. city: '昆明市',
  11. district: '西山区',
  12. street: '',
  13. province: '云南省',
  14. address: '',
  15. name: '',
  16. city_code: '',
  17. lat: '',
  18. lng: '',
  19. }
  20. },
  21. mutations: {
  22. newCityFun(state, newCity) {
  23. state.addressInfo.city = newCity.city
  24. state.addressInfo.district = newCity.district
  25. state.addressInfo.street = newCity.street
  26. state.addressInfo.province = newCity.province
  27. state.addressInfo.address = newCity.address
  28. state.addressInfo.name = newCity.name
  29. state.addressInfo.city_code = newCity.city_code
  30. state.addressInfo.lat = newCity.lat
  31. state.addressInfo.lng = newCity.lng
  32. console.log(state.addressInfo.city)
  33. }
  34. },
  35. actions: {
  36. getCity(context) {
  37. // 向用户发起授权请求,弹框提示
  38. uni.authorize({
  39. // 获取用户定位信息
  40. scope: "scope.userLocation",
  41. // 用户同意授权执行
  42. success() {
  43. // 引入腾讯地图api
  44. // 实例化API核心类
  45. let qqmapsdk = new QQMapWX({
  46. // 填写自己的Key值,这个值是与AppID绑定的
  47. key: 'NOMBZ-FMNCS-NCAOO-6AC37-DF76Z-VFBVX'
  48. });
  49. //获取位置信息
  50. uni.getLocation({
  51. type: 'gcj02',
  52. success: function(res) {
  53. console.log('当前位置的经度:' + res.longitude)
  54. console.log('当前位置的纬度:' + res.latitude)
  55. // 逆地址解析方法
  56. qqmapsdk.reverseGeocoder({
  57. location: {
  58. latitude: res.latitude,
  59. longitude: res.longitude
  60. },
  61. success(res) {
  62. var newCity = { }
  63. console.log(res)
  64. // 取到用户的定位城市,赋值传递出去
  65. newCity.city = res.result.address_component.city
  66. newCity.district = res.result.address_component.district
  67. newCity.street = res.result.address_component.street
  68. newCity.province = res.result.address_component.province
  69. newCity.address = res.result.address
  70. newCity.name = res.result.ad_info.name
  71. newCity.city_code = res.result.ad_info.city_code
  72. newCity.lat = res.result.location.lat
  73. newCity.lng = res.result.location.lng
  74. context.commit('newCityFun', newCity)
  75. },
  76. fail(res) {
  77. console.log("逆地址解析失败")
  78. console.log(res)
  79. }
  80. })
  81. }
  82. })
  83. },
  84. // 若用户不同意授权,弹框提示
  85. fail(res) {
  86. uni.showToast({
  87. icon: "none",
  88. title: '注意:需要获取您的定位授权,否则部分功能将无法使用',
  89. duration: 2000
  90. })
  91. }
  92. })
  93. }
  94. }
  95. })
  96. export default store

页面使用

page 》index.vue

  1. <template>
  2. <view class="content">
  3. <view class="text-area">
  4. <text class="title">{ { title}}</text>
  5. </view>
  6. <view>{ { addressInfo.city}}</view>
  7. <view>{ { addressInfo.district}}</view>
  8. <view>{ { addressInfo.street}}</view>
  9. <view>{ { addressInfo.province}}</view>
  10. <view>{ { addressInfo.address}}</view>
  11. <view>{ { addressInfo.name}}</view>
  12. <view>{ { addressInfo.city_code}}</view>
  13. <view>{ { addressInfo.lat}},{ { addressInfo.lng}}</view>
  14. </view>
  15. </template>
  16. <script>
  17. import {
  18. mapState
  19. } from 'vuex';
  20. export default {
  21. data() {
  22. return {
  23. title: 'Hello',
  24. }
  25. },
  26. onLoad() {
  27. },
  28. onReady() {
  29. this.$store.dispatch('getCity')
  30. },
  31. methods: {
  32. },
  33. computed: {
  34. ...mapState(["addressInfo"])
  35. // newCity() {
  36. // return this.$store.state.city
  37. // console.log(this.$store.state.city)
  38. // }
  39. }
  40. }
  41. </script>
  42. <style>
  43. .content {
  44. display: flex;
  45. flex-direction: column;
  46. align-items: center;
  47. justify-content: center;
  48. }
  49. .text-area {
  50. display: flex;
  51. justify-content: center;
  52. }
  53. .title {
  54. font-size: 36rpx;
  55. color: #8f8f94;
  56. }
  57. </style>

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
搜索 闲益小区 小程序了解更多或者扫描太阳码
在这里插入图片描述

发表评论

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

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

相关阅读