uniapp实现位置功能:

<uni-forms-item label="所在地区:" name="area" label-align="center" color="#D7D7D7">
	<view @click="openPicker()">
		<input type="text" v-model="valiFormData.region" placeholder="请选择所在城市" style="border:1px solid #d7d7d7;padding: 5px;border-radius:3px;">
	</view>
</uni-forms-item>
<uni-forms-item label="详细地址:" name="address" label-align="center" color="#D7D7D7" style="position: relative;">
	<!-- uni-easyinput组件 -->
	<uni-easyinput @click="chooseLocation()" v-model="valiFormData.address" placeholder="请输入详细地址" />
	<view style="position: absolute;right: 0;top:5px;" @click="chooseLocation()">
		<uni-icons type="location " size="25"></uni-icons>
	</view>
</uni-forms-item>

<!-- 地区选择组件 -->
<lotus-address v-on:choseVal="choseValue" :lotusAddressData="lotusAddressData"></lotus-address> 

import lotusAddress from "@/components/Winglau14-lotusAddress/Winglau14-lotusAddress.vue";
export default {
	components: {
		lotusAddress,
	},
	data() {
		return {
			valiFormData: {
				region: '', //所在地区
				address: '', //详细地址
			},
			hasLocation: false,
			lotusAddressData: { //省市 区
				visible: false,
				provinceName: '',
				cityName: '',
				townName: '',
			},
		}
	},
	methods: {
		//打开地区选择框
		openPicker() {
			this.lotusAddressData.visible = true;
		},
		//选择省市区
		choseValue(res) {
			this.lotusAddressData.visible = res.visible; //visible为显示与关闭组件标识true显示false隐藏
			//res.isChose = 1省市区已选 res.isChose = 0;未选
			if (res.isChose) {
				this.lotusAddressData.provinceName = res.province; //省
				this.lotusAddressData.cityName = res.city; //市
				this.lotusAddressData.townName = res.town; //区
				this.valiFormData.region = res.province + res.city + res.town;
			}
		},
		//选择具体位置
		chooseLocation: function() {
			const that = this;
			wx.getLocation({
				type: 'gcj02', // 'wgs84', // 默认为wgs84的 gps 坐标,如果要返回直接给 openLocation 用的火星坐标,可传入'gcj02'
				success: function(res) {
					var latitudeer = res.latitude; // 纬度,浮点数,范围为90 ~ -90
					var longitudeer = res.longitude; // 经度,浮点数,范围为180 ~ -180。
					// var speed = res.speed; // 速度,以米/每秒计
					// var accuracy = res.accuracy; // 位置精度

					uni.chooseLocation({
						latitude: latitudeer, //Number	否	目标地纬度	微信小程序(2.9.0+)、H5-Vue3(3.2.10+)
						longitude: longitudeer, //Number	否	目标地经度
						success: (res2) => {
							that.valiFormData.latitude = res2.latitude;
							that.valiFormData.longitude = res2.longitude;
							that.hasLocation = true;
							that.valiFormData.address = res2.address;
						}
					})
				}
			});
		},
		clear: function() {
			this.hasLocation = false
		},
	}
}

地区选择组件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Sun Peng

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

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

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

打赏作者

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

抵扣说明:

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

余额充值