uniapp使用腾讯云获取位置转为省市区

腾讯云获取位置转为省市区

腾讯位置服务提供了多种SDK程序包,其中的JavaScript版本的SDK适用于微信小程序,所以我们下载这个SDK包。

下载地址

在小程序项目中,创建lib目录,把SDK文件放入其中

js 复制代码
<script>
	var QQMapWX = require('../../lib/qqmap-wx-jssdk.min.js');
	var qqmapsdk;
	export default {
		data() {
			return {
				addrlocal: null,
			}
		},
		methods: {
			myLocation() { //5LFBZ-5HV6L-SAIPY-E6BNI-O7FOJ-XNFAN 
				const self = this; // 保存Vue实例的引用
				console.log(self.addrlocal);
				//获取地理定位
				uni.getLocation({
					type: 'wgs84',
					success: function(resp) {
						let latitude = resp.latitude;
						let longitude = resp.longitude;
						qqmapsdk.reverseGeocoder({
							location: {
								latitude: latitude,
								longitude: longitude
							},
							success: function(resp) {
								// console.log(resp.result)
								let address = resp.result.address
								let addressComponent = resp.result.address_component
								let nation = addressComponent.nation;
								let province = addressComponent.province;
								let city = addressComponent.city;
								let district = addressComponent.district;
								console.log("address:" + address +
									"addressComponent:" + addressComponent +
									"nation:" + nation +
									"province:" + province +
									"city:" + city +
									"district:" + district)
								self.addrlocal = city + district; // 使用保存的self
								console.log(self.addrlocal);
								/**
								 * address:吉林省长春市南关区东岭南街3355号addressComponent:[object Object]nation:中国province:吉林省city:长春市district:南关区
								 */
							}
						})
					}
				})
			}
		},
		components: {
			"fuwu": fuwuItem
		},
		onLoad: function() {
			qqmapsdk = new QQMapWX({
				key: '5LFBZ-5HV6L-SAIPY-E6BNI-O7FOJ-XNFAN'
			});
			this.myLocation();
		}
	}
</script>
相关推荐
wb18912 小时前
LVS各种调度以及Haproxy调度重习
笔记·云计算·lvs·haproxy
hhzz13 小时前
阿里云的OpenAPI来操作云资源
阿里云·云计算·openapi
特立独行的猫a14 小时前
uniapp-x的HarmonyOS鸿蒙应用开发:tabbar底部导航栏的实现
华为·uni-app·harmonyos·鸿蒙·uniapp-x
迪巴拉152514 小时前
基于Yolov8训练的Flask后端和Uniapp野生菌识别系统
yolo·flask·uni-app
青衫码上行1 天前
【项目部署】Spring Boot项目部署的四种方式
java·linux·服务器·spring boot·后端·docker·腾讯云
500佰1 天前
Hive常见故障多案例FAQ宝典 --项目总结(宝典一)
大数据·linux·数据仓库·hive·hadoop·云计算·运维开发
峰顶听歌的鲸鱼2 天前
Kubernetes-Pod
linux·运维·云原生·容器·kubernetes·云计算
IvanCodes2 天前
十、Linux Shell脚本:流程控制语句
linux·云计算
qq_316837753 天前
caddy 使用阿里云的域名通过dns验证申请ssl证书
阿里云·云计算·ssl
IvanCodes3 天前
七、Linux Shell 与脚本基础
linux·云计算