当前位置: 首页 > 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.dtcms.com/a/291740.html

相关文章:

  • Taro 网络 API 详解与实用案例
  • JavaScript AJAX 实现,演示如何将 Token 添加到 Authorization
  • 异地服务器备份Mysql数据
  • 电子电气架构 --- 从软件质量看组织转型路径
  • 基于Patroni实现PostgreSQL数据库高可用
  • postgresql使用记录 SCRAM authentication requires libpq version 10 or above
  • Nginx防盗链和Keepalived
  • VirtualBox安装提示security安全问题
  • 【coze扣子】第1篇:coze快速入门
  • 消息队列学习
  • 3.4 安全-分布式-数据库-挖掘
  • LNMP平台部署
  • 【uboot/kernel1】启动流程,环境变量,内存,initramfs
  • 【大模型记忆实战Demo】基于SpringAIAlibaba通过内存和Redis两种方式实现多轮记忆对话
  • 本地代理和服务器代理区别
  • 【AI时代速通QT】第五节:Qt Creator如何引入第三方库,以OpenCV为例
  • 深入解析MIPI C-PHY (三)C-PHY 功耗屠龙刀
  • FunASR 说话人识别 Xvector 环境版本配置
  • 一文读懂深度模型优化器,掌握炼丹工具
  • 【数学建模】基础知识
  • FTP考点
  • SparkSQL 聚合函数 COUNT 对 NULL 值的处理
  • css3地球转动模型(动态数据)
  • RxSwift 核心解析
  • 生成式AI導論 2024】第9講:以大型語言模型打造的AI Agent 学习记录
  • Nacos 注册中心高频面试题及解析
  • v0+claude+cursor构建初始脚手架
  • ADA4522-2ARMZ-R7 ADI亚德诺 双通道零漂移运算放大器 工业高精度测试设备应用
  • 您需要了解的有关 GIS 中基于位置和基于属性的查询的所有信息
  • [pdf epub]《软件方法》电子书202507更新下载