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>

效果

进去后看到的效果

点击选择位置后

点击导航后

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

相关推荐
平凡シンプル1 小时前
安卓 uniapp跨端开发
android·uni-app
艾小逗3 小时前
uniapp快速入门教程,内容来源于官方文档,仅仅记录快速入门需要了解到的知识点
小程序·uni-app·app·es6
2401_8459375313 小时前
PHP一键约课高效健身智能健身管理系统小程序源码
微信·微信小程序·小程序·微信公众平台·微信开放平台
CHEtuzki14 小时前
微信这样回复客户高效又方便
微信·自动回复
鸭子嘎鹅子呱17 小时前
uniapp使用高德地图设置marker标记点,后续根据接口数据改变某个marker标记点,动态更新
uni-app·map·高德地图
计算机源码社1 天前
分享一个基于微信小程序的居家养老服务小程序 养老服务预约安卓app uniapp(源码、调试、LW、开题、PPT)
android·微信小程序·uni-app·毕业设计项目·毕业设计源码·计算机课程设计·计算机毕业设计开题
Angus-zoe1 天前
uniapp+vue+微信小程序实现侧边导航
vue.js·微信小程序·uni-app
Pluto & Ethereal1 天前
uni-app尺寸单位、flex布局于背景图片
uni-app
天空下sky1 天前
uniapp+若依 开发租房小程序源码分享
uni-app
帅过二硕ฅ1 天前
uniapp点击跳转到对应位置
前端·javascript·uni-app