@tmsfe/tms-core 0.0.45 → 0.0.48

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.45",
3
+ "version": "0.0.48",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -0,0 +1,65 @@
1
+ /**
2
+ * 经纬度距离计算
3
+ */
4
+ import { roundStr } from './stringUtils';
5
+
6
+ // 将角度换算为弧度
7
+ function convertDegreesToRadians(degrees: number): number {
8
+ return (degrees * Math.PI) / 180;
9
+ }
10
+
11
+ function haverSin(theta: number): number {
12
+ const val = Math.sin(theta / 2);
13
+ return val * val;
14
+ }
15
+
16
+ // 计算两点间距离,单位 m
17
+ function calCoordinateDistance(
18
+ a: { latitude: number, longitude: number },
19
+ b: { latitude: number, longitude: number },
20
+ ): number {
21
+ // 地球半径 平均值,千米
22
+ const earthRadius = 6371;
23
+ const lat1 = convertDegreesToRadians(a.latitude);
24
+ const lon1 = convertDegreesToRadians(a.longitude);
25
+ const lat2 = convertDegreesToRadians(b.latitude);
26
+ const lon2 = convertDegreesToRadians(b.longitude);
27
+ // 差值
28
+ const vLon = Math.abs(lon1 - lon2);
29
+ const vLat = Math.abs(lat1 - lat2);
30
+ const h = haverSin(vLat) + (Math.cos(lat1) * Math.cos(lat2) * haverSin(vLon));
31
+ const distance = 2 * earthRadius * Math.asin(Math.sqrt(h));
32
+ return distance * 1000;
33
+ }
34
+
35
+ /**
36
+ * 对距离进行格式化
37
+ * @param {Number} distance 距离,单位米
38
+ * @param {Number} keep 保留几位小数
39
+ * @param {Boolean} removeTrailingZero 是否移除字符串末尾的无效数字0
40
+ * @param {String} unit 单位风格类型:en-英文-m/km,EN-英文大写-M/KM,zh-中文-米/千米,ZH-中文-米/公里
41
+ * @returns 格式化后的字符串;distance非法时返回空字符串
42
+ */
43
+ function formatDistance(distance: number|string, keep = 2, removeTrailingZero = false, unit = 'en'): string {
44
+ const distanceNum = parseFloat(String(distance));
45
+ if (isNaN(distanceNum)) return '';
46
+
47
+ // 数字部分格式化
48
+ const num = distanceNum >= 1000 ? distanceNum / 1000 : distanceNum;
49
+ const numStr = roundStr(num, keep, removeTrailingZero);
50
+ // 单位部分格式化
51
+ let units;
52
+ switch (unit) {
53
+ case 'zh': units = ['米', '千米']; break;
54
+ case 'ZH': units = ['米', '公里']; break;
55
+ case 'EN': units = ['M', 'KM']; break;
56
+ default: units = ['m', 'km']; break;
57
+ }
58
+ const unitStr = units[distanceNum >= 1000 ? 1 : 0];
59
+ return `${numStr}${unitStr}`;
60
+ }
61
+
62
+ export {
63
+ calCoordinateDistance,
64
+ formatDistance,
65
+ };
@@ -10,6 +10,7 @@ import AutoReport from './report/proxy/index';
10
10
  import md5 from './md5';
11
11
  import { rpxToPx } from './rpx';
12
12
  import { serialize } from './objUtils';
13
+ import { calCoordinateDistance, formatDistance } from './distanceUtils';
13
14
  import * as stringUtils from './stringUtils';
14
15
  import * as timeUtils from './timeUtils';
15
16
  import * as ipxHelper from './ipxHelper';
@@ -210,6 +211,8 @@ const api = {
210
211
  getLocationManager,
211
212
  rpxToPx,
212
213
  serialize,
214
+ calCoordinateDistance,
215
+ formatDistance,
213
216
  createRequest,
214
217
  getEnvInfo,
215
218
  isAppPageExist,
package/src/index.js CHANGED
@@ -10,6 +10,7 @@ import md5 from './md5';
10
10
  import { callCloudFunc } from './cloudService';
11
11
  import EventDispatcher from './eventDispatcher';
12
12
  import { serialize } from './objUtils';
13
+ import { calCoordinateDistance, formatDistance } from './distanceUtils';
13
14
  import { rpxToPx } from './rpx';
14
15
  import {
15
16
  formatPlate,
@@ -169,6 +170,10 @@ const api = {
169
170
  /* 处理对象方法 */
170
171
  serialize,
171
172
 
173
+ /* 距离方法 */
174
+ calCoordinateDistance,
175
+ formatDistance,
176
+
172
177
  /* 获取外部合作商openid */
173
178
  getMpOpenId, // 变更为 getOuterOpenId
174
179
  getOuterOpenId,
@@ -11,7 +11,7 @@ function reportData(...args: any[]): any {
11
11
  const tms = getApp()?.tms || wx.tms;
12
12
  reporter = tms.getReporter();
13
13
  }
14
- // console.log('自动埋点:', ...args);
14
+ console.log('自动埋点:', ...args);
15
15
  reporter.report2(...args);
16
16
  }
17
17