UNI-APP uni.chooseLocation使用及省市区编码获取

下载qqmap-wx-jssdk1.0.zip,解压放在您的项目中(如根目录下的lib文件夹)。

http://3gimg.qq.com/lightmap/xcx/jssdk/qqmap-wx-jssdk1.0.zip

将该域名配置在微信公众平台,已防打包后无法使用。

https://apis.map.qq.com

引入qqmap-wx-jssdk.min.js到相应的代码

import QQMapWX from '@/libs/qqmap-wx-jssdk.min.js';
	const QQMapWXS = new QQMapWX({
		key: 'xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-xxxxx' //开发者密钥 腾讯地图key
	});

在这里我们需要使用到腾讯的地图key , 我们先不管继续走下面的流程(获取省市区及详细地址如下)

                var that = this
				uni.chooseLocation({
					success: function(res) {
						that.sellAnimalsForm.latitude = res.latitude  // 纬度
						that.sellAnimalsForm.longitude = res.longitude // 经度
                     // 这里只能拿到整个的地址 , 如果需要拿到单独的省市区请看下面的代码
						that.location_text = res.address  
						// 解析地址
						QQMapWXS.reverseGeocoder({
							location: {
								latitude: that.sellAnimalsForm.latitude,
								longitude: that.sellAnimalsForm.longitude
							},
							success: function(res) {
								// 省
								that.sellAnimalsForm.province = res.result
									.address_component.province;
								// 市
								that.sellAnimalsForm.city = res.result
									.address_component.city;
								// 区
								that.sellAnimalsForm.area = res.result
									.address_component.district;
								// 详细地址
								that.sellAnimalsForm.address = res.result
									.address_component.street 
                                    + res.result.formatted_addresses
									.recommend
							},
							fail: function(res) {
								uni.showToast({
									title: '无法解析位置信息',
									duration: 2000,
									icon: 'none'
								});
							},
							complete: function(res) {}
						});
					},
					fail(err) {
						console.log(err)
					}
				});

获取省市区编码:(写在QQMapWXS.reverseGeocoder的success中)

QQMapWXS.reverseGeocoder返回的结果中有一个adcode

adcode: 行政区划代码,是中国用来表示各级行政区划的六位数字代码。例如,您提供的"420111"代表的是湖北省武汉市洪山区的行政区划代码

ADCode通常是一个六位数字组成:
例如,北京的海淀区对应的ADCode可能是"110108",其中:
“11” 表示北京市(省级行政区代码)。
“01” 表示市辖区(地级行政区代码)。
“08” 表示海淀区(县级行政区代码)。
需要注意的是,随着行政区划的调整,ADCode也可能发生变化。因此,在使用ADCode时,应确保使用的是最新的、官方发布的代码。

// 省 - 市 - 区
var adCode = res.result.ad_info.adcode
// ADCode通常是一个六位数字组成,其中:
// 前两位表示省级行政区代码。
const Provincial_code = that.substring(adCode, 0, 2); // 输出: 42
// 第三位表示地级行政区代码。
const City_code = that.substring(adCode, 0, 4); // 输出: 4211
// 第四位表示县级行政区代码。
const District_code = that.substring(adCode, 0, 6); // 输出: 421181
// 编号(42@4201@420102)
that.sellAnimalsForm.AddressLeveCode = [Provincial_code, City_code, District_code]
.join('@')
// 经纬度(34.444,23.3344)
that.sellAnimalsForm.LongitudeAndLatitude = [that.sellAnimalsForm.longitude, that
.sellAnimalsForm.latitude].join(',')
// 编码截取方法(写在methods中)
			substring(adCode, startIndex, endIndex) {
				return adCode.substr(startIndex, endIndex - startIndex);
			},

整体上代码的已经完成了,其它的请结合自己的业务场景开发 , 然后我们再来申请腾讯的key(点击申请网址

开通步骤:注册一个账号,然后登录点击控制台(点击应用管理-》我的应用-》添加key-》勾选WebServiceAPI-》保存-》复制key值,将上面的xxxxx-xxxxx-xxxxx-xxxxx-xxxxx-xxxxx替换掉)

注意个人开发者的请求有配额限制 , 建议开通企业的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

码海扬帆:前端探索之旅

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

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

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

打赏作者

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

抵扣说明:

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

余额充值