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>

效果

进去后看到的效果

点击选择位置后

点击导航后

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

相关推荐
样子20185 小时前
Uniapp 之renderjs解决swiper+多个video卡顿问题
前端·javascript·css·uni-app·html
fakaifa7 小时前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
Bug改不动了10 小时前
React Native 与 UniApp 对比
react native·react.js·uni-app
anyup12 小时前
🔥🔥 10 天 Star 破百!uView Pro 文档也开源啦:完全免费、无广告、高效上手
前端·vue.js·uni-app
fakaifa1 天前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
2501_915918411 天前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
梦里寻码1 天前
自行食用 uniapp 多端 手写签名组件
前端·uni-app
不如摸鱼去2 天前
Trae 辅助下的 uni-app 跨端小程序工程化开发实践分享
微信小程序·小程序·uni-app·aigc·ai编程
小白_ysf3 天前
uniapp 开发微信小程序,获取经纬度并且转化详细地址(单独封装版本)
微信小程序·uni-app
iOS阿玮3 天前
三年期已满,你的产品不再更新将于90天后下架。
uni-app·app·apple