uniapp在H5获取当前定位信息不需要SDK可直接获取城市(包括经纬度省市区和市区编码)_uni如何获取城市定位(5)

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!


img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

				uni.hideLoading()
				uni.showToast({
					title: '当前环境无法获取定位信息',
					icon: 'none',
					duration: 2000,
				});
				throw '请在 https 环境中使用本插件'
			}
			if (!navigator || !navigator.geolocation) {
				uni.hideLoading()
				uni.showToast({
					title: '当前环境无法获取定位信息',
					icon: 'none',
					duration: 2000,
				});

				throw '地理位置服务不可用'
			}

			const options = {
				enableHighAccuracy: true,
				timeout: 5000,
				maximumAge: 0
			};
			return new Promise((resolve, rejace) => {
				navigator.geolocation.getCurrentPosition((res) => {
					this.myMap = new qq.maps.Geolocation("你的key",
						"地图标点");
					resolve(this)
				}, rejace, options)
			})
		},

`code 1 表示用户拒绝授权 code 3 未获取到地址信息,可能是设备没有开启定位服务或者系统没有给浏览器定位权限`  
 【H5】 经纬度位置获取[navigator.geolocation.getCurrentPosition]( )  
 [navigator.geolocation.getCurrentPosition]( )(function(){  
 })  
 **参数说明:**



> 
> 经度 : coords.longitude  
>  纬度 : coords.latitude  
>  准确度 : coords.accuracy  
>  海拔 : coords.altitude  
>  海拔准确度 : coords.altitudeAcuracy  
>  行进方向 : coords.heading  
>  地面速度 : coords.speed  
>  请求的时间: new Date(position.timestamp)
> 
> 
> 


###### 3.2封装getLocation获取位置信息的方法



		getLocation() {
			return new Promise((resolve, reject) => {
				this.myMap.getLocation(res => {
					resolve(res)
				}, err => {
					reject(err)
				})
			})
		},

##### 4.使用方法



<get_location ref=‘muLocation’></get_location>

import getLocation from ‘@/componentss/getlocation.vue’
components: {
getLocation,
}

locationRef: null,

onLoad() {
//初始化权限,提示用户授权以及重新获取权限
this.KaTeX parse error: Expected '}', got 'EOF' at end of input: … }) this.refs.muLocation.init().then(location => {
this.locationRef = location
this.getLocationl();
uni.hideLoading()
}, err => {
uni.hideLoading()
if (err.code === 1) {
uni.showModal({
title: ‘获取定位权限失败’,
content: ‘你拒绝了位置授权服务。请允许当前页面获取定位授权,后刷新页面。’
})
} else {
uni.showModal({
title: ‘获取定位权限失败’,
content: ‘请确定手机定位已打开,并且当前浏览器允许获取定位,开启后请刷新页面。’
})
}
})

		})
	},
      methods: {
        // 获取精准定位
		getLocationl() {
			let that = this
			if (!that.locationRef) return uni.showToast({
				title: '未授权位置获取',
				icon: 'none'
			})
			that.locationRef.getLocation()
				.then(res => {
			     	console.oog(res)
			     	//这里面写你自己的逻辑 res即为详细定位信息
					that.valiFormData.latitude = res.lat; //纬度
					that.valiFormData.longitude = res.lng; //经度
					that.valiFormData.areaNum = res.adcode;//市区code
					that.valiFormData.areaNumName = res.city;//市区
				})
		},
	}



![img](https://img-blog.csdnimg.cn/img_convert/e2b712e16cd572c7684212b656fbe295.png)
![img](https://img-blog.csdnimg.cn/img_convert/c9e9e2cebc52fa26656fccde47ff781c.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618636735)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618636735)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值