当前位置:   article > 正文

uniapp接入腾讯地图实现定位导航功能。_uni-app 地图导航

uni-app 地图导航

打开腾讯地图的官网注册账号登陆进入,滑入我的头像开发者信息:https://lbs.qq.com/service/webService/webServiceGuide/webServiceOverview

2 找到添加的应用,添加key

3 webService API 查询

4 然后在manifest.json 中设置如下,以及小程序id

uniapp用到的api

uni.getLocation 获取当前的地理位置、速度。当用户离开小程序后,此接口无法调用。
uni.openLocation 使用微信内置地图查看位置
this.mapDom = uni.createMapContext('mapDom') 获取map id=mapDom 的实例
this.mapDom.includePoints includePoints 缩放视野展示所有经纬度

bug uni.getLocation 不触发直接将基础库调整为 2.17.0

首页代码

  1. <template>
  2. <view class="">
  3. <div class="header">
  4. <view class="content">
  5. 你要去哪里:
  6. <view class="text-area">
  7. <text @click="toMap()" class="title">{{title}}</text>
  8. </view>
  9. </view>
  10. <input @input="changeCity" v-model="city" type="text" height="50" />
  11. <ul v-show="isShowSearch">
  12. <li @click="getCityHandle(item.address)" v-for="item in suggestionData" :key="item.id">{{item.address}}</li>
  13. </ul>
  14. </div>
  15. </view>
  16. </template>
  17. <script>
  18. import {
  19. Debounce
  20. } from '../../utils/debounce.js'
  21. export default {
  22. data() {
  23. return {
  24. title: '出发',
  25. key: '2J4BZ-3CFEV-OYSPG-UDFGU-ONNNS-63FOT',
  26. city: '河南省鹤壁市浚县',
  27. locationConfig: null,
  28. noClick: true,
  29. suggestionData: null,
  30. isShowSearch:false
  31. }
  32. },
  33. onLoad() {},
  34. methods: {
  35. toMap() {
  36. this.getPosition((location) => {
  37. uni.navigateTo({
  38. url: `/pages/map/index?lat=${ location.lat}&lng=${ location.lng}&city=${this.city}`
  39. })
  40. })
  41. },
  42. getPosition(callback) {
  43. uni.request({
  44. url: `https://apis.map.qq.com/ws/geocoder/v1/?address=${this.city}&key=${this.key}`,
  45. success: ({
  46. data
  47. }) => {
  48. callback(data.result.location)
  49. },
  50. })
  51. },
  52. getCityHandle(val) {
  53. this.city = val
  54. this.isShowSearch = false
  55. },
  56. changeCity: Debounce(function(e) {
  57. this.isShowSearch = true
  58. console.log(432);
  59. this.city = e.target.value
  60. uni.request({
  61. url: `https://apis.map.qq.com/ws/place/v1/suggestion?key=${this.key}&keyword=${this.city}`,
  62. success: ({
  63. data
  64. }) => {
  65. console.log(423, data.data);
  66. this.suggestionData = data.data
  67. },
  68. })
  69. }, 1000)
  70. }
  71. }
  72. </script>
  73. <style>
  74. ul {
  75. height: auto;
  76. width: 90%;
  77. overflow: scroll;
  78. }
  79. ul li {
  80. margin: 2px;
  81. width: 100%;
  82. height: auto;
  83. border-radius: 10upx;
  84. background-color: #ccc;
  85. padding: 10upx;
  86. box-sizing: border-box;
  87. }
  88. .content {
  89. display: flex;
  90. margin-bottom: 20upx;
  91. }
  92. .logo {
  93. height: 200rpx;
  94. width: 200rpx;
  95. margin-top: 200rpx;
  96. margin-left: auto;
  97. margin-right: auto;
  98. margin-bottom: 50rpx;
  99. }
  100. .text-area {
  101. display: flex;
  102. justify-content: center;
  103. border: 1px solid #ccc;
  104. width: 30%;
  105. border-radius: 10upx;
  106. }
  107. .title {
  108. font-size: 36rpx;
  109. color: #8f8f94;
  110. }
  111. .header {
  112. width: 100%;
  113. height: 30upx;
  114. padding: 30upx;
  115. }
  116. .header input {
  117. width: 90%;
  118. border: 1px solid #ccc;
  119. border-radius: 10upx;
  120. }
  121. </style>

地图页面

  1. <template>
  2. <view class="">
  3. <view class="page-body">
  4. <view class="page-section page-section-gap">
  5. <map style="width: 100%; height: 75vh;"
  6. id="mapDom"
  7. show-location
  8. :latitude="latitude"
  9. :longitude="longitude"
  10. :markers="covers"
  11. show-location
  12. @updated="lodingMap"
  13. >
  14. </map>
  15. <div @click="backPositionHandle" class="positionIcon"></div>
  16. </view>
  17. </view>
  18. <view class="footer">
  19. <h3>你的目的地</h3>
  20. <view class="descriptive">{{city}}</view>
  21. <button class="position" @click="getHereHandle">到这里</button>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. latitude: 0,
  30. longitude: 0,
  31. covers: [{
  32. id: 1,
  33. width: 50,
  34. height: 50,
  35. latitude: 0,
  36. longitude: 0,
  37. iconPath: '../../static/1.gif'
  38. }, ],
  39. city:'',
  40. flag:false
  41. }
  42. },
  43. onReady() {
  44. this.mapDom = uni.createMapContext('mapDom')
  45. console.log(423,this.mapDom);
  46. },
  47. onLoad(option) {
  48. this.covers[0].latitude = Number(option.lat)
  49. this.covers[0].longitude = Number(option.lng)
  50. this.city = option.city
  51. // uni.getLocation 不触发直接将基础库调整为 2.17.0
  52. uni.getLocation({
  53. type: 'gcj02',
  54. success: (res) => {
  55. console.log('当前位置的经度:' + res.longitude);
  56. console.log('当前位置的纬度:' + res.latitude);
  57. this.latitude = Number(res.latitude)
  58. this.longitude = Number(res.longitude)
  59. // 获取位置成功之后进行缩放,直观的看到坐标
  60. // includePoints 缩放视野展示所有经纬度
  61. this.mapDom.includePoints({
  62. padding:[140,50,140,50],
  63. points:[
  64. //第一个是我的位置
  65. {
  66. latitude:this.latitude,
  67. longitude:this.longitude
  68. },
  69. // 第二个是店铺的位置
  70. {
  71. latitude:this.covers[0].latitude,
  72. longitude:this.covers[0].longitude
  73. }
  74. ]
  75. })
  76. }
  77. });
  78. },
  79. methods:{
  80. // 地图初始化完成后触发
  81. lodingMap() {
  82. console.log(423);
  83. this.flag = true
  84. },
  85. getHereHandle() {
  86. if(!this.flag) return
  87. // let plugin = requirePlugin('routePlan');
  88. // let key = '2J4BZ-3CFEV-OYSPG-UDFGU-ONNNS-63FOT'; //使用在腾讯位置服务申请的key
  89. // let referer = 'navigateMap'; //调用插件的app的名称
  90. // let endPoint = JSON.stringify({ //终点
  91. // 'name': '北京西站',
  92. // 'latitude': 39.894806,
  93. // 'longitude': 116.321592
  94. // });
  95. // wx.navigateTo({
  96. // url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
  97. // });
  98. uni.openLocation({
  99. latitude:this.covers[0].latitude, //维度
  100. longitude: this.covers[0].longitude, //经度
  101. name: this.city, //目的地定位名称
  102. scale: 15, //缩放比例
  103. // address: "河南省鹤壁市浚县" //导航详细地址
  104. })
  105. },
  106. backPositionHandle() {
  107. if(!this.flag) return
  108. this.mapDom.moveToLocation();
  109. }
  110. }
  111. }
  112. </script>
  113. <style>
  114. .page-section-gap {
  115. position: relative;
  116. }
  117. .footer{
  118. width: 100%;
  119. height: 25vh;
  120. background-color: #fff;
  121. box-sizing: border-box;
  122. padding: 20upx 40upx 0 40upx;
  123. display: flex;
  124. flex-direction: column;
  125. border-radius: 22upx;
  126. }
  127. .position{
  128. height: 90upx;
  129. line-height:90upx;
  130. background-color: #3d89f0;
  131. color: #fff;
  132. width: 100%;
  133. }
  134. .descriptive{
  135. font-size: 30upx;
  136. margin: 30upx 0 30upx 0;
  137. }
  138. .positionIcon {
  139. width: 40upx;
  140. height: 40upx;
  141. border-radius: 50%;
  142. position: absolute;
  143. bottom: 40upx;
  144. right: 40upx;
  145. background-image: url('https://tse2-mm.cn.bing.net/th/id/OIP-C.fCPxTGJccQvVdqrNFqrMRwHaHa?pid=ImgDet&rs=1');
  146. background-size: 100% 100%;
  147. }
  148. </style>

/utils/debounce.js 防抖方法

  1. export const Debounce = (fn, wait) => {
  2. let delay = wait|| 500
  3. let timer
  4. return function () {
  5. let args = arguments;
  6. if (timer) {
  7. clearTimeout(timer)
  8. }
  9. let callNow = !timer
  10. timer = setTimeout(() => {
  11. timer = null
  12. }, delay)
  13. if (callNow) fn.apply(this, args)
  14. }
  15. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/548582
推荐阅读