@heycar/heycars-map 0.6.5 → 0.6.6

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/dist/index.cjs CHANGED
@@ -943,6 +943,11 @@ const isPlaceEqual = (p1, p2) => {
943
943
  const isPointEqual = (p1, p2) => {
944
944
  return p1[0] === (p2 == null ? void 0 : p2[0]) && p1[1] === (p2 == null ? void 0 : p2[1]);
945
945
  };
946
+ const isGeoPositionEqual = (p1, p2) => {
947
+ if (!p2)
948
+ return false;
949
+ return p1.coords.longitude === p2.coords.longitude && p1.coords.latitude === p2.coords.latitude;
950
+ };
946
951
  const toPlaceType = (maybePlace) => {
947
952
  const lng = Number(maybePlace.lng);
948
953
  if (isNaN(lng))
@@ -952,6 +957,21 @@ const toPlaceType = (maybePlace) => {
952
957
  throw new Error("MyError: expect lat to be number");
953
958
  return { lng, lat, name: maybePlace.name };
954
959
  };
960
+ const wxGetLocationSuccessResponse2GeolocationPosition = (res) => {
961
+ const { longitude, latitude, accuracy } = res;
962
+ const speed = Number(res.speed);
963
+ const timestamp = Date.now();
964
+ const coords = {
965
+ accuracy: Number(accuracy),
966
+ altitude: null,
967
+ altitudeAccuracy: null,
968
+ heading: null,
969
+ latitude: Number(latitude),
970
+ longitude: Number(longitude),
971
+ speed
972
+ };
973
+ return { timestamp, coords };
974
+ };
955
975
  const deepCompareEqualsForMaps = createComparator((deepEqual) => (a, b) => {
956
976
  if (isLatLngLiteral(a) || isLatLngLiteral(b)) {
957
977
  return new google.maps.LatLng(a).equals(new google.maps.LatLng(b));
@@ -3036,6 +3056,90 @@ const BusinessQuotingMap = defineLagecySetup(function BusinessQuotingMap2(props)
3036
3056
  })]);
3037
3057
  };
3038
3058
  }).props(["fallback", "from", "loading", "mapRef", "registerOverlay", "renderDescription", "to", "fromDescription", "log"]);
3059
+ const WX_GET_LOCATION_INTERVAL_STILL = 10 * 1e3;
3060
+ const WX_GET_LOCATION_INTERVAL_SLOW = 3 * 1e3;
3061
+ const WX_GET_LOCATION_INTERVAL_FAST = 1 * 1e3;
3062
+ const takeIsForceBrowserGeo = () => {
3063
+ const searchParam = new URLSearchParams(location.search);
3064
+ return searchParam.has(`force-browser-geo`);
3065
+ };
3066
+ const sleep$1 = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
3067
+ const wxReady = () => new Promise((resolve, reject) => {
3068
+ wx.ready(resolve);
3069
+ wx.error(reject);
3070
+ });
3071
+ const detectPlatform = () => {
3072
+ const useragent = navigator.userAgent.toLowerCase();
3073
+ if (useragent.includes("micromessenger"))
3074
+ return "wechat";
3075
+ if (useragent.includes("alipay"))
3076
+ return "alipay";
3077
+ return "other";
3078
+ };
3079
+ function wechatWatchPosition(onSuccess, onError, option) {
3080
+ const { timeout } = option;
3081
+ let enable = true;
3082
+ let interval = WX_GET_LOCATION_INTERVAL_STILL;
3083
+ let prevGeoPosition = void 0;
3084
+ const startWatch = async () => {
3085
+ await wxReady();
3086
+ do {
3087
+ const geoPosition = await new Promise((resolve, reject) => {
3088
+ let isHandled = false;
3089
+ wx.getLocation({
3090
+ type: "wgs84",
3091
+ success(res) {
3092
+ if (isHandled)
3093
+ return;
3094
+ isHandled = true;
3095
+ const speed = Number(res.speed);
3096
+ interval = speed === 0 ? WX_GET_LOCATION_INTERVAL_STILL : speed < 2 ? WX_GET_LOCATION_INTERVAL_SLOW : WX_GET_LOCATION_INTERVAL_FAST;
3097
+ resolve(wxGetLocationSuccessResponse2GeolocationPosition(res));
3098
+ },
3099
+ fail(res) {
3100
+ if (isHandled)
3101
+ return;
3102
+ isHandled = true;
3103
+ const error = new Error(res.errMsg);
3104
+ reject(error);
3105
+ onError == null ? void 0 : onError(error);
3106
+ }
3107
+ });
3108
+ if (!timeout)
3109
+ return;
3110
+ setTimeout(() => {
3111
+ if (isHandled)
3112
+ return;
3113
+ interval = WX_GET_LOCATION_INTERVAL_SLOW;
3114
+ resolve(void 0);
3115
+ }, timeout);
3116
+ });
3117
+ if (geoPosition && !isGeoPositionEqual(geoPosition, prevGeoPosition))
3118
+ onSuccess(geoPosition);
3119
+ prevGeoPosition = geoPosition;
3120
+ await sleep$1(interval);
3121
+ } while (enable);
3122
+ };
3123
+ startWatch();
3124
+ return () => {
3125
+ enable = false;
3126
+ };
3127
+ }
3128
+ function compatibleWathPosition(onSuccess, onError, option) {
3129
+ const isForceBrowserGeo = takeIsForceBrowserGeo();
3130
+ if (isForceBrowserGeo) {
3131
+ const watchId = navigator.geolocation.watchPosition(onSuccess, onError, option);
3132
+ return () => navigator.geolocation.clearWatch(watchId);
3133
+ }
3134
+ switch (detectPlatform()) {
3135
+ case "wechat":
3136
+ return wechatWatchPosition(onSuccess, onError, option);
3137
+ default: {
3138
+ const watchId = navigator.geolocation.watchPosition(onSuccess, onError, option);
3139
+ return () => navigator.geolocation.clearWatch(watchId);
3140
+ }
3141
+ }
3142
+ }
3039
3143
  const isPlace = (place) => {
3040
3144
  return place.lng !== void 0 && place.lat !== void 0;
3041
3145
  };
@@ -3055,7 +3159,7 @@ class SingleGeoWatch {
3055
3159
  __publicField(this, "id", 0);
3056
3160
  __publicField(this, "listeners", /* @__PURE__ */ new Map());
3057
3161
  __publicField(this, "option", {});
3058
- __publicField(this, "singleWatchId");
3162
+ __publicField(this, "unwatch");
3059
3163
  this.option = option != null ? option : {};
3060
3164
  }
3061
3165
  watchPosition(onSuccess, onError) {
@@ -3069,12 +3173,12 @@ class SingleGeoWatch {
3069
3173
  this.listeners.delete(id);
3070
3174
  }
3071
3175
  async start() {
3072
- const { listeners, option, singleWatchId } = this;
3073
- if (singleWatchId) {
3074
- navigator.geolocation.clearWatch(singleWatchId);
3176
+ const { listeners, option, unwatch } = this;
3177
+ if (unwatch) {
3178
+ unwatch();
3075
3179
  await sleep(20);
3076
3180
  }
3077
- this.singleWatchId = navigator.geolocation.watchPosition(
3181
+ this.unwatch = compatibleWathPosition(
3078
3182
  (position) => {
3079
3183
  for (const [onSuccess] of listeners.values()) {
3080
3184
  onSuccess(position);
@@ -3145,10 +3249,12 @@ const useGeoLocation = (props) => {
3145
3249
  Vue.watch(
3146
3250
  () => true,
3147
3251
  (_1, _2, onCleanup) => {
3252
+ const logStartTime = Date.now();
3148
3253
  loading.value = true;
3149
3254
  const watchId = singleGeoWatch.watchPosition(
3150
3255
  async (position) => {
3151
- console.log("watchPosition success position = ", position);
3256
+ const duration = Date.now() - logStartTime;
3257
+ spaceLog("watchPosition", "success duration, position = ", duration, position);
3152
3258
  const coordinate = position.coords;
3153
3259
  const wgsPoint = [position.coords.longitude, position.coords.latitude];
3154
3260
  const point = await toGcj02(wgsPoint);
@@ -3162,7 +3268,7 @@ const useGeoLocation = (props) => {
3162
3268
  onChange == null ? void 0 : onChange({ position: point, coordinate });
3163
3269
  },
3164
3270
  (error) => {
3165
- console.log("watchPosition error = ", error);
3271
+ spaceLog("watchPosition", "error.message = ", error.message);
3166
3272
  loading.value = false;
3167
3273
  errorRef.value = error;
3168
3274
  const position = props == null ? void 0 : props.geoDefaultPosition;
@@ -3985,14 +4091,12 @@ const BusinessRecomendPlaceMap = defineLagecySetup(function BusinessRecomendPlac
3985
4091
  } = useGeoLocation({
3986
4092
  geoDefaultPosition,
3987
4093
  onLoad: async (value) => {
3988
- console.log("useGeoLocation onLoad value = ", value);
3989
4094
  await readyPromise;
3990
4095
  centerSource.source = "geo";
3991
4096
  updatePlace(value.position);
3992
4097
  emit("loadGeoLocation", value);
3993
4098
  },
3994
4099
  onLoadDefault: async (value) => {
3995
- console.log("useGeoLocation onLoadDefault value = ", value);
3996
4100
  await readyPromise;
3997
4101
  const place = await defaultCenterPlacePromise;
3998
4102
  centerSource.source = "geo";
@@ -4086,7 +4190,6 @@ const BusinessRecomendPlaceMap = defineLagecySetup(function BusinessRecomendPlac
4086
4190
  } = props;
4087
4191
  const title = geoLoading.value ? geoLoadingTitle : !availableRef.value ? unavailableTitle : centerPlace.name;
4088
4192
  const description = !availableRef.value ? void 0 : isElectedRef.value ? recomendDescription : void 0;
4089
- console.log("BusinessRecomendPlaceMap render geoLoading, isFirstWorkflowRecomendLoadingRef = ", geoLoading.value, isFirstWorkflowRecomendLoadingRef.value);
4090
4193
  return Vue.h(HeycarMap, {
4091
4194
  "attrs": {
4092
4195
  "center": centerPoint.value,
package/dist/index.js CHANGED
@@ -941,6 +941,11 @@ const isPlaceEqual = (p1, p2) => {
941
941
  const isPointEqual = (p1, p2) => {
942
942
  return p1[0] === (p2 == null ? void 0 : p2[0]) && p1[1] === (p2 == null ? void 0 : p2[1]);
943
943
  };
944
+ const isGeoPositionEqual = (p1, p2) => {
945
+ if (!p2)
946
+ return false;
947
+ return p1.coords.longitude === p2.coords.longitude && p1.coords.latitude === p2.coords.latitude;
948
+ };
944
949
  const toPlaceType = (maybePlace) => {
945
950
  const lng = Number(maybePlace.lng);
946
951
  if (isNaN(lng))
@@ -950,6 +955,21 @@ const toPlaceType = (maybePlace) => {
950
955
  throw new Error("MyError: expect lat to be number");
951
956
  return { lng, lat, name: maybePlace.name };
952
957
  };
958
+ const wxGetLocationSuccessResponse2GeolocationPosition = (res) => {
959
+ const { longitude, latitude, accuracy } = res;
960
+ const speed = Number(res.speed);
961
+ const timestamp = Date.now();
962
+ const coords = {
963
+ accuracy: Number(accuracy),
964
+ altitude: null,
965
+ altitudeAccuracy: null,
966
+ heading: null,
967
+ latitude: Number(latitude),
968
+ longitude: Number(longitude),
969
+ speed
970
+ };
971
+ return { timestamp, coords };
972
+ };
953
973
  const deepCompareEqualsForMaps = createComparator((deepEqual) => (a, b) => {
954
974
  if (isLatLngLiteral(a) || isLatLngLiteral(b)) {
955
975
  return new google.maps.LatLng(a).equals(new google.maps.LatLng(b));
@@ -3034,6 +3054,90 @@ const BusinessQuotingMap = defineLagecySetup(function BusinessQuotingMap2(props)
3034
3054
  })]);
3035
3055
  };
3036
3056
  }).props(["fallback", "from", "loading", "mapRef", "registerOverlay", "renderDescription", "to", "fromDescription", "log"]);
3057
+ const WX_GET_LOCATION_INTERVAL_STILL = 10 * 1e3;
3058
+ const WX_GET_LOCATION_INTERVAL_SLOW = 3 * 1e3;
3059
+ const WX_GET_LOCATION_INTERVAL_FAST = 1 * 1e3;
3060
+ const takeIsForceBrowserGeo = () => {
3061
+ const searchParam = new URLSearchParams(location.search);
3062
+ return searchParam.has(`force-browser-geo`);
3063
+ };
3064
+ const sleep$1 = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
3065
+ const wxReady = () => new Promise((resolve, reject) => {
3066
+ wx.ready(resolve);
3067
+ wx.error(reject);
3068
+ });
3069
+ const detectPlatform = () => {
3070
+ const useragent = navigator.userAgent.toLowerCase();
3071
+ if (useragent.includes("micromessenger"))
3072
+ return "wechat";
3073
+ if (useragent.includes("alipay"))
3074
+ return "alipay";
3075
+ return "other";
3076
+ };
3077
+ function wechatWatchPosition(onSuccess, onError, option) {
3078
+ const { timeout } = option;
3079
+ let enable = true;
3080
+ let interval = WX_GET_LOCATION_INTERVAL_STILL;
3081
+ let prevGeoPosition = void 0;
3082
+ const startWatch = async () => {
3083
+ await wxReady();
3084
+ do {
3085
+ const geoPosition = await new Promise((resolve, reject) => {
3086
+ let isHandled = false;
3087
+ wx.getLocation({
3088
+ type: "wgs84",
3089
+ success(res) {
3090
+ if (isHandled)
3091
+ return;
3092
+ isHandled = true;
3093
+ const speed = Number(res.speed);
3094
+ interval = speed === 0 ? WX_GET_LOCATION_INTERVAL_STILL : speed < 2 ? WX_GET_LOCATION_INTERVAL_SLOW : WX_GET_LOCATION_INTERVAL_FAST;
3095
+ resolve(wxGetLocationSuccessResponse2GeolocationPosition(res));
3096
+ },
3097
+ fail(res) {
3098
+ if (isHandled)
3099
+ return;
3100
+ isHandled = true;
3101
+ const error = new Error(res.errMsg);
3102
+ reject(error);
3103
+ onError == null ? void 0 : onError(error);
3104
+ }
3105
+ });
3106
+ if (!timeout)
3107
+ return;
3108
+ setTimeout(() => {
3109
+ if (isHandled)
3110
+ return;
3111
+ interval = WX_GET_LOCATION_INTERVAL_SLOW;
3112
+ resolve(void 0);
3113
+ }, timeout);
3114
+ });
3115
+ if (geoPosition && !isGeoPositionEqual(geoPosition, prevGeoPosition))
3116
+ onSuccess(geoPosition);
3117
+ prevGeoPosition = geoPosition;
3118
+ await sleep$1(interval);
3119
+ } while (enable);
3120
+ };
3121
+ startWatch();
3122
+ return () => {
3123
+ enable = false;
3124
+ };
3125
+ }
3126
+ function compatibleWathPosition(onSuccess, onError, option) {
3127
+ const isForceBrowserGeo = takeIsForceBrowserGeo();
3128
+ if (isForceBrowserGeo) {
3129
+ const watchId = navigator.geolocation.watchPosition(onSuccess, onError, option);
3130
+ return () => navigator.geolocation.clearWatch(watchId);
3131
+ }
3132
+ switch (detectPlatform()) {
3133
+ case "wechat":
3134
+ return wechatWatchPosition(onSuccess, onError, option);
3135
+ default: {
3136
+ const watchId = navigator.geolocation.watchPosition(onSuccess, onError, option);
3137
+ return () => navigator.geolocation.clearWatch(watchId);
3138
+ }
3139
+ }
3140
+ }
3037
3141
  const isPlace = (place) => {
3038
3142
  return place.lng !== void 0 && place.lat !== void 0;
3039
3143
  };
@@ -3053,7 +3157,7 @@ class SingleGeoWatch {
3053
3157
  __publicField(this, "id", 0);
3054
3158
  __publicField(this, "listeners", /* @__PURE__ */ new Map());
3055
3159
  __publicField(this, "option", {});
3056
- __publicField(this, "singleWatchId");
3160
+ __publicField(this, "unwatch");
3057
3161
  this.option = option != null ? option : {};
3058
3162
  }
3059
3163
  watchPosition(onSuccess, onError) {
@@ -3067,12 +3171,12 @@ class SingleGeoWatch {
3067
3171
  this.listeners.delete(id);
3068
3172
  }
3069
3173
  async start() {
3070
- const { listeners, option, singleWatchId } = this;
3071
- if (singleWatchId) {
3072
- navigator.geolocation.clearWatch(singleWatchId);
3174
+ const { listeners, option, unwatch } = this;
3175
+ if (unwatch) {
3176
+ unwatch();
3073
3177
  await sleep(20);
3074
3178
  }
3075
- this.singleWatchId = navigator.geolocation.watchPosition(
3179
+ this.unwatch = compatibleWathPosition(
3076
3180
  (position) => {
3077
3181
  for (const [onSuccess] of listeners.values()) {
3078
3182
  onSuccess(position);
@@ -3143,10 +3247,12 @@ const useGeoLocation = (props) => {
3143
3247
  watch(
3144
3248
  () => true,
3145
3249
  (_1, _2, onCleanup) => {
3250
+ const logStartTime = Date.now();
3146
3251
  loading.value = true;
3147
3252
  const watchId = singleGeoWatch.watchPosition(
3148
3253
  async (position) => {
3149
- console.log("watchPosition success position = ", position);
3254
+ const duration = Date.now() - logStartTime;
3255
+ spaceLog("watchPosition", "success duration, position = ", duration, position);
3150
3256
  const coordinate = position.coords;
3151
3257
  const wgsPoint = [position.coords.longitude, position.coords.latitude];
3152
3258
  const point = await toGcj02(wgsPoint);
@@ -3160,7 +3266,7 @@ const useGeoLocation = (props) => {
3160
3266
  onChange == null ? void 0 : onChange({ position: point, coordinate });
3161
3267
  },
3162
3268
  (error) => {
3163
- console.log("watchPosition error = ", error);
3269
+ spaceLog("watchPosition", "error.message = ", error.message);
3164
3270
  loading.value = false;
3165
3271
  errorRef.value = error;
3166
3272
  const position = props == null ? void 0 : props.geoDefaultPosition;
@@ -3983,14 +4089,12 @@ const BusinessRecomendPlaceMap = defineLagecySetup(function BusinessRecomendPlac
3983
4089
  } = useGeoLocation({
3984
4090
  geoDefaultPosition,
3985
4091
  onLoad: async (value) => {
3986
- console.log("useGeoLocation onLoad value = ", value);
3987
4092
  await readyPromise;
3988
4093
  centerSource.source = "geo";
3989
4094
  updatePlace(value.position);
3990
4095
  emit("loadGeoLocation", value);
3991
4096
  },
3992
4097
  onLoadDefault: async (value) => {
3993
- console.log("useGeoLocation onLoadDefault value = ", value);
3994
4098
  await readyPromise;
3995
4099
  const place = await defaultCenterPlacePromise;
3996
4100
  centerSource.source = "geo";
@@ -4084,7 +4188,6 @@ const BusinessRecomendPlaceMap = defineLagecySetup(function BusinessRecomendPlac
4084
4188
  } = props;
4085
4189
  const title = geoLoading.value ? geoLoadingTitle : !availableRef.value ? unavailableTitle : centerPlace.name;
4086
4190
  const description = !availableRef.value ? void 0 : isElectedRef.value ? recomendDescription : void 0;
4087
- console.log("BusinessRecomendPlaceMap render geoLoading, isFirstWorkflowRecomendLoadingRef = ", geoLoading.value, isFirstWorkflowRecomendLoadingRef.value);
4088
4191
  return h(HeycarMap, {
4089
4192
  "attrs": {
4090
4193
  "center": centerPoint.value,
@@ -0,0 +1,21 @@
1
+ export interface WxResponse {
2
+ errMsg: string;
3
+ }
4
+ export interface WxGetLocationSuccessResponse extends WxResponse {
5
+ latitude: string;
6
+ longitude: string;
7
+ speed: string;
8
+ accuracy: string;
9
+ }
10
+ interface WxGetLocationProps {
11
+ type: "wgs84" | "gcj02";
12
+ success: (response: WxGetLocationSuccessResponse) => void;
13
+ fail?: (response: WxResponse) => void;
14
+ complete?: (response: WxResponse) => void;
15
+ }
16
+ export interface Wx {
17
+ ready: (callback: () => void) => void;
18
+ error: (callback: (response: WxResponse) => void) => void;
19
+ getLocation: (props: WxGetLocationProps) => void;
20
+ }
21
+ export {};
@@ -1,3 +1,4 @@
1
+ import { type ClearWathPosition } from "./compatibleWatchPosition";
1
2
  /**
2
3
  * @note navigator.geolocation.watchPosition 期间,再调用 watchPosition 或 getCurrentPosition 会导致定位失败。
3
4
  */
@@ -5,7 +6,7 @@ export declare class SingleGeoWatch {
5
6
  id: number;
6
7
  listeners: Map<number, [PositionCallback, PositionErrorCallback | undefined]>;
7
8
  option: PositionOptions;
8
- singleWatchId: number | undefined;
9
+ unwatch: ClearWathPosition | undefined;
9
10
  constructor(option?: PositionOptions);
10
11
  watchPosition(onSuccess: PositionCallback, onError?: PositionErrorCallback): number;
11
12
  clearWatch(id: number): void;
@@ -0,0 +1,2 @@
1
+ export type ClearWathPosition = () => void;
2
+ export declare function compatibleWathPosition(onSuccess: PositionCallback, onError: PositionErrorCallback, option: PositionOptions): ClearWathPosition;
@@ -1,5 +1,6 @@
1
1
  /// <reference types="google.maps" />
2
2
  import type { Place, Point } from "../types/interface";
3
+ import type { WxGetLocationSuccessResponse } from "../types/wx";
3
4
  export declare const vec2lnglat: ([lng, lat]: [number, number]) => google.maps.LatLngLiteral;
4
5
  export interface AsteriskItem {
5
6
  type: "normal" | "emphasize";
@@ -16,8 +17,10 @@ export declare const pipeDefer: <P extends any[], R>(fn: (...args: P) => R) => (
16
17
  export declare const pipeOnlyLastEffect: <P extends any[], R>(fn: (...arg: P) => Promise<R>) => (...args: P) => Promise<R>;
17
18
  export declare const isPlaceEqual: (p1: Place, p2?: Place) => boolean;
18
19
  export declare const isPointEqual: (p1: Point, p2?: Point) => boolean;
20
+ export declare const isGeoPositionEqual: (p1: GeolocationPosition, p2?: GeolocationPosition) => boolean;
19
21
  export declare const toPlaceType: (maybePlace: {
20
22
  name: string;
21
23
  lng: string | number;
22
24
  lat: string | number;
23
25
  }) => Place;
26
+ export declare const wxGetLocationSuccessResponse2GeolocationPosition: (res: WxGetLocationSuccessResponse) => GeolocationPosition;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@heycar/heycars-map",
3
- "version": "0.6.5",
3
+ "version": "0.6.6",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "dev": "vite -c vite.config.dev.ts",