@heycar/heycars-map 2.8.2 → 2.9.0

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.
@@ -16,6 +16,7 @@ declare global {
16
16
  let wx: Wx;
17
17
  let my: AlipayMy;
18
18
  let ap: AlipayAp;
19
+ let HEYCAR_GEOLOCATION_GET_CURRENT_POSITION: Geolocation["getCurrentPosition"] | undefined;
19
20
  interface WindowEventMap {
20
21
  deviceorientationabsolute: DeviceOrientationEvent;
21
22
  }
@@ -5,6 +5,7 @@ import { createReadOnce, sleep, createRunOnce } from "./helper.js";
5
5
  import { detectBrowserPlatform, BRWOSER_PLATFORM, OS_PLATFORM, detectAlipayWallet, ALIPAY_WALLET, detectOSPlatform } from "./platform.js";
6
6
  import { QueryWxSignatureStatus } from "./QueryWxSignatureStatus.js";
7
7
  import { isGeoPositionEqual, alipayMyGetLocationSuccessResponse2GeolocationPosition, alipayMyGetLocationError2GeolocationPositionErrorCode, wxGetLocationSuccessResponse2GeolocationPosition } from "./transform.js";
8
+ import { toGeolocationGetCurrentPosition } from "./typeChecking.js";
8
9
  const WX_INITIAL_GET_LOCATION_TIME_RATIO = 4;
9
10
  const WX_SIGNATURE_TIMEOUT = 20 * 1e3;
10
11
  const WX_GET_LOCATION_INTERVAL = 1 * 1e3;
@@ -200,7 +201,7 @@ const apGetLocation = ({ type, cacheTimeout, success, fail }) => {
200
201
  const apType = type >= 2 ? 0 : type === 0 ? 2 : 1;
201
202
  ap.getLocation({ type: apType, cacheTimeout }).then(success).catch(fail);
202
203
  };
203
- function androidWatchPosition(onSuccess, onError, option) {
204
+ function androidWatchPosition(onSuccess, onError, option, getLocationFn) {
204
205
  let enable = true;
205
206
  let prevGeoPosition = void 0;
206
207
  let geoErrorMessage = void 0;
@@ -213,7 +214,7 @@ function androidWatchPosition(onSuccess, onError, option) {
213
214
  }
214
215
  const geoPosition = await new Promise((resolve) => {
215
216
  let isHandled = false;
216
- navigator.geolocation.getCurrentPosition(
217
+ getLocationFn(
217
218
  function onSuccess2(geoLocationPosition) {
218
219
  if (isHandled)
219
220
  return;
@@ -290,7 +291,12 @@ function compatibleWathPosition(onSuccess, onError, option) {
290
291
  if (browserPlatform === BRWOSER_PLATFORM.ALIPAY || browserPlatform == BRWOSER_PLATFORM.ALIPAY_MINIPROGRAM) {
291
292
  return alipayWatchPosition(onSuccess, onError, option, (props) => my.getLocation(props));
292
293
  }
293
- return androidWatchPosition(onSuccess, onError, option);
294
+ return androidWatchPosition(onSuccess, onError, option, (...args) => {
295
+ return typeof HEYCAR_GEOLOCATION_GET_CURRENT_POSITION === "function" ? toGeolocationGetCurrentPosition(
296
+ HEYCAR_GEOLOCATION_GET_CURRENT_POSITION,
297
+ "HEYCAR_GEOLOCATION_GET_CURRENT_POSITION"
298
+ )(...args) : navigator.geolocation.getCurrentPosition(...args);
299
+ });
294
300
  }
295
301
  function compatibleGeoPositionTransform(position, transform) {
296
302
  const platform = detectBrowserPlatform();
@@ -1,6 +1,6 @@
1
1
  const availableLogKeys = /* @__PURE__ */ new Set();
2
2
  const pkgName = "@heycar/heycars-map";
3
- const pkgVersion = "2.8.2";
3
+ const pkgVersion = "2.9.0";
4
4
  const isEnableLog = (name) => {
5
5
  const searchParam = new URLSearchParams(location.search);
6
6
  return searchParam.has(`log-${name}`);
@@ -48,4 +48,5 @@ export declare const signal2promise: (signal?: AbortSignal) => Promise<any>;
48
48
  export declare const googleEncodedPolyline2googleLatLng: (value: string) => google.maps.LatLng[];
49
49
  export declare const gaodePolyline2amapLngLat: (value: string) => AMap.LngLat[];
50
50
  export declare const amapTraffic2trafficStatus: (value?: AMap.TMC["status"]) => TrafficStatus;
51
+ export declare const error2beautifyString: (error: unknown) => string;
51
52
  export {};
@@ -306,6 +306,15 @@ const amapTraffic2trafficStatus = (value) => {
306
306
  return "HEAVY_TRAFFIC_JAM";
307
307
  }
308
308
  };
309
+ const error2beautifyString = (error) => {
310
+ if (typeof error === "string")
311
+ return error;
312
+ if (error instanceof GeolocationPositionError) {
313
+ const { code, message } = error;
314
+ return `GeolocationPositionError: code: ${code}, message: ${message}`;
315
+ }
316
+ return JSON.stringify(error, Object.getOwnPropertyNames(error), 2).replace(/\\n/g, "\n").replace(/\\"/g, '"');
317
+ };
309
318
  export {
310
319
  alipayMyGetLocationError2GeolocationPositionErrorCode,
311
320
  alipayMyGetLocationSuccessResponse2GeolocationPosition,
@@ -316,6 +325,7 @@ export {
316
325
  decodeAsterisk,
317
326
  deltaZoom2animationDuration,
318
327
  distanceVw2animationDuration,
328
+ error2beautifyString,
319
329
  gaodePointsStringify,
320
330
  gaodePolyline2amapLngLat,
321
331
  geoPositionError2businessTimeout,
@@ -42,4 +42,13 @@ interface MaybeCoordinateTrackPoint {
42
42
  }
43
43
  export declare const toCoordinateTrackPoint: (value?: MaybeCoordinateTrackPoint) => CoordinateTrackPoint;
44
44
  export declare function isProxyServiceError(error: unknown): error is ProxyServiceError;
45
+ export declare function toGeolocationPositionError(error: unknown, prefix?: string): GeolocationPositionError;
46
+ /**
47
+ * @warning: 目前只用到
48
+ * * GeolocationPosition.coords.latitude
49
+ * * GeolocationPosition.coords.longitude
50
+ * 如果将来用到其他属性,需要修补充
51
+ */
52
+ export declare function assertGeolocationPosition(position: unknown): asserts position is GeolocationPosition;
53
+ export declare function toGeolocationGetCurrentPosition(fn: Geolocation["getCurrentPosition"], fnName?: string): Geolocation["getCurrentPosition"];
45
54
  export {};
@@ -1,3 +1,4 @@
1
+ import { error2beautifyString } from "./transform.js";
1
2
  const createTypeError = (typeName, value, detail) => {
2
3
  if (detail)
3
4
  return new Error(
@@ -109,7 +110,62 @@ const toCoordinateTrackPoint = (value) => {
109
110
  function isProxyServiceError(error) {
110
111
  return !!error && typeof error === "object" && "msg" in error && "code" in error;
111
112
  }
113
+ function toGeolocationPositionError(error, prefix = "") {
114
+ if (error instanceof GeolocationPositionError)
115
+ return error;
116
+ if (typeof error === "object" && error !== null && "code" in error && (error.code === 1 || error.code === 2 || error.code === 3) && "message" in error && typeof error.message === "string") {
117
+ return error;
118
+ }
119
+ return {
120
+ code: 2,
121
+ message: prefix + `GeolocationPositionError 格式错误, origin error:
122
+ ${error2beautifyString(error)}`
123
+ };
124
+ }
125
+ function assertGeolocationPosition(position) {
126
+ if (position instanceof GeolocationPosition)
127
+ return;
128
+ if (typeof position !== "object" || position === null)
129
+ throw Error("GeolocationPosition 格式错误, 应当是一个非 null 的 object");
130
+ if (!("coords" in position) || typeof position.coords !== "object" || position.coords === null)
131
+ throw Error("GeolocationPosition 格式错误, coords 应当是非 null 的 object");
132
+ if (!("latitude" in position.coords) || typeof position.coords.latitude !== "number")
133
+ throw Error("GeolocationPosition 格式错误, latitude 应当是 number");
134
+ if (!("longitude" in position.coords) || typeof position.coords.longitude !== "number")
135
+ throw Error("GeolocationPosition 格式错误, longitude 应当是 number");
136
+ }
137
+ function toGeolocationGetCurrentPosition(fn, fnName = fn.name) {
138
+ const prefix = "ASSERT_GEOLOCATION_GET_CURRENT_POSITION_ERROR\n";
139
+ if (typeof fn !== "function")
140
+ throw Error(prefix + "Geolocation.getCurrentPosition 类型必须是一个 function");
141
+ return function assertedGetCurrentPosition(onSuccess, onError, options) {
142
+ const asseertedOnSuccess = (position) => {
143
+ try {
144
+ assertGeolocationPosition(position);
145
+ } catch (err) {
146
+ const { message } = err;
147
+ onError({
148
+ code: 2,
149
+ message: prefix + `${fnName} onSuccess 回调错误: ${message}`
150
+ });
151
+ return;
152
+ }
153
+ onSuccess(position);
154
+ };
155
+ const asseertedOnError = (error) => onError(toGeolocationPositionError(error, prefix));
156
+ try {
157
+ fn(asseertedOnSuccess, asseertedOnError, options);
158
+ } catch (err) {
159
+ onError({
160
+ code: 2,
161
+ message: prefix + `${fnName} 运行异常:
162
+ ${error2beautifyString(err)}`
163
+ });
164
+ }
165
+ };
166
+ }
112
167
  export {
168
+ assertGeolocationPosition,
113
169
  assertNoConflictTypeAndZone,
114
170
  createTypeError,
115
171
  isCoordinatePoint,
@@ -121,5 +177,7 @@ export {
121
177
  toCoordinateRecommendZonePlacesType,
122
178
  toCoordinateTrackPoint,
123
179
  toCoordinateZoneType,
180
+ toGeolocationGetCurrentPosition,
181
+ toGeolocationPositionError,
124
182
  toRecommendType
125
183
  };
@@ -16,6 +16,7 @@ declare global {
16
16
  let wx: Wx;
17
17
  let my: AlipayMy;
18
18
  let ap: AlipayAp;
19
+ let HEYCAR_GEOLOCATION_GET_CURRENT_POSITION: Geolocation["getCurrentPosition"] | undefined;
19
20
  interface WindowEventMap {
20
21
  deviceorientationabsolute: DeviceOrientationEvent;
21
22
  }
@@ -5,6 +5,7 @@ import { createReadOnce, sleep, createRunOnce } from "./helper.js";
5
5
  import { detectBrowserPlatform, BRWOSER_PLATFORM, OS_PLATFORM, detectAlipayWallet, ALIPAY_WALLET, detectOSPlatform } from "./platform.js";
6
6
  import { QueryWxSignatureStatus } from "./QueryWxSignatureStatus.js";
7
7
  import { isGeoPositionEqual, alipayMyGetLocationSuccessResponse2GeolocationPosition, alipayMyGetLocationError2GeolocationPositionErrorCode, wxGetLocationSuccessResponse2GeolocationPosition } from "./transform.js";
8
+ import { toGeolocationGetCurrentPosition } from "./typeChecking.js";
8
9
  const WX_INITIAL_GET_LOCATION_TIME_RATIO = 4;
9
10
  const WX_SIGNATURE_TIMEOUT = 20 * 1e3;
10
11
  const WX_GET_LOCATION_INTERVAL = 1 * 1e3;
@@ -200,7 +201,7 @@ const apGetLocation = ({ type, cacheTimeout, success, fail }) => {
200
201
  const apType = type >= 2 ? 0 : type === 0 ? 2 : 1;
201
202
  ap.getLocation({ type: apType, cacheTimeout }).then(success).catch(fail);
202
203
  };
203
- function androidWatchPosition(onSuccess, onError, option) {
204
+ function androidWatchPosition(onSuccess, onError, option, getLocationFn) {
204
205
  let enable = true;
205
206
  let prevGeoPosition = void 0;
206
207
  let geoErrorMessage = void 0;
@@ -213,7 +214,7 @@ function androidWatchPosition(onSuccess, onError, option) {
213
214
  }
214
215
  const geoPosition = await new Promise((resolve) => {
215
216
  let isHandled = false;
216
- navigator.geolocation.getCurrentPosition(
217
+ getLocationFn(
217
218
  function onSuccess2(geoLocationPosition) {
218
219
  if (isHandled)
219
220
  return;
@@ -290,7 +291,12 @@ function compatibleWathPosition(onSuccess, onError, option) {
290
291
  if (browserPlatform === BRWOSER_PLATFORM.ALIPAY || browserPlatform == BRWOSER_PLATFORM.ALIPAY_MINIPROGRAM) {
291
292
  return alipayWatchPosition(onSuccess, onError, option, (props) => my.getLocation(props));
292
293
  }
293
- return androidWatchPosition(onSuccess, onError, option);
294
+ return androidWatchPosition(onSuccess, onError, option, (...args) => {
295
+ return typeof HEYCAR_GEOLOCATION_GET_CURRENT_POSITION === "function" ? toGeolocationGetCurrentPosition(
296
+ HEYCAR_GEOLOCATION_GET_CURRENT_POSITION,
297
+ "HEYCAR_GEOLOCATION_GET_CURRENT_POSITION"
298
+ )(...args) : navigator.geolocation.getCurrentPosition(...args);
299
+ });
294
300
  }
295
301
  function compatibleGeoPositionTransform(position, transform) {
296
302
  const platform = detectBrowserPlatform();
@@ -1,6 +1,6 @@
1
1
  const availableLogKeys = /* @__PURE__ */ new Set();
2
2
  const pkgName = "@heycar/heycars-map";
3
- const pkgVersion = "2.8.2";
3
+ const pkgVersion = "2.9.0";
4
4
  const isEnableLog = (name) => {
5
5
  const searchParam = new URLSearchParams(location.search);
6
6
  return searchParam.has(`log-${name}`);
@@ -48,4 +48,5 @@ export declare const signal2promise: (signal?: AbortSignal) => Promise<any>;
48
48
  export declare const googleEncodedPolyline2googleLatLng: (value: string) => google.maps.LatLng[];
49
49
  export declare const gaodePolyline2amapLngLat: (value: string) => AMap.LngLat[];
50
50
  export declare const amapTraffic2trafficStatus: (value?: AMap.TMC["status"]) => TrafficStatus;
51
+ export declare const error2beautifyString: (error: unknown) => string;
51
52
  export {};
@@ -306,6 +306,15 @@ const amapTraffic2trafficStatus = (value) => {
306
306
  return "HEAVY_TRAFFIC_JAM";
307
307
  }
308
308
  };
309
+ const error2beautifyString = (error) => {
310
+ if (typeof error === "string")
311
+ return error;
312
+ if (error instanceof GeolocationPositionError) {
313
+ const { code, message } = error;
314
+ return `GeolocationPositionError: code: ${code}, message: ${message}`;
315
+ }
316
+ return JSON.stringify(error, Object.getOwnPropertyNames(error), 2).replace(/\\n/g, "\n").replace(/\\"/g, '"');
317
+ };
309
318
  export {
310
319
  alipayMyGetLocationError2GeolocationPositionErrorCode,
311
320
  alipayMyGetLocationSuccessResponse2GeolocationPosition,
@@ -316,6 +325,7 @@ export {
316
325
  decodeAsterisk,
317
326
  deltaZoom2animationDuration,
318
327
  distanceVw2animationDuration,
328
+ error2beautifyString,
319
329
  gaodePointsStringify,
320
330
  gaodePolyline2amapLngLat,
321
331
  geoPositionError2businessTimeout,
@@ -42,4 +42,13 @@ interface MaybeCoordinateTrackPoint {
42
42
  }
43
43
  export declare const toCoordinateTrackPoint: (value?: MaybeCoordinateTrackPoint) => CoordinateTrackPoint;
44
44
  export declare function isProxyServiceError(error: unknown): error is ProxyServiceError;
45
+ export declare function toGeolocationPositionError(error: unknown, prefix?: string): GeolocationPositionError;
46
+ /**
47
+ * @warning: 目前只用到
48
+ * * GeolocationPosition.coords.latitude
49
+ * * GeolocationPosition.coords.longitude
50
+ * 如果将来用到其他属性,需要修补充
51
+ */
52
+ export declare function assertGeolocationPosition(position: unknown): asserts position is GeolocationPosition;
53
+ export declare function toGeolocationGetCurrentPosition(fn: Geolocation["getCurrentPosition"], fnName?: string): Geolocation["getCurrentPosition"];
45
54
  export {};
@@ -1,3 +1,4 @@
1
+ import { error2beautifyString } from "./transform.js";
1
2
  const createTypeError = (typeName, value, detail) => {
2
3
  if (detail)
3
4
  return new Error(
@@ -109,7 +110,62 @@ const toCoordinateTrackPoint = (value) => {
109
110
  function isProxyServiceError(error) {
110
111
  return !!error && typeof error === "object" && "msg" in error && "code" in error;
111
112
  }
113
+ function toGeolocationPositionError(error, prefix = "") {
114
+ if (error instanceof GeolocationPositionError)
115
+ return error;
116
+ if (typeof error === "object" && error !== null && "code" in error && (error.code === 1 || error.code === 2 || error.code === 3) && "message" in error && typeof error.message === "string") {
117
+ return error;
118
+ }
119
+ return {
120
+ code: 2,
121
+ message: prefix + `GeolocationPositionError 格式错误, origin error:
122
+ ${error2beautifyString(error)}`
123
+ };
124
+ }
125
+ function assertGeolocationPosition(position) {
126
+ if (position instanceof GeolocationPosition)
127
+ return;
128
+ if (typeof position !== "object" || position === null)
129
+ throw Error("GeolocationPosition 格式错误, 应当是一个非 null 的 object");
130
+ if (!("coords" in position) || typeof position.coords !== "object" || position.coords === null)
131
+ throw Error("GeolocationPosition 格式错误, coords 应当是非 null 的 object");
132
+ if (!("latitude" in position.coords) || typeof position.coords.latitude !== "number")
133
+ throw Error("GeolocationPosition 格式错误, latitude 应当是 number");
134
+ if (!("longitude" in position.coords) || typeof position.coords.longitude !== "number")
135
+ throw Error("GeolocationPosition 格式错误, longitude 应当是 number");
136
+ }
137
+ function toGeolocationGetCurrentPosition(fn, fnName = fn.name) {
138
+ const prefix = "ASSERT_GEOLOCATION_GET_CURRENT_POSITION_ERROR\n";
139
+ if (typeof fn !== "function")
140
+ throw Error(prefix + "Geolocation.getCurrentPosition 类型必须是一个 function");
141
+ return function assertedGetCurrentPosition(onSuccess, onError, options) {
142
+ const asseertedOnSuccess = (position) => {
143
+ try {
144
+ assertGeolocationPosition(position);
145
+ } catch (err) {
146
+ const { message } = err;
147
+ onError({
148
+ code: 2,
149
+ message: prefix + `${fnName} onSuccess 回调错误: ${message}`
150
+ });
151
+ return;
152
+ }
153
+ onSuccess(position);
154
+ };
155
+ const asseertedOnError = (error) => onError(toGeolocationPositionError(error, prefix));
156
+ try {
157
+ fn(asseertedOnSuccess, asseertedOnError, options);
158
+ } catch (err) {
159
+ onError({
160
+ code: 2,
161
+ message: prefix + `${fnName} 运行异常:
162
+ ${error2beautifyString(err)}`
163
+ });
164
+ }
165
+ };
166
+ }
112
167
  export {
168
+ assertGeolocationPosition,
113
169
  assertNoConflictTypeAndZone,
114
170
  createTypeError,
115
171
  isCoordinatePoint,
@@ -121,5 +177,7 @@ export {
121
177
  toCoordinateRecommendZonePlacesType,
122
178
  toCoordinateTrackPoint,
123
179
  toCoordinateZoneType,
180
+ toGeolocationGetCurrentPosition,
181
+ toGeolocationPositionError,
124
182
  toRecommendType
125
183
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heycar/heycars-map",
3
- "version": "2.8.2",
3
+ "version": "2.9.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "checkVersion": "./bin/checkVersion.js",