Uni-app 调用微信地图导航功能【有图】

前言

我们在使用uni-app时,有时候会遇到需要开发地图和导航的功能,这些方法其实微信小程序的API已经帮我们封装好了
详见微信小程序开发文档
接下来我们就演示如何用uni-app来使用他们

使用

javascript 复制代码
<template>
	<view>
		<button type="primary" @click="chooseLocation" class="acc">选择位置 </button>
		经度:<span>{{longitude==' ' || longitude==null ?'/':longitude}}</span>
		纬度:<span>{{latitude==' ' || latitude==null ?'/':latitude}}</span>
		<button type="default" @click="nav">导航 </button>
	</view>
</template>

<script setup>
	import { getCurrentInstance, ref } from "vue";
	import { onBackPress } from "@dcloudio/uni-app";
	const { proxy } = getCurrentInstance();

	const name = ref(null)
	const address = ref(null)
	const latitude = ref(null)
	const longitude = ref(null)


	function nav() {
		uni.openLocation({
			latitude: latitude.value,
			longitude: longitude.value,
			name: name.value,
			scale: 15,
			address: address.value,
			success: function(res) {
				proxy.$modal.msgSuccess('打开地图成功!', res);
			},
			fail: function(error) {
				proxy.$modal.msgError('打开地图失败!', error);
			},
		});
	}


	function chooseLocation() {
		uni.chooseLocation({
			success: function(res) {
				name.value = res.name; // 位置名称
				address.value = res.address; // 详细地址
				latitude.value = res.latitude; // 纬度
				longitude.value = res.longitude; // 经度
				console.log('name', name.value);
				console.log('address', address.value);
				console.log('latitude', latitude.value);
				console.log('longitude', longitude.value);
			},
			fail: function(error) {
				console.error('选择位置失败', error);
			},
		});
	}
	// 获取当前位置的经纬度
	uni.getLocation({
		success: function(res) {
			var latitude = res.latitude; // 纬度
			var longitude = res.longitude; // 经度
			console.log('latitude', latitude);
			console.log('longitude', longitude);
			// 在这里可以将经纬度转换为地址信息,后续步骤中会介绍如何实现这一功能。
		},
		fail: function(error) {
			console.error('获取地理位置失败', error);
		},
	});

	// 页面加载
	onBackPress(() => {
		proxy.$tab.switchTab('/pages/office/index');
		return true;
	});
</script>

<style>
	.acc {
		margin-bottom: 20px;
	}
</style>

效果

进去后看到的效果

点击选择位置后

点击导航后

点击右下角的位置后,调用地图功能,会根据手机下载的软件来显示
如果手机内没有下载软件,会有腾讯地图和谷歌地图选项,点击会跳转到下载界面

相关推荐
SohongAI智慧办公7 小时前
广州AI办公软件哪家可以落地
其他·微信·微信公众平台
微擎应用市场12 小时前
多功能 SEO 企业官网系统:助力企业数字化营销高效落地
微信
微擎应用市场12 小时前
百姓微官网小程序系统 —— 企业数字化转型的轻量化解决方案
微信
微擎应用市场12 小时前
高效财务对账新选择:用户资金统计小程序系统深度解析
微信
微擎应用市场12 小时前
大展宏兔公众号应用:春节专属吸粉引流神器,赋能商家高效增长
微信
微擎应用市场12 小时前
智能洗衣柜小程序系统:一站式智能储物服务解决方案
微信
2501_9160074712 小时前
苹果手机iOS应用管理全指南与隐藏功能详解
android·ios·智能手机·小程序·uni-app·iphone·webview
陈思杰系统思考Jason15 小时前
美学+AI:企业竞争力新维度
百度·微信·微信公众平台·新浪微博·微信开放平台
2501_9151063215 小时前
全面理解 iOS 帧率,构建从渲染到系统行为的多工具协同流畅度分析体系
android·ios·小程序·https·uni-app·iphone·webview
2501_9160088917 小时前
iOS 能耗检测的工程化方法,构建多工具协同的电量分析与性能能效体系
android·ios·小程序·https·uni-app·iphone·webview