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

2023-10-25 05:31

本文主要是介绍uniapp接入腾讯地图实现定位导航功能。,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

打开腾讯地图的官网注册账号登陆进入,滑入我的头像开发者信息: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

首页代码

<template><view class=""><div class="header"><view class="content">你要去哪里:<view class="text-area"><text @click="toMap()" class="title">{{title}}</text></view></view><input @input="changeCity" v-model="city" type="text" height="50" /><ul v-show="isShowSearch"><li @click="getCityHandle(item.address)" v-for="item in suggestionData" :key="item.id">{{item.address}}</li></ul></div></view></template><script>import {Debounce} from '../../utils/debounce.js'export default {data() {return {title: '出发',key: '2J4BZ-3CFEV-OYSPG-UDFGU-ONNNS-63FOT',city: '河南省鹤壁市浚县',locationConfig: null,noClick: true,suggestionData: null,isShowSearch:false}},onLoad() {},methods: {toMap() {this.getPosition((location) => {uni.navigateTo({url: `/pages/map/index?lat=${ location.lat}&lng=${ location.lng}&city=${this.city}`})})},getPosition(callback) {uni.request({url: `https://apis.map.qq.com/ws/geocoder/v1/?address=${this.city}&key=${this.key}`,success: ({data}) => {callback(data.result.location)},})},getCityHandle(val) {this.city = valthis.isShowSearch = false},changeCity: Debounce(function(e) {this.isShowSearch = trueconsole.log(432);this.city = e.target.valueuni.request({url: `https://apis.map.qq.com/ws/place/v1/suggestion?key=${this.key}&keyword=${this.city}`,success: ({data}) => {console.log(423, data.data);this.suggestionData = data.data},})}, 1000)}}
</script><style>ul {height: auto;width: 90%;overflow: scroll;}ul li {margin: 2px;width: 100%;height: auto;border-radius: 10upx;background-color: #ccc;padding: 10upx;box-sizing: border-box;}.content {display: flex;margin-bottom: 20upx;}.logo {height: 200rpx;width: 200rpx;margin-top: 200rpx;margin-left: auto;margin-right: auto;margin-bottom: 50rpx;}.text-area {display: flex;justify-content: center;border: 1px solid #ccc;width: 30%;border-radius: 10upx;}.title {font-size: 36rpx;color: #8f8f94;}.header {width: 100%;height: 30upx;padding: 30upx;}.header input {width: 90%;border: 1px solid #ccc;border-radius: 10upx;}
</style>

地图页面

<template><view class=""><view class="page-body"><view class="page-section page-section-gap"><map style="width: 100%; height: 75vh;" id="mapDom"show-location :latitude="latitude" :longitude="longitude" :markers="covers"show-location@updated="lodingMap"></map><div @click="backPositionHandle" class="positionIcon"></div></view></view><view class="footer"><h3>你的目的地</h3><view class="descriptive">{{city}}</view><button class="position" @click="getHereHandle">到这里</button></view></view></template><script>export default {data() {return {latitude: 0,longitude: 0,covers: [{id: 1,width: 50,height: 50,latitude: 0,longitude: 0,iconPath: '../../static/1.gif'}, ],city:'',flag:false}},onReady() {this.mapDom = uni.createMapContext('mapDom')console.log(423,this.mapDom);},onLoad(option) {this.covers[0].latitude = Number(option.lat)this.covers[0].longitude = Number(option.lng)this.city = option.city//  uni.getLocation 不触发直接将基础库调整为 2.17.0uni.getLocation({type: 'gcj02',success: (res) => {console.log('当前位置的经度:' + res.longitude);console.log('当前位置的纬度:' + res.latitude);this.latitude = Number(res.latitude)this.longitude = Number(res.longitude)// 获取位置成功之后进行缩放,直观的看到坐标// includePoints 缩放视野展示所有经纬度this.mapDom.includePoints({padding:[140,50,140,50],points:[//第一个是我的位置{latitude:this.latitude,longitude:this.longitude},// 第二个是店铺的位置{latitude:this.covers[0].latitude,longitude:this.covers[0].longitude}]})}});},methods:{// 地图初始化完成后触发lodingMap() {console.log(423);this.flag = true},getHereHandle() {if(!this.flag) return// let plugin = requirePlugin('routePlan');// let key = '2J4BZ-3CFEV-OYSPG-UDFGU-ONNNS-63FOT';  //使用在腾讯位置服务申请的key// let referer = 'navigateMap';   //调用插件的app的名称// let endPoint = JSON.stringify({  //终点//   'name': '北京西站',//   'latitude': 39.894806,//   'longitude': 116.321592// });// wx.navigateTo({//   url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint// });uni.openLocation({latitude:this.covers[0].latitude,    //维度longitude: this.covers[0].longitude, //经度name: this.city,    //目的地定位名称scale: 15,    //缩放比例// address: "河南省鹤壁市浚县"    //导航详细地址})},backPositionHandle() {if(!this.flag) returnthis.mapDom.moveToLocation();}}}
</script><style>.page-section-gap {position: relative;}.footer{width: 100%;height: 25vh;background-color: #fff;box-sizing: border-box;padding: 20upx  40upx 0 40upx;display: flex;flex-direction: column;border-radius: 22upx;}.position{height: 90upx;line-height:90upx;background-color: #3d89f0;color: #fff;width: 100%;}.descriptive{font-size: 30upx;margin: 30upx 0 30upx 0;}.positionIcon {width: 40upx;height: 40upx;border-radius: 50%;position: absolute;bottom: 40upx;right: 40upx;background-image: url('https://tse2-mm.cn.bing.net/th/id/OIP-C.fCPxTGJccQvVdqrNFqrMRwHaHa?pid=ImgDet&rs=1');background-size: 100% 100%;}
</style>

/utils/debounce.js 防抖方法

export  const Debounce = (fn, wait) => {let delay = wait|| 500let timerreturn function () {let args = arguments;if (timer) {clearTimeout(timer)}let callNow = !timertimer = setTimeout(() => {timer = null}, delay)if (callNow) fn.apply(this, args)}
}

这篇关于uniapp接入腾讯地图实现定位导航功能。的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/280339

相关文章

MySQL 8 中的一个强大功能 JSON_TABLE示例详解

《MySQL8中的一个强大功能JSON_TABLE示例详解》JSON_TABLE是MySQL8中引入的一个强大功能,它允许用户将JSON数据转换为关系表格式,从而可以更方便地在SQL查询中处理J... 目录基本语法示例示例查询解释应用场景不适用场景1. ‌jsON 数据结构过于复杂或动态变化‌2. ‌性能要

Python实现终端清屏的几种方式详解

《Python实现终端清屏的几种方式详解》在使用Python进行终端交互式编程时,我们经常需要清空当前终端屏幕的内容,本文为大家整理了几种常见的实现方法,有需要的小伙伴可以参考下... 目录方法一:使用 `os` 模块调用系统命令方法二:使用 `subprocess` 模块执行命令方法三:打印多个换行符模拟

SpringBoot+EasyPOI轻松实现Excel和Word导出PDF

《SpringBoot+EasyPOI轻松实现Excel和Word导出PDF》在企业级开发中,将Excel和Word文档导出为PDF是常见需求,本文将结合​​EasyPOI和​​Aspose系列工具实... 目录一、环境准备与依赖配置1.1 方案选型1.2 依赖配置(商业库方案)二、Excel 导出 PDF

Python实现MQTT通信的示例代码

《Python实现MQTT通信的示例代码》本文主要介绍了Python实现MQTT通信的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 安装paho-mqtt库‌2. 搭建MQTT代理服务器(Broker)‌‌3. pytho

使用zip4j实现Java中的ZIP文件加密压缩的操作方法

《使用zip4j实现Java中的ZIP文件加密压缩的操作方法》本文介绍如何通过Maven集成zip4j1.3.2库创建带密码保护的ZIP文件,涵盖依赖配置、代码示例及加密原理,确保数据安全性,感兴趣的... 目录1. zip4j库介绍和版本1.1 zip4j库概述1.2 zip4j的版本演变1.3 zip4

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

Linux下删除乱码文件和目录的实现方式

《Linux下删除乱码文件和目录的实现方式》:本文主要介绍Linux下删除乱码文件和目录的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录linux下删除乱码文件和目录方法1方法2总结Linux下删除乱码文件和目录方法1使用ls -i命令找到文件或目录