uni-app, WeChat 애플릿 포지셔닝 개발

onLocationChange 메서드를 사용하여 주소를 지속적으로 모니터링하고 위치 정확도 필드에 따라 이 위치의 위도와 경도를 사용할지 여부를 판단합니다.

7. 상대적으로 정확한 위도 및 경도 주소  획득

1. 계정 등록

정보를 입력하면 됩니다

Tencent 위치 서비스 - 생태를 기반으로 미래에 연결

2. 애플리케이션 및 키 생성

1. 콘솔로 들어갑니다

2. 앱 만들기

 3. 키 생성

제품을 활성화하고 WebServiceAPI 및 WeChat 애플릿을 확인하고 해당 정보를 입력합니다.

 3. WeChat 공개 플랫폼 백스테이지 로그인

요청 법적 도메인 이름 추가: https://apis.map.qq.com

 개발 관리 - 개발 설정 - 서버 도메인 이름 - 수정

 4. WeChat 애플릿 JavaScript SDK 다운로드

다운로드 후 자신의 프로젝트에 넣으십시오.

WeChat 미니 프로그램 JavaScript SDK | Tencent 위치 서비스

다섯, 코드 구현

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

6. 일반적으로 위도 및 경도 주소를 얻습니다.

더 번영하는 곳에서는 일반적으로 이것으로 충분합니다.

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

7. 상대적으로 정확한 위도 및 경도 주소 획득

1. 위치 모니터링 권한 획득

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

2. 위치 모니터링 켜기

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

3. 실시간 지리적 위치 변경 모니터링

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)

이런 식으로 위치하는 주소는 비슷할 수 있지만 위도와 경도는 getLocation으로 직접 얻은 것보다 더 정확합니다.

추천

출처blog.csdn.net/qq_18676843/article/details/124043402