소스 검색

减少地理位置请求频率

master
dong 5 달 전
부모
커밋
edd64dece2
1개의 변경된 파일41개의 추가작업 그리고 4개의 파일을 삭제
  1. 41
    4
      app.js

+ 41
- 4
app.js 파일 보기

@@ -231,6 +231,7 @@ App({
231 231
   /**
232 232
    * 私有方法:执行真正的定位启动逻辑(核心修复:兼容低版本,改用 wx.onLocationChange)
233 233
    */
234
+  // 
234 235
   _doStartLocationUpload(workorderId, service) {
235 236
     console.log(`开始为订单 ${workorderId} 启动后台定位上传...`);
236 237
     service.currentWorkorderId = workorderId;
@@ -245,13 +246,33 @@ App({
245 246
       success: (res) => {
246 247
         console.log('定位服务已启动', res);
247 248
         service.isLocationUpdateStarted = true;
248
-
249
+// 存储上一次的定位信息
250
+let lastLocation = null;
251
+// 距离阈值(单位:米,根据需求调整,5米)
252
+const DISTANCE_THRESHOLD = 5;
249 253
         // 修复3:改用低版本支持的 wx.onLocationChange(替代 wx.watchPosition)
250 254
         wx.onLocationChange((location) => {
251
-          console.log('位置变化,准备上传:', location);
252
-          this._uploadLocation(location, workorderId);
255
+          if (!lastLocation) {
256
+            lastLocation = location;
257
+            console.log('位置变化,准备上传:', location);
258
+            this._uploadLocation(location, workorderId);
259
+            return;
260
+          }
261
+        // 计算当前定位与上一次的直线距离(Haversine公式)
262
+  const distance = this.calculateDistance(
263
+    lastLocation.latitude,
264
+    lastLocation.longitude,
265
+    location.latitude,
266
+    location.longitude
267
+  );
268
+console.log(distance);
269
+  // 只有距离超过阈值,才认为是“有效变化”
270
+  if (distance >= DISTANCE_THRESHOLD) {
271
+    lastLocation = location; // 更新上一次定位
272
+    this._uploadLocation(location, workorderId);
273
+  }  
274
+      
253 275
         });
254
-
255 276
         // 标记定位中状态
256 277
         service.isTracking = true;
257 278
         console.log('位置监听已注册,开始实时上传');
@@ -267,6 +288,22 @@ App({
267 288
     });
268 289
   },
269 290
 
291
+// Haversine公式:计算两点经纬度之间的直线距离(单位:米)
292
+ calculateDistance(lat1, lng1, lat2, lng2) {
293
+  const R = 6371000; // 地球半径(米)
294
+  const radLat1 = (lat1 * Math.PI) / 180;
295
+  const radLat2 = (lat2 * Math.PI) / 180;
296
+  const deltaLat = radLat2 - radLat1;
297
+  const deltaLng = (lng2 - lng1) * Math.PI / 180;
298
+
299
+  const a = 
300
+    Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
301
+    Math.cos(radLat1) * Math.cos(radLat2) *
302
+    Math.sin(deltaLng / 2) * Math.sin(deltaLng / 2);
303
+  
304
+  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
305
+  return R * c; // 距离(米)
306
+},
270 307
   /**
271 308
    * 司机端停止定位上传(核心修复:对应移除 wx.onLocationChange 监听)
272 309
    */

Loading…
취소
저장