@tmsfe/tms-core 0.0.24 → 0.0.25
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 +1 -1
- package/src/distanceBetweenPoints.js +73 -0
- package/src/index.js +2 -0
- package/src/location/index.ts +1 -0
package/package.json
CHANGED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
|
|
2
|
+
// 墨卡托坐标转经纬度
|
|
3
|
+
function mercator2LonLat(p) {
|
|
4
|
+
const r = p.y / 111319.49077777778;
|
|
5
|
+
const n = Math.atan(Math.exp(0.017453292519943295 * r)) / 0.008726646259971648 - 90;
|
|
6
|
+
return {
|
|
7
|
+
x: p.x / 111319.49077777778,
|
|
8
|
+
y: n,
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
// 角度转弧度
|
|
14
|
+
function angle2Rad(t) {
|
|
15
|
+
return (t * Math.PI) / 180;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
function client2ServerX(t) {
|
|
20
|
+
return t - 20037508;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function client2ServerY(t) {
|
|
24
|
+
return t - 30240971;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function longitude2ClientX(t) {
|
|
28
|
+
return Number(111319.49077777778 * Number(t) + 20037508).toFixed(0);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function latitude2ClientY(t) {
|
|
32
|
+
const n = Math.log(Math.tan(0.008726646259971648 * (90 + Number(t)))) / 0.017453292519943295;
|
|
33
|
+
return Number(111319.49077777778 * n + 30240971).toFixed(0);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function geoPointToServerPoint(t) {
|
|
37
|
+
let n = {};
|
|
38
|
+
if (t) {
|
|
39
|
+
n = {
|
|
40
|
+
x: client2ServerX(longitude2ClientX(t.longitude)),
|
|
41
|
+
y: client2ServerY(latitude2ClientY(t.latitude)),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return n;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* 两个经纬度间地理距离(单位:米)
|
|
50
|
+
* @param {*} start {longitude, latitude}
|
|
51
|
+
* @param {*} end {longitude, latitude}
|
|
52
|
+
* @returns
|
|
53
|
+
*/
|
|
54
|
+
function distanceBetweenPoints(start, end) {
|
|
55
|
+
let s = geoPointToServerPoint(start);
|
|
56
|
+
let t = geoPointToServerPoint(end);
|
|
57
|
+
s = mercator2LonLat(s); // eslint-disable-line
|
|
58
|
+
t = mercator2LonLat(t); // eslint-disable-line
|
|
59
|
+
const deltaY = angle2Rad(s.y) - angle2Rad(t.y);
|
|
60
|
+
const deltaX = angle2Rad(s.x) - angle2Rad(t.x);
|
|
61
|
+
let res = 2
|
|
62
|
+
* Math.asin(Math.sqrt(Math.pow(Math.sin(deltaY / 2), 2) // eslint-disable-line
|
|
63
|
+
+ Math.cos(angle2Rad(s.y))
|
|
64
|
+
* Math.cos(angle2Rad(t.y))
|
|
65
|
+
* Math.pow(Math.sin(deltaX / 2), 2))); // eslint-disable-line
|
|
66
|
+
res *= 6378137;
|
|
67
|
+
res = Math.floor(res * 10000 + 0.5) / 10000;
|
|
68
|
+
return res;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export {
|
|
72
|
+
distanceBetweenPoints,
|
|
73
|
+
};
|
package/src/index.js
CHANGED
|
@@ -11,6 +11,7 @@ import { cache as report, startListenApp } from './report';
|
|
|
11
11
|
import EventDispatcher from './eventDispatcher';
|
|
12
12
|
import { serialize } from './objUtils';
|
|
13
13
|
import { rpxToPx } from './rpx';
|
|
14
|
+
import { distanceBetweenPoints } from './distanceBetweenPoints';
|
|
14
15
|
import {
|
|
15
16
|
formatPlate,
|
|
16
17
|
subStr,
|
|
@@ -180,6 +181,7 @@ const api = {
|
|
|
180
181
|
|
|
181
182
|
/** rpx转px */
|
|
182
183
|
rpxToPx,
|
|
184
|
+
distanceBetweenPoints,
|
|
183
185
|
|
|
184
186
|
storage,
|
|
185
187
|
|