hookehuyr

fix 完善定位精确度问题

......@@ -2,7 +2,7 @@
* @Author: hookehuyr hookehuyr@gmail.com
* @Date: 2022-05-26 23:52:36
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-12-25 14:49:43
* @LastEditTime: 2024-12-25 16:00:31
* @FilePath: /data-table/src/App.vue
* @Description:
-->
......@@ -169,7 +169,7 @@ onMounted(async () => {
const targetLng = form_setting.geofence_center_longitude;
const radius = form_setting.geofence_circle_radius; // 半径 1000 米
wx.getLocation({
type: 'wgs84', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {
let currentLat = res.latitude; // 纬度,浮点数,范围为90 ~ -90
let currentLng = res.longitude; // 经度,浮点数,范围为180 ~ -180。
......
/*
* @Date: 2024-07-31 17:19:25
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2024-12-25 15:09:33
* @LastEditTime: 2024-12-25 16:02:17
* @FilePath: /data-table/src/utils/tools.js
* @Description: 文件描述
*/
......@@ -225,16 +225,21 @@ const isInViewport = (el) => { // 判断元素是否在可视区域
// return distance <= radius;
// }
const isWithinRadius = (currentLat, currentLng, targetLat, targetLng, radius) => {
const earthRadius = 6371; // 地球平均半径,单位为千米
const toRadians = (angle) => angle * (Math.PI / 180);
let dLat = toRadians(targetLat - currentLat);
let dLng = toRadians(targetLng - currentLng);
let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(toRadians(currentLat)) * Math.cos(toRadians(targetLat)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
let distance = earthRadius * c;
function calculateDistance(lat1, lng1, lat2, lng2) {
var that = this;
let rad1 = lat1 * Math.PI / 180.0;
let rad2 = lat2 * Math.PI / 180.0;
let a = rad1 - rad2;
let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) * Math.cos(
rad2) * Math.pow(
Math.sin(b / 2), 2)));
s = s * 6378137; // 地球半径改为米
s = Math.round(s); // 四舍五入取整,直接返回米
return s; // 返回距离,单位为米
}
function isWithinRadius(currentLat, currentLng, targetLat, targetLng, radius) {
const distance = calculateDistance(currentLat, currentLng, targetLat, targetLng);
return distance <= radius;
}
......