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>

效果

进去后看到的效果

点击选择位置后

点击导航后

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

相关推荐
2501_9159090614 小时前
HTTPS 错误解析,常见 HTTPS 抓包失败、443 端口错误与 iOS 抓包调试全攻略
android·网络协议·ios·小程序·https·uni-app·iphone
源码师傅18 小时前
uniapp开源多商户小程序商城平台源码 支持二次开发+永久免费升级
小程序·uni-app·多商户商城源码·uniapp开源商城源码·开源多商户小程序商城平台·商城小程序代码·多商户商城小程序源码
梦远青城19 小时前
C#地方门户网站 基于NET6.0、Admin.NET,uniapp,vue3,elementplus开源的地方门户网站项目
uni-app·开源·门户网站·地方生活网站·本地租房·本地找工作·东川本地生活
2501_915909061 天前
苹果上架App软件全流程指南:iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传与审核技巧详解
android·ios·小程序·https·uni-app·iphone·webview
2501_915921431 天前
iOS 文件管理与能耗调试结合实战 如何查看缓存文件、优化电池消耗、分析App使用记录(uni-app开发与性能优化必备指南)
android·ios·缓存·小程序·uni-app·iphone·webview
小徐_23331 天前
uni-app 也能使用 App.vue?wot-starter 是这样实现的!
前端·uni-app
2501_915918411 天前
App 苹果 上架全流程解析 iOS 应用发布步骤、App Store 上架流程
android·ios·小程序·https·uni-app·iphone·webview
哆啦A梦15881 天前
uniapp分包实现
前端·vue.js·uni-app·vue3
2501_916007471 天前
苹果上架全流程详解,iOS 应用发布步骤、App Store 上架流程、uni-app 打包上传与审核要点完整指南
android·ios·小程序·https·uni-app·iphone·webview
jingling5551 天前
uniapp | 实现微信小程序端的分包处理
微信小程序·小程序·uni-app