uniapp get current location applet

If you just need to use the latitude and longitude, you can directly use the official api

uni.getLocation({
	type:"wgs84",
	success:function(res) {
		console.log('当前位置的经度:' + res.longitude);
		console.log('当前位置的纬度:' + res.latitude);
	}
});

If you want to use specific geographical location information
1. You need Tencent Maps to go to Tencent Maps to apply for a key
insert image description here
2. Through the webserviceAPI service: console -> key management -> settings (use the key of this function) -> check webserviceAPI -> Save (the applet SDK needs to use some services of webserviceAPI, so the KEY to use this function needs to have corresponding permissions)
insert image description here

3. Go here to Tencent Maps to download the js file you want to use.
insert image description here
4. After decompression, you can get two js files, which you need to put in your project file directory
insert image description here
. file import or global import

import map from'@static/js/qqmap-wx-jssdk.min.js'```
	
// 获取经纬度
		getLocation(){
			let _this = this;
			uni.getLocation({
			    type: 'wgs84',
			    success: function (res) {
			        console.log('当前位置的经度:' + res.longitude);
			        console.log('当前位置的纬度:' + res.latitude);
		
					_this.getNowCityInfo(res)  //获取城市信息
			    }
			});
		},
		
		
		// 获取当前城市信息
		getNowCityInfo(lag) {
			let qqmapsdk = new  map({
				key: ''  //腾讯地图 - 申请开发者密钥(key)
			});
			qqmapsdk.reverseGeocoder({
				location: {
					longitude: lag.longitude,
					latitude: lag.latitude	
				},
				success(res) {
							console.log('当前位置信息',res)
				}
			})
		}

6. Another point is that if you use uniapp to develop applets, you need to set the location authorization prompt.

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41688609/article/details/120280926