uni-app develops WeChat applet positioning

Use the onLocationChange method to continuously monitor the address, and judge whether to use the latitude and longitude of this positioning according to the positioning accuracy field.

7. Relatively accurate acquisition of latitude and longitude addresses 

1. Register an account

Just enter the information

Tencent Location Services - Based on Ecology, Connecting to the Future

2. Create application and key

1. Enter the console

2. Create an app

 3. Create Key

Enable the product, check WebServiceAPI and WeChat applet, and fill in the corresponding information.

 3. Log in to the backstage of the WeChat public platform

Add request legal domain name: https://apis.map.qq.com

 Development Management - Development Settings - Server Domain Name - Modify

 4. Download the WeChat applet JavaScript SDK

After downloading, put it in your own project

WeChat Mini Program JavaScript SDK | Tencent Location Services

Five, code implementation

var QQMap = require('../../js_sdk/qqmap-wx-jssdk.min.js') // SDK放置的路径
var qqmapsdk = new QQMap({
	key: '**************' // 自己的Key
});

6. Generally obtain the latitude and longitude address

In more prosperous places, this is generally enough.

wx.getLocation({
	type: 'wgs84',
	success: res => {
		console.log(res); //获取到经纬度值
		qqmapsdk.reverseGeocoder({ // 根据经纬度转化为地址
			location: {
				latitude: res.latitude,
				longitude: res.longitude
			},
			success: res => {
				console.log(res);								
			}
		})
	}
});

7. Relatively accurate acquisition of latitude and longitude addresses

1. Obtain permission to monitor location

uni.authorize({
	scope: 'scope.userLocation',
	success(res) {
		console.log(res);
	}
})

2. Turn on location monitoring

wx.startLocationUpdate({
	success: res => {
		console.log(res);
	},
	fail: res => {
		console.log(res);
		//在这里处理开启失败的业务逻辑
	}
})

3. Monitor real-time geographic location changes

let index = 0;
const _locationChangeFn = function(res) {
	console.log('location change', res)
	index++;
	
	//res.accuracy 代表定位精度, 这里根据定位精度和定位次数进行一个综合判断,根据用户在当前页面的停留时间进行相应的修改
	if (index > 10 || (res.accuracy < 35 && index > 5)) {


		wx.offLocationChange();
		wx.stopLocationUpdate();

		qqmapsdk.reverseGeocoder({// 根据经纬度转化为地址
			location: {
				//纬度
				latitude: res.latitude,
				//经度
				longitude: res.longitude
			},
			success: function(res1) {
				console.log(res1.result);
				
			},
			fail: function(res1) {
				console.log(res1);
			}
		})
	}

}

//监听实时地理位置变化事件
wx.onLocationChange(_locationChangeFn)

The addresses located in this way may be similar, but the latitude and longitude are more accurate than those obtained directly with getLocation.

Guess you like

Origin blog.csdn.net/qq_18676843/article/details/124043402