uniapp 调用手机上安装的app (高德地图 百度地图 Apple地图 谷歌地图)

uniapp 调用手机上安装的app (高德地图 百度地图 Apple地图 谷歌地图)

效果

思路

  1. 获取手机类型(安卓/iOS)

    js 复制代码
    let platform = uni.getSystemInfoSync().platform
  2. 判断手机有没有安装需要的应用

    js 复制代码
    plus.runtime.isApplicationExist({action: ""}))
  3. 打开应用 跳转过去

    js 复制代码
    plus.runtime.openURL()

核心代码

html 复制代码
<view slot="value" class="u-slot-title" @click="goMap">
	<u--image :src="goMapImgSrc" width="18px" height="20px"></u--image>
</view>
js 复制代码
data() {
	return {
		latitude: 23.763780,
		longitude: -45.831800,
		shopAddress: "XXXXXXXXXXX",
		}
	}

goMap() {
	const _this = this
	if (!this.latitude || !this.longitude || !this.shopAddress) return
	// 判断系统安装的地图应用有哪些, 并生成菜单按钮
	let _mapName = [{
			title: this.$t('my.order.Gaode Maps'),
			name: 'amap',
			androidName: 'com.autonavi.minimap',
			iosName: 'iosamap://'
		},
		{
			title: this.$t('my.order.Baidu Maps'),
			name: 'baidumap',
			androidName: 'com.baidu.BaiduMap',
			iosName: 'baidumap://'
		}
		{
			title: this.$t('my.order.Google Maps'),
			name: 'googlemap',
			androidName: 'com.google.android.apps.maps',
			iosName: 'comgooglemaps://'
		},
		{
			title: this.$t('my.order.Apple Maps'),
			name: 'applemap',
			androidName: 'com.apple.Maps',
			iosName: 'maps://'
		},
	]
	// 根据真机安装的地图软件 生成操作菜单
	let buttons = []
	let platform = uni.getSystemInfoSync().platform
	platform === 'android' && _mapName.forEach(item => {
		if (plus.runtime.isApplicationExist({
				pname: item.androidName
			})) {
			buttons.push(item)
		}
	})
	platform === 'ios' && _mapName.forEach(item => {
		console.log(item)
		if (plus.runtime.isApplicationExist({
				action: item.iosName
			})) {
			buttons.push(item)
		}
	})
	if (buttons.length) {
		plus.nativeUI.actionSheet({ //选择菜单
			title: this.$t('my.order.select'),
			cancel: this.$t('pages.predetermine.predetermine.Cancel'),
			buttons: buttons
		}, function(e) {
			let _map = buttons[e.index - 1]
			_this.openURL(_map, platform)
		})
	} else {
		uni.showToast({
			title: '请安装地图软件',
			icon: 'none'
		})
		return
	}
},
// 打开第三方应用
openURL(map, platform) {
	console.log(map, platform);
	let _defaultUrl = {
		android: {
			"amap": `amapuri://route/plan/?sid=&did=&dlat=${this.latitude}&dlon=${this.longitude}&dname=${this.shopAddress}&dev=0&t=0`,
			'baidumap': `baidumap://map/direction?origin=${this.latitude},${this.longitude}&destination=name:${this.shopAddress}|latlng:${this.latitude},${this.longitude}&coord_type=wgs84&mode=driving&src=andr.baidu.openAPIdemo"`,
			'googlemap': `geo: + ${this.latitude} + ',' + ${this.longitude} + '?q=' + encodeURIComponent(${this.shopAddress})`
		},
		ios: {
			"amap": `iosamap://path?sourceApplication=fuxishan_uni_client&dlat=${this.latitude}&dlon=${this.longitude}&dname=${this.shopAddress}&dev=0&t=0`,
			'baidumap': `baidumap://map/direction?origin=${this.latitude},${this.longitude}&destination=name:${this.shopAddress}|latlng:${this.latitude},${this.longitude}&mode=driving&src=ios.baidu.openAPIdemo`,
			'googlemap': `comgooglemaps://?q=${this.shopAddress}`,
			'applemap': `maps://?q=${this.shopAddress}&sll=${this.latitude}+ ',' + ${this.longitude}&z=10&t=s`
		}
	}
	let newurl = encodeURI(_defaultUrl[platform][map.name]);
	plus.runtime.openURL(newurl, function(res) {
		uni.showModal({
			content: res.message
		})
	}, map.androidName ? map.androidName : '');
},

重点

要是需要打开谷歌地图的话, 需要在manifest.json中配置应用访问白名单

uniapp为了方便开发者调用一些常用的第三方应用,云端打包时默认已经一部分白名单但不包含谷歌地图,所以需要单独添加一下

相关推荐
Q_Q5110082859 小时前
python+uniapp基于微信小程序的旅游信息系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
2501_9160074711 小时前
iOS 混淆工具链实战,多工具组合完成 IPA 混淆与加固(iOS混淆|IPA加固|无源码混淆|App 防反编译)
android·ios·小程序·https·uni-app·iphone·webview
游戏开发爱好者813 小时前
FTP 抓包分析实战,命令、被动主动模式要点、FTPS 与 SFTP 区别及真机取证流程
运维·服务器·网络·ios·小程序·uni-app·iphone
2501_9159090615 小时前
iOS 26 文件管理实战,多工具组合下的 App 数据访问与系统日志调试方案
android·ios·小程序·https·uni-app·iphone·webview
盛夏绽放16 小时前
uni-app Vue 项目的规范目录结构全解
前端·vue.js·uni-app
2501_915918411 天前
掌握 iOS 26 App 运行状况,多工具协作下的监控策略
android·ios·小程序·https·uni-app·iphone·webview
知识分享小能手1 天前
uni-app 入门学习教程,从入门到精通,uni-app基础扩展 —— 详细知识点与案例(3)
vue.js·学习·ui·微信小程序·小程序·uni-app·编程
2501_915909061 天前
iOS 混淆实战,多工具组合完成 IPA 混淆与加固(源码 + 成品 + 运维一体化方案)
android·运维·ios·小程序·uni-app·iphone·webview
赵庆明老师1 天前
Uniapp微信小程序开发:EF Core 中级联删除
uni-app
Javashop_jjj1 天前
三勾软件| 用SpringBoot+Element-UI+UniApp+Redis+MySQL打造的点餐连锁系统
spring boot·ui·uni-app