首页 > 其他分享 >uniapp接入腾讯地图实现定位导航功能。

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

时间:2023-11-27 13:24:38浏览次数:34  
标签:city uniapp longitude 定位导航 key 腾讯 latitude uni width

转自:https://blog.csdn.net/qq_54753561/article/details/129500254

 打开腾讯地图的官网注册账号登陆进入,滑入我的头像开发者信息: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             
 11              <input @input="changeCity" v-model="city" type="text" height="50" />
 12             <ul v-show="isShowSearch">
 13                 <li @click="getCityHandle(item.address)" v-for="item in suggestionData" :key="item.id">{{item.address}}</li>
 14             </ul>
 15         </div>
 16         
 17     </view>
 18  
 19 </template>
 20  
 21 <script>
 22     import {
 23         Debounce
 24     } from '../../utils/debounce.js'
 25  
 26     export default {
 27         data() {
 28             return {
 29                 title: '出发',
 30                 key: '2J4BZ-3CFEV-OYSPG-UDFGU-ONNNS-63FOT',
 31                 city: '河南省鹤壁市浚县',
 32                 locationConfig: null,
 33                 noClick: true,
 34                 suggestionData: null,
 35                 isShowSearch:false
 36             }
 37         },
 38         onl oad() {},
 39         methods: {
 40             toMap() {
 41  
 42                 this.getPosition((location) => {
 43                     uni.navigateTo({
 44                         url: `/pages/map/index?lat=${ location.lat}&lng=${ location.lng}&city=${this.city}`
 45                     })
 46                 })
 47             },
 48             getPosition(callback) {
 49                 uni.request({
 50                     url: `https://apis.map.qq.com/ws/geocoder/v1/?address=${this.city}&key=${this.key}`,
 51                     success: ({
 52                         data
 53                     }) => {
 54                         callback(data.result.location)
 55                     },
 56                 })
 57             },
 58             getCityHandle(val) {
 59                 this.city = val
 60                 this.isShowSearch = false
 61             },
 62             changeCity: Debounce(function(e) {
 63                 this.isShowSearch = true
 64                 console.log(432);
 65                 this.city = e.target.value
 66                 uni.request({
 67                     url: `https://apis.map.qq.com/ws/place/v1/suggestion?key=${this.key}&keyword=${this.city}`,
 68                     success: ({
 69                         data
 70                     }) => {
 71                         console.log(423, data.data);
 72                         this.suggestionData = data.data
 73                     },
 74                 })
 75  
 76             }, 1000)
 77         }
 78     }
 79 </script>
 80  
 81 <style>
 82     ul {
 83         height: auto;
 84             width: 90%;
 85         overflow: scroll;
 86     }
 87  
 88     ul li {
 89         margin: 2px;
 90         width: 100%;
 91         height: auto;
 92         border-radius: 10upx;
 93         background-color: #ccc;
 94         padding: 10upx;
 95         box-sizing: border-box;
 96     }
 97  
 98     .content {
 99         display: flex;
100         margin-bottom: 20upx;
101     }
102  
103     .logo {
104         height: 200rpx;
105         width: 200rpx;
106         margin-top: 200rpx;
107         margin-left: auto;
108         margin-right: auto;
109         margin-bottom: 50rpx;
110     }
111  
112     .text-area {
113         display: flex;
114         justify-content: center;
115         border: 1px solid #ccc;
116         width: 30%;
117             border-radius: 10upx;
118     }
119  
120     .title {
121         font-size: 36rpx;
122         color: #8f8f94;
123     }
124  
125     .header {
126         width: 100%;
127         height: 30upx;
128         padding: 30upx;
129  
130     }
131  
132     .header input {
133             width: 90%;
134         border: 1px solid #ccc;
135         border-radius: 10upx;
136     }
137 </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  
 25 </template>
 26  
 27 <script>
 28     export default {
 29         data() {
 30             return {
 31                 latitude: 0,
 32                 longitude: 0,
 33                 covers: [{
 34                     id: 1,
 35                     width: 50,
 36                     height: 50,
 37                     latitude: 0,
 38                     longitude: 0,
 39                     iconPath: '../../static/1.gif'
 40                 }, ],
 41                 city:'',
 42                 flag:false
 43             }
 44         },
 45         onReady() {
 46             this.mapDom = uni.createMapContext('mapDom')
 47             console.log(423,this.mapDom);
 48         },
 49         onl oad(option) {
 50             this.covers[0].latitude = Number(option.lat)
 51             this.covers[0].longitude = Number(option.lng)
 52             this.city = option.city
 53             //  uni.getLocation 不触发直接将基础库调整为 2.17.0
 54             uni.getLocation({
 55                 type: 'gcj02',
 56                 success: (res) => {
 57                     console.log('当前位置的经度:' + res.longitude);
 58                     console.log('当前位置的纬度:' + res.latitude);
 59                     this.latitude = Number(res.latitude)
 60                     this.longitude = Number(res.longitude)
 61                     // 获取位置成功之后进行缩放,直观的看到坐标
 62                     // includePoints 缩放视野展示所有经纬度
 63                     this.mapDom.includePoints({
 64                         padding:[140,50,140,50],
 65                         points:[
 66                             //第一个是我的位置
 67                             {
 68                                 latitude:this.latitude,
 69                                 longitude:this.longitude
 70                             },
 71                             // 第二个是店铺的位置
 72                             {
 73                                 latitude:this.covers[0].latitude,
 74                                 longitude:this.covers[0].longitude
 75                             }
 76                         ]
 77                     })
 78                 }
 79             });
 80         },
 81         methods:{
 82             // 地图初始化完成后触发
 83             lodingMap() {
 84                 console.log(423);
 85                 this.flag = true
 86             },
 87             getHereHandle() {
 88                 if(!this.flag) return
 89                 // let plugin = requirePlugin('routePlan');
 90                 // let key = '2J4BZ-3CFEV-OYSPG-UDFGU-ONNNS-63FOT';  //使用在腾讯位置服务申请的key
 91                 // let referer = 'navigateMap';   //调用插件的app的名称
 92                 // let endPoint = JSON.stringify({  //终点
 93                 //   'name': '北京西站',
 94                 //   'latitude': 39.894806,
 95                 //   'longitude': 116.321592
 96                 // });
 97                 // wx.navigateTo({
 98                 //   url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint
 99                 // });
100                 uni.openLocation({
101                       latitude:this.covers[0].latitude,    //维度
102                       longitude: this.covers[0].longitude, //经度
103                       name: this.city,    //目的地定位名称
104                       scale: 15,    //缩放比例
105                       // address: "河南省鹤壁市浚县"    //导航详细地址
106                     })
107         
108             },
109             backPositionHandle() {
110                 if(!this.flag) return
111                 this.mapDom.moveToLocation();
112             }
113         }
114     }
115 </script>
116  
117 <style>
118     .page-section-gap {
119         position: relative;
120     }
121     .footer{
122         width: 100%;
123         height: 25vh;
124         background-color: #fff;
125         box-sizing: border-box;
126         padding: 20upx  40upx 0 40upx;
127         display: flex;
128         flex-direction: column;
129         border-radius: 22upx;
130     }
131     .position{
132         height: 90upx;
133         line-height:90upx;
134         background-color: #3d89f0;
135         color: #fff;
136         width: 100%;
137     }
138     .descriptive{
139         font-size: 30upx;
140         margin: 30upx 0 30upx 0;
141     }
142     .positionIcon {
143         width: 40upx;
144         height: 40upx;
145         border-radius: 50%;
146         position: absolute;
147         bottom: 40upx;
148         right: 40upx;
149         background-image: url('https://tse2-mm.cn.bing.net/th/id/OIP-C.fCPxTGJccQvVdqrNFqrMRwHaHa?pid=ImgDet&rs=1');
150         background-size: 100% 100%;
151     }
152 </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        
10         let callNow = !timer
11         timer = setTimeout(() => {
12             timer = null
13         }, delay)
14         if (callNow) fn.apply(this, args)
15     }
16 }

 

标签:city,uniapp,longitude,定位导航,key,腾讯,latitude,uni,width
From: https://www.cnblogs.com/gzy2016Blog/p/17859010.html

相关文章

  • 腾讯云创建镜像
    title:没有手都可以在腾讯云创建镜像banner_img:https://cloud.studyinglover.com/api/raw/?path=/photos/blog/33a1d238f41bcd6994390b5a52067cd6.pngdate:2023-6-1621:15:00categories:-踩坑腾讯云是国内顶级的云服务商。在大型项目上环境配置和编译是很多人的噩梦,当......
  • 100元预算,轻松涨粉1000!腾讯运营面试秘籍大揭秘!
    大家好啊!小米在这里~很高兴又有机会和大家见面啦!最近小米参加了一场腾讯的运营面试,遇到了一个超有趣的问题:如果让你运营一个公众号,近期需要增加1000个关注,预算100元,怎么完成这个目标?说实话,小米当时差点就跟面试官热聊了起来,因为这个问题太过火辣刺激了!好了,不多扯了,让我们一起揭开这......
  • Java互联网+医院智能导诊系统源码 自动兼容H5小程序、Uniapp
    智能导诊系统是一种基于人工智能技术的医疗辅助系统,旨在帮助患者快速找到合适的就诊路径。患者可以通过系统了解医院的科室设置、医生排班、就诊流程等信息,并根据自己的症状和描述,系统会推荐合适的科室和医生。同时,智能导诊系统还可以为医院提供数据分析和决策支持,提高医院的管理效......
  • uniapp base64与file互转
    uniappbase64与file互转//base64转flie//base64转fliebase64ToFile(base64data,cb){ constfsm=uni.getFileSystemManager(); constFILE_BASE_NAME='tmp_base64src';//自定义文件名 const[,format,bodyData]=/data:image\/(\w+);base64,(.*)/.exec......
  • 基于EVA估价模型的互联网企业价值评估方法——以“腾讯公司”为例
    摘要:从20世纪90年代以来,互联网行业在中国迅速发展。互联网企业是一种结合了网络经济效应和轻资产经营特点的新型企业组织形式。与传统企业相比,它在生产经营、创造价值方式和评估反映的问题上都各不相同。目前,EVA被广泛应用于企业价值评估,主要以经济利润为核心,考虑股权资本成本和权......
  • uniapp IOS从打包到上架流程(详细简单) 原创
    ​uniappIOS从打包到上架流程(详细简单)原创1.登入苹果开发者网站,打开AppStoreConnect   ​ 2.新App的创建点击我的App可以进入App管理界面,在右上角点击➕新建App即可创建新的App,如下图: ​ 3.app基本信息填写新建完App后,需要填写App的基本信息,比如App的......
  • uniapp 封装一个类似js-cookie可时效性存储token的方法
    贴代码cache.js/***存储数据*key:缓存的键名,必填*value:缓存的值,选填*seconds:缓存的过期时间,选填,单位为秒,默认为28天*/functionset(key,value,seconds){if(!key){//如果key为空,直接返回console.log("key不能空");return;}const......
  • uniapp打包的ipa上架到appstore的傻瓜式教程
    ​转载:uniapp打包的ipa上架到appstore的傻瓜式教程uniapp打包在HBuilderX编辑器中打开需要打包的项目,然后点击上面菜单栏中发行=>原生App-云打包,对以下弹出的弹窗进行内容填写  ​  填写完成以后,点击打包操作  ​    ​  点击继续打包,等待......
  • uniapp-微信小程序绑定动态样式 :style 避坑
    在uniapp中绑定动态样式:style="object"使用此种方法,在H5页面中并不会出现任何问题而在微信小程序中,此种方式就会被编译成 style="[object,object]"从而导致样式无法生效解决方法:    使用:style="[object]"此种方式即可......
  • 开源在线客服系统源码PHP(H5网页在线客服系统小程序源码uniapp全套搭建)
    现代客户服务的重要性得到了越来越多的认可。一个优质的客户服务可以使客户在购买和使用产品、寻求技术支持时获得更好的体验,从而建立起品牌声誉和客户忠诚度。为了优化客户服务体验,许多企业已经开始使用客服系统来更好地管理、响应和交互客户需求。源码:kf.zxkfym.top......