uniapp-获取省市区地址及内部高德sdk的使用

最近,项目需要获取一个当前定位的市级名称,看了网上的方法都是获取定位经纬度,然后调用api进行反向解析,可是这不适合uniapp所有端都可通用的宗旨,先自己摸索看了文档解决此问题,特在此处随意记上一笔。

首先代码,关键是type和geocode这两个值的配置

// 获取当前市区
			getnowcity(){
				uni.getLocation({
					type:'gcj02',
					geocode:true,
				    success: (res) => {
						// console.log("ssss")
				        // this.nowlocation = formatLocation(res.longitude, res.latitude);
						this.nowlocation = res
						if(res.address && res.address.city){
							this.nowcity = res.address.city
							this.getweather()
						}
						console.log(this.nowlocation)
						
				    },
				    fail: (err) => {
				        // #ifdef MP-BAIDU
				        if (err.errCode === 202 || err.errCode === 10003) { // 202模拟器 10003真机 user deny
				            this.showConfirm();
				        }
				        // #endif
				        // #ifndef MP-BAIDU
				        if (err.errMsg.indexOf("auth deny") >= 0) {
				            uni.showToast({
				                title: "访问位置被拒绝"
				            })
				        } else {
				            uni.showToast({
				                title: err.errMsg+"102",
								icon:'error'
				            })
				        }
				        // #endif
						this.getweather()
				    }
				})
			}

然后,需要外部定位sdk,这里我用的是高德

 在这里,到高德开发者平台配置好,就ok了。

到这里就可以为所欲为了,哪个地方都可以随便调用

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-app中获取省市信息的方法如下所示: 1. 首先,需要导入qqmap-wx-jssdk.js文件,并在方法中实例化API核心类,传入你的key值。 2. 通过uni.authorize方法获取用户的位置权限,成功后使用uni.chooseLocation方法获取用户的经纬度。 3. 创建一个map对象,并使用reverseGeocoder方法进行逆地理位置解析,将经纬度作为参数传入。 4. 在成功的回调函数中,可以通过res.result.ad_info来获取城市信息,其中包括省、市、区(县)等详细信息。 参考代码如下: ``` import map from "../../common/qqmap-wx-jssdk1.2/qqmap-wx-jssdk.js"; export default { methods: { maphq() { uni.authorize({ scope: 'scope.userLocation', success: () => { uni.chooseLocation({ success: (res) => { // 实例化API核心类 var mapsdk = new map({ key: 'key值' }); mapsdk.reverseGeocoder({ location: { latitude: res.latitude, longitude: res.longitude }, success: (res) => { console.log(res.result.ad_info); // 城市信息 }, fail: (error) => { // 处理错误 } }); } }); }, fail: () => { uni.showModal({ title: '提示', content: '位置权限获取失败,请重新授权!', success: function(res) { if (res.confirm) { console.log('用户点击确定'); this.ditu(); } else if (res.cancel) { console.log('用户点击取消'); } } }); } }); }, } } ``` 通过上述方法,你可以在uni-app中获取到省、市等地区的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [uni-app APP端定位获取省/市/区(县)](https://blog.csdn.net/weixin_43840289/article/details/114540688)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [uniapp 小程序使用腾讯地图搜索位置地点,获取省、市、县地区码的方法](https://blog.csdn.net/m0_46647799/article/details/124254740)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值