当前位置: 首页 > news >正文

uniapp各大平台导航组件

最近有个需求要点击导航然后跳出各家导航软件

话不多出直接贴出代码:这个可以作为组件引入

<template><view><view class="nav" :style="{color: customColor}" @click.stop="openMap">{{title}}</view><!-- 弹窗仅在 H5 / App 显示 --><uni-popup ref="popupRef" type="bottom"><view class="popup-content"><view class="option" @click.stop="selectMap('amap')">高德地图</view><view class="option" @click.stop="selectMap('baidu')">百度地图</view><view class="option" @click.stop="selectMap('tencent')">腾讯地图</view><view class="cancel" @click.stop="popupRef.close()">取消</view></view></uni-popup></view>
</template><script setup>import {ref,computed} from 'vue';import {onReady} from '@dcloudio/uni-app';const props = defineProps({lat: {type: [Number, String],required: true},lng: {type: [Number, String],required: true},name: {type: String,default: '目标位置'},myLat: {type: [Number, String],required: true},myLng: {type: [Number, String],required: true},title: {type: String,default: '导航'},customColor: {tyep: String,default: 'red'}});const popupRef = ref(null);// const isIOS = computed(() => uni.getSystemInfoSync().platform === 'ios');// 判断平台const isWeChatMiniProgram = uni.getSystemInfoSync().uniPlatform === 'mp-weixin';// 打开地图弹窗或直接调用微信小程序地图function openMap() {if (isWeChatMiniProgram) {console.log("微信小程序打开地图", props.lat, props.lng, props.name);wx.openLocation({latitude: Number(props.lat),longitude: Number(props.lng),name: props.name,scale: 18,success: () => {},fail: (err) => {console.log("打开地图失败", err);}});} else {popupRef.value.open();}}// 选择地图跳转(H5 / App)function selectMap(type) {const {lat,lng,name} = props;const encodedName = encodeURIComponent(name);let url = '';const platform = uni.getSystemInfoSync().platform;const isIOS = platform === 'ios';// 判断是否为微信浏览器// const ua = window.navigator.userAgent.toLowerCase();// const isWeChatBrowser = ua.indexOf('micromessenger') !== -1;// if (isWeChatBrowser) {// 微信浏览器:使用网页地图跳转// #ifdef H5 switch (type) {case 'amap':url =`https://uri.amap.com/navigation?to=${lng},${lat},${encodedName}&mode=car&policy=1&src=webapp&coordinate=gaode`;break;case 'baidu':url =`https://api.map.baidu.com/direction?origin=latlng:${props.myLat},${props.myLng}|name:我的位置&destination=latlng:${lat},${lng}|name:${encodedName}&mode=driving&region=全国&output=html&src=yourCompanyName|yourAppName`;break;case 'tencent':url =`https://apis.map.qq.com/uri/v1/routeplan?type=drive&to=${encodedName}&tocoord=${lat},${lng}&policy=0&referer=yourAppName`;break;}console.log("跳转的URL:", url);popupRef.value.close();window.location.href = url;// #endif// } else {// #ifdef APP-PLUS//   //其它浏览器:唤醒APP - TOTO 由于H5无法判断手机是否安装对应APP,有对应APP则唤醒,无对应APP会被过滤掉let title = ''switch (type) {case 'amap':title = '高德地图'url = isIOS ?`iosamap://path?sourceApplication=app&dlat=${lat}&dlon=${lng}&dname=${encodedName}&dev=0&t=0` :`amapuri://route/plan/?dlat=${lat}&dlon=${lng}&dname=${encodedName}&dev=0&t=0`;break;case 'baidu':title = '百度地图'url =`baidumap://map/direction?origin=我的位置&destination=latlng:${lat},${lng}|name:${encodedName}&mode=driving&coord_type=gcj02`;break;case 'tencent':title = '腾讯地图'url = `qqmap://map/routeplan?type=drive&tocoord=${lat},${lng}&to=${encodedName}&coord_type=1`;break;}plus.runtime.openURL(url, function(res) {console.log("打开地图APP失败:", res);uni.showToast({title: "导航失败,请下载" + title,icon: 'none'})});// }// #endif}
</script><style scoped>.nav {text-align: center;color: red;font-size: 26upx;}.popup-content {background: #fff;padding: 20rpx;}.option {padding: 30rpx;text-align: center;border-bottom: 1px solid #eee;}.cancel {padding: 30rpx;text-align: center;color: #999;}
</style>

使用:

<MapNavigator :lat="" :lng="" :myLat="" :myLng="" :name="" />
// lat, lng 要去的经纬度   myLat myLng name 当前身处位置

http://www.xdnf.cn/news/1167049.html

相关文章:

  • 在 Ubuntu 22.04 上安装并优化 Nginx nginx入门操作 稍难,需要有一定理论 多理解 多实践
  • 《Uniapp-Vue 3-TS 实战开发》自定义时间选择
  • Kafka基础理论速通
  • IDEA全局Maven配置
  • 比特币技术简史 第六章:网络协议 - P2P网络、节点类型与消息传播
  • 未来趋势:LeafletJS 与 Web3/AI 的融合
  • Visual Studio Code 远端云服务器开发使用指南
  • (3)重定向 | 时间相关指令 | 文件查找 | 打包与压缩
  • FastDFS 6.11.0 单机环境搭建与测试(附 Nginx 集成)+ docker构建+k8s启动文件
  • Linux异常与信号处理
  • 浙江大学PTA程序设计C语言基础编程练习题1-5
  • 【论文阅读 | TIV 2024 | CDC-YOLOFusion:利用跨尺度动态卷积融合实现可见光-红外目标检测】
  • 边缘计算网关赋能智慧农业:物联网边缘计算的创新应用与实践
  • 【每日算法】专题九_链表
  • python-FTP爆破脚本(phpstudy)-一点bug记录
  • C++性能优化擂台技术文章大纲
  • Unity笔记——事件中心
  • Web3介绍(Web 3.0)(一种基于区块链技术的去中心化互联网范式,旨在通过技术手段实现用户对数据的自主权、隐私保护和价值共享)
  • 算法第26天|贪心算法:用最少数量的箭引爆气球、无重叠区间、划分字母区间
  • solidity从入门到精通 第二章:Solidity初相见
  • AI 音频产品开发模板及流程(二)
  • 数据结构 堆(2)---堆的实现
  • Markdown 转 PDF API 数据接口
  • Android ViewModel 深度解析:原理、使用与最佳实践
  • Redis——Redis进阶命令集详解(下)
  • Docker Compose UI远程访问教程:结合贝锐花生壳实现内网穿透
  • Qt中QObject类的核心作用与使用
  • C++函数 vs Go函数
  • Qt基本控件使用:按钮、标签、文本框等
  • 【打怪升级 - 01】保姆级机器视觉入门指南:硬件选型 + CUDA/cuDNN/Miniconda/PyTorch 安装全流程(附版本匹配秘籍)