Uniapp 微信小程序直接打开地图App (不经过内置地图页)

说明

1. 该方法依赖 map组件,如果页面不需要展示地图可直接将地图隐藏 display:none; 即可。

2. 如需展示则赋值给 map组件 属性id即可。只是调用组件内的 openMapApp 方法,不影响组件本身。

3. 参数说明参考 MapContext.openMapApp

效果图

代码块

<template>
	<view class="content">
		<map id="myMap" style="display: none;"/>
		
		<view class="input-box">
			<text>经度:</text>
			<input type="number" v-model="longitude" />
		</view>
		
		<view class="input-box">
			<text>纬度:</text>
			<input type="number" v-model="latitude" />
		</view>
		
		<view class="input-box">
			<text>名字:</text>
			<input type="text" v-model="destination" />
		</view>
		
		<button type="default" @click="openMap">打开地图App</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				longitude:0,
				latitude:0,
				destination:'目的地目的地'
			}
		},
		mounted() {
			let _this = this;
			
			uni.getLocation({
				success(res) {
					_this.latitude = res.latitude;
					_this.longitude = res.longitude;
				}
			})
		},
		methods: {
			openMap(){
				let mapContext = uni.createMapContext("myMap");
				mapContext.openMapApp({
					latitude: Number(this.latitude),
					longitude: Number(this.longitude),
					destination: this.destination,
					success() {
						console.log('success!!');
					},
					fail() {
						console.log('fail!!');
					},
					complete() {
						console.log('complete!!');
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	
	.input-box{
		display: flex;
		align-items: center;
		input{
			flex: 1;
			display: inline-block;
			border: 1rpx solid rgba(0,0,0,.2);
			border-radius: 8rpx;
		}
		margin: 30rpx;
	}
	
</style>

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

惊悚的毛毛虫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值