• uniapp小程序:使用uni.getLocation通过腾讯地图获取相关地址信息详情(超详细)


    先看运行结果:

    流程:

    1、在edge网页搜索腾讯位置服务

    搜索后点击这里

    已经有账号的就进行登录,没有账号的进行注册即可

    点击控制台:

    进去后点击成员管理---->我的应用---->创建应用

    输入相应的参数应用名称(随便写)和应用类型更具你的项目类型进行选择我选择了出行

    选择好后点击创建:

    创建好后点击添加key:

    打开微信小程序开发工具:

    这样就获取到了key:

    2、下载腾讯小程序JavaScriptJDK点击即可跳转

    微信小程序JavaScript SDK | 腾讯位置服务 (qq.com)

    下载其中一个都可以

    解压后放在common目录下皆可,如果没有common路面自己生成即可。

    3、安全域名设计

    在微信小程序后台小程序 (qq.com)​​​​​​

    添加request合法域名,添加apis.map.qq.com

    4、配置manifest.json文件

    1. "permission" : {
    2. "scope.userLocation" : {
    3. "desc" : "为了您更好的体验,请确认获取您的位置"
    4. }
    5. },
    6. "requiredPrivateInfos" : [ "getLocation", "chooseLocation", "chooseAddress" ]

    5、引用代码演示

    我这里吧方法引入到mixins中

    1. //获取用户实时位置
    2. import QQMapWX from "../../common/qqmap-wx-jssdk.js"

    1. //获取用户实时位置
    2. import QQMapWX from "../../common/qqmap-wx-jssdk.js"
    3. export const showMixin ={
    4. data(){
    5. return{
    6. show: true
    7. }
    8. },
    9. methods:{
    10. showto(){
    11. this.show=!this.show
    12. console.log('this.show',this.show)
    13. uni.navigateBack({
    14. delta:1
    15. })
    16. },
    17. async getLocationInfo() {
    18. // this.show = !this.show
    19. return new Promise((resolve) => {
    20. let location = {
    21. longitude: 0,
    22. latitude: 0,
    23. province: "",
    24. city: "",
    25. area: "",
    26. street: "",
    27. address: "",
    28. };
    29. uni.getLocation({
    30. type: "gcj02",
    31. success(res) {
    32. location.longitude = res.longitude;
    33. location.latitude = res.latitude;
    34. const qqmapsdk = new QQMapWX({
    35. key: 'ANDBZ-VEM6T-IPIXG-VEWUH-XJYGS-N2BPT'
    36. });
    37. qqmapsdk.reverseGeocoder({
    38. location,
    39. success(response) {
    40. let info = response.result;
    41. console.log(info);
    42. location.province = info.address_component.province;
    43. location.city = info.address_component.city;
    44. location.area = info.address_component.district;
    45. location.street = info.address_component.street;
    46. location.address = info.address;
    47. resolve(location);
    48. }
    49. });
    50. },
    51. fail(err) {
    52. console.log(err)
    53. }
    54. })
    55. })
    56. }
    57. }
    58. }

    显示:

    引入:

    生命周期调用:

    代码直接使用即可:

    1. <template>
    2. <view class="site">
    3. <view class="map">
    4. <uni-search-bar class="uni-mt-10" radius="100" placeholder="搜索城市/区县/地点" clearButton="none"
    5. cancelButton="none" @confirm="search" />
    6. </view>
    7. <view class="current">
    8. <view style="display: flex; font-size: 28rpx; line-height: 44rpx;">
    9. <uni-icons type="location" size="20"></uni-icons>
    10. <txte v-if="position !== null">当前位置:{{position}}</txte>
    11. </view>
    12. <view style="display: flex; color: #4687e1; font-size: 28rpx;" @click="showto">
    13. <image src="../../../static/images/tabbar/locations.png" mode="aspectFill"
    14. style="width: 35rpx; height: 35rpx; margin-right: 10rpx;"></image>
    15. <text>刷新定位</text>
    16. </view>
    17. </view>
    18. <view class="chosen">
    19. <view v-for="(item,index) in list" :key="index" class="box" @click="selects(index)"
    20. :class="{'active': activeindex === index}">
    21. {{item.name}}
    22. <view class="line" v-if="activeindex === index"></view>
    23. </view>
    24. </view>
    25. <view class="area">
    26. <view class="area-box" v-for="(item,index) in box" :key="index" @click="city(index)"
    27. :class="{'active': activeindexs === index}">
    28. {{item.name}}
    29. </view>
    30. </view>
    31. <view class="ess">
    32. <view v-if="activeindexs !== -1" class="area-box" v-for="(item,index) in boxs" :key="index"
    33. @click="citys(index)" :class="{'active': activeindextwo === index}">
    34. {{item.name}}
    35. </view>
    36. </view>
    37. <uni-popup ref="popup" background-color="#fff">
    38. <view style="width: 300rpx; height: 300rpx;">
    39. <uni-loading></uni-loading>
    40. </view>
    41. </uni-popup>
    42. </view>
    43. </template>
    44. <script>
    45. // import QQMapWX from "../../../common/qqmap-wx-jssdk.js"
    46. import {
    47. showMixin
    48. } from '../../../shopro/mixins/site.js'
    49. export default {
    50. mixins: [showMixin],
    51. data() {
    52. return {
    53. position: null,
    54. activeindex: 0,
    55. activeindexs: -1,
    56. activeindextwo: -1,
    57. list: [{
    58. name: '贵州省'
    59. }],
    60. box: [{
    61. name: '贵阳市'
    62. },
    63. {
    64. name: '安顺市'
    65. },
    66. {
    67. name: '遵义市'
    68. },
    69. {
    70. name: '毕节市'
    71. },
    72. {
    73. name: '黔东南'
    74. },
    75. {
    76. name: '黔东南'
    77. },
    78. {
    79. name: '黔东南'
    80. },
    81. {
    82. name: '黔东南'
    83. }
    84. ],
    85. boxs: [{
    86. name: '花溪区'
    87. },
    88. {
    89. name: '观山湖区'
    90. },
    91. {
    92. name: '南明区'
    93. },
    94. {
    95. name: '云岩区'
    96. },
    97. {
    98. name: '白云区'
    99. },
    100. {
    101. name: '清镇'
    102. }
    103. ],
    104. }
    105. },
    106. async mounted() {
    107. const location = await this.getLocationInfo();
    108. console.log('location', location)
    109. // that.position = location.province + location.city
    110. let position = location.province + location.city + location.area
    111. console.log('position', position)
    112. this.position = position
    113. },
    114. methods: {
    115. citys(index) {
    116. this.activeindextwo = index
    117. },
    118. city(index) {
    119. this.activeindexs = index
    120. },
    121. selects(index) {
    122. this.activeindex = index
    123. uni.showLoading({
    124. title:'加载中',
    125. mask: true
    126. })
    127. // this.$refs.popup.open('center')
    128. }
    129. },
    130. // change(e){
    131. // console.log('当前模式:' + e.type + ',状态:' + e.show);
    132. // }
    133. }
    134. </script>
    135. <style scoped>
    136. .area-box {
    137. width: 130rpx;
    138. height: 80rpx;
    139. background-color: #fff;
    140. text-align: center;
    141. line-height: 80rpx;
    142. margin-top: 20rpx;
    143. margin-right: 10rpx;
    144. margin-left: 10rpx;
    145. border-radius: 20rpx;
    146. }
    147. .ess {
    148. width: 100vw;
    149. height: 300rpx;
    150. /* background-color: #c9c9c9; */
    151. display: flex;
    152. justify-content: flex-start;
    153. align-items: flex-start;
    154. flex-flow: row wrap;
    155. align-content: flex-start;
    156. overflow-y: scroll;
    157. }
    158. .area {
    159. width: 100vw;
    160. height: 300rpx;
    161. /* background-color: aqua; */
    162. display: flex;
    163. justify-content: flex-start;
    164. align-items: flex-start;
    165. flex-flow: row wrap;
    166. align-content: flex-start;
    167. overflow-y: scroll;
    168. }
    169. .line {
    170. position: absolute;
    171. bottom: 10rpx;
    172. width: 70%;
    173. height: 5rpx;
    174. background-color: brown;
    175. border-radius: 50rpx;
    176. left: 0;
    177. right: 0;
    178. margin: auto;
    179. }
    180. .box {
    181. width: 130rpx;
    182. height: 100%;
    183. /* background-color: antiquewhite; */
    184. text-align: center;
    185. line-height: 80rpx;
    186. }
    187. .active {
    188. font-weight: bold;
    189. position: relative;
    190. }
    191. .chosen {
    192. width: 100vw;
    193. height: 80rpx;
    194. padding: 0 20rpx;
    195. background: #f8f8f8;
    196. }
    197. .current {
    198. display: flex;
    199. align-items: center;
    200. justify-content: space-between;
    201. width: 100vw;
    202. height: 100rpx;
    203. padding: 0 30rpx;
    204. }
    205. .map {
    206. width: 100vw;
    207. height: 100rpx;
    208. }
    209. .site {
    210. width: 100vw;
    211. height: 100vh;
    212. background-color: #fff;
    213. }
    214. </style>

    运行结果:

  • 相关阅读:
    前端周刊第三十六期
    go学习-基本知识点
    【机器学习】分值融合方法
    GPO:组策略与系统配置
    MATLAB实战Sobel边缘检测(Edge Detection)
    59.【C++迷宫小游戏(超详细,有手就行)】
    企业哪些项目可以参与CMMI的评估?
    如何使用Docker部署开源Leanote蚂蚁笔记并发布个人博客至公网
    【Linux】-文件操作(重定向、缓冲区以及Linux下一切皆文件的详解)
    如何搭建网课查题公众号?小白教程!内附免费题库接口!
  • 原文地址:https://blog.csdn.net/m0_65069237/article/details/136679330