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>
相关推荐
AOwhisky16 小时前
Linux(CentOS)系统管理入门笔记(第九期)——文件权限进阶:SUIDSGIDSticky、ACL 与 chattr
linux·运维·笔记·centos·云计算·文件权限管理
宠友信息1 天前
消息序号如何保证即时通讯源码聊天记录稳定加载
java·spring boot·redis·python·mysql·uni-app
2501_916008891 天前
苹果上架工具怎么选 不用 Mac 上架 App Store 的几种方案
android·macos·ios·小程序·uni-app·iphone·webview
宠友信息1 天前
MySQL复合索引与Druid优化仿小红书源码个人主页查询链路
数据库·spring boot·websocket·mysql·uni-app
Geek-Chow2 天前
Assuming an AWS IAM Role via Federation
云计算·aws
全云在线allcloudonline2 天前
上海阿里云代理商怎么选?跨区域交付能力核验清单
阿里云·云计算·企业上云
Qianyunzhiyun2 天前
AWS EC2实例启动失败?常见报错和解决办法
云计算·aws
Qianyunzhiyun3 天前
AWS EBS快照怎么创建?服务器备份详细教程
服务器·云计算·aws
百胜软件@百胜软件3 天前
零点零一×百胜E3+业务智能中台项目启动,中台赋能客制化机械键盘品牌“加数”发展
大数据·云计算