@lugg/maps 0.2.0-alpha.5 → 0.2.0-alpha.7

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.
Files changed (61) hide show
  1. package/README.md +17 -4
  2. package/android/src/main/java/com/luggmaps/LuggGoogleMapView.kt +2 -5
  3. package/android/src/main/java/com/luggmaps/LuggMarkerView.kt +7 -1
  4. package/android/src/main/java/com/luggmaps/LuggMarkerViewManager.kt +6 -0
  5. package/ios/LuggAppleMapView.mm +8 -0
  6. package/ios/LuggGoogleMapView.mm +2 -0
  7. package/ios/LuggMarkerView.h +2 -0
  8. package/ios/LuggMarkerView.mm +13 -0
  9. package/lib/module/MapProvider.js +13 -0
  10. package/lib/module/MapProvider.js.map +1 -0
  11. package/lib/module/MapProvider.types.js +4 -0
  12. package/lib/module/MapProvider.types.js.map +1 -0
  13. package/lib/module/MapProvider.web.js +14 -0
  14. package/lib/module/MapProvider.web.js.map +1 -0
  15. package/lib/module/MapView.web.js +212 -0
  16. package/lib/module/MapView.web.js.map +1 -0
  17. package/lib/module/components/Marker.js +4 -1
  18. package/lib/module/components/Marker.js.map +1 -1
  19. package/lib/module/components/Marker.web.js +34 -0
  20. package/lib/module/components/Marker.web.js.map +1 -0
  21. package/lib/module/components/Polyline.web.js +171 -0
  22. package/lib/module/components/Polyline.web.js.map +1 -0
  23. package/lib/module/components/index.js +2 -2
  24. package/lib/module/components/index.js.map +1 -1
  25. package/lib/module/components/index.web.js +5 -0
  26. package/lib/module/components/index.web.js.map +1 -0
  27. package/lib/module/index.js +3 -2
  28. package/lib/module/index.js.map +1 -1
  29. package/lib/module/index.web.js +6 -0
  30. package/lib/module/index.web.js.map +1 -0
  31. package/lib/typescript/src/MapProvider.d.ts +8 -0
  32. package/lib/typescript/src/MapProvider.d.ts.map +1 -0
  33. package/lib/typescript/src/MapProvider.types.d.ts +16 -0
  34. package/lib/typescript/src/MapProvider.types.d.ts.map +1 -0
  35. package/lib/typescript/src/MapProvider.web.d.ts +3 -0
  36. package/lib/typescript/src/MapProvider.web.d.ts.map +1 -0
  37. package/lib/typescript/src/MapView.web.d.ts +12 -0
  38. package/lib/typescript/src/MapView.web.d.ts.map +1 -0
  39. package/lib/typescript/src/components/Marker.d.ts +4 -0
  40. package/lib/typescript/src/components/Marker.d.ts.map +1 -1
  41. package/lib/typescript/src/components/Marker.web.d.ts +6 -0
  42. package/lib/typescript/src/components/Marker.web.d.ts.map +1 -0
  43. package/lib/typescript/src/components/Polyline.web.d.ts +6 -0
  44. package/lib/typescript/src/components/Polyline.web.d.ts.map +1 -0
  45. package/lib/typescript/src/components/index.web.d.ts +5 -0
  46. package/lib/typescript/src/components/index.web.d.ts.map +1 -0
  47. package/lib/typescript/src/index.d.ts +3 -1
  48. package/lib/typescript/src/index.d.ts.map +1 -1
  49. package/lib/typescript/src/index.web.d.ts +7 -0
  50. package/lib/typescript/src/index.web.d.ts.map +1 -0
  51. package/package.json +13 -1
  52. package/src/MapProvider.tsx +10 -0
  53. package/src/MapProvider.types.ts +16 -0
  54. package/src/MapProvider.web.tsx +6 -0
  55. package/src/MapView.web.tsx +255 -0
  56. package/src/components/Marker.tsx +6 -2
  57. package/src/components/Marker.web.tsx +32 -0
  58. package/src/components/Polyline.web.tsx +206 -0
  59. package/src/components/index.web.ts +4 -0
  60. package/src/index.ts +8 -1
  61. package/src/index.web.ts +17 -0
@@ -0,0 +1,255 @@
1
+ import React from 'react';
2
+ import type { NativeSyntheticEvent, ViewStyle } from 'react-native';
3
+ import { View } from 'react-native';
4
+ import { Map, useMap } from '@vis.gl/react-google-maps';
5
+ import { Marker } from './components/Marker.web';
6
+ import { Polyline } from './components/Polyline.web';
7
+
8
+ import type {
9
+ MapViewProps,
10
+ MapViewRef,
11
+ MoveCameraOptions,
12
+ FitCoordinatesOptions,
13
+ CameraEventPayload,
14
+ } from './MapView.types';
15
+ import type { Coordinate } from './types';
16
+
17
+ // Map-specific component types that render inside the Google Map
18
+ const MAP_COMPONENT_TYPES = new Set([Marker, Polyline]);
19
+
20
+ const isMapComponent = (child: React.ReactElement): boolean =>
21
+ MAP_COMPONENT_TYPES.has(child.type as typeof Marker | typeof Polyline);
22
+
23
+ const createSyntheticEvent = <T,>(nativeEvent: T): NativeSyntheticEvent<T> =>
24
+ ({
25
+ nativeEvent,
26
+ currentTarget: null,
27
+ target: null,
28
+ bubbles: false,
29
+ cancelable: false,
30
+ defaultPrevented: false,
31
+ eventPhase: 0,
32
+ isTrusted: true,
33
+ preventDefault: () => {},
34
+ stopPropagation: () => {},
35
+ isDefaultPrevented: () => false,
36
+ isPropagationStopped: () => false,
37
+ persist: () => {},
38
+ timeStamp: Date.now(),
39
+ type: '',
40
+ } as unknown as NativeSyntheticEvent<T>);
41
+
42
+ interface MapControllerProps {
43
+ onMapReady: (map: google.maps.Map) => void;
44
+ onCameraMove?: (event: NativeSyntheticEvent<CameraEventPayload>) => void;
45
+ onCameraIdle?: (event: NativeSyntheticEvent<CameraEventPayload>) => void;
46
+ onReady?: () => void;
47
+ }
48
+
49
+ function MapController({
50
+ onMapReady,
51
+ onCameraMove,
52
+ onCameraIdle,
53
+ onReady,
54
+ }: MapControllerProps) {
55
+ const map = useMap();
56
+ const readyFired = React.useRef(false);
57
+
58
+ React.useEffect(() => {
59
+ if (!map) return;
60
+ onMapReady(map);
61
+
62
+ if (!readyFired.current) {
63
+ readyFired.current = true;
64
+ onReady?.();
65
+ }
66
+ }, [map, onMapReady, onReady]);
67
+
68
+ React.useEffect(() => {
69
+ if (!map) return;
70
+
71
+ const createPayload = (gesture: boolean): CameraEventPayload => {
72
+ const center = map.getCenter();
73
+ return {
74
+ coordinate: {
75
+ latitude: center?.lat() ?? 0,
76
+ longitude: center?.lng() ?? 0,
77
+ },
78
+ zoom: map.getZoom() ?? 0,
79
+ gesture,
80
+ };
81
+ };
82
+
83
+ let isDragging = false;
84
+
85
+ const dragStartListener = map.addListener('dragstart', () => {
86
+ isDragging = true;
87
+ });
88
+
89
+ const dragEndListener = map.addListener('dragend', () => {
90
+ isDragging = false;
91
+ });
92
+
93
+ const centerListener = map.addListener('center_changed', () => {
94
+ onCameraMove?.(createSyntheticEvent(createPayload(isDragging)));
95
+ });
96
+
97
+ const idleListener = map.addListener('idle', () => {
98
+ onCameraIdle?.(createSyntheticEvent(createPayload(false)));
99
+ });
100
+
101
+ return () => {
102
+ google.maps.event.removeListener(dragStartListener);
103
+ google.maps.event.removeListener(dragEndListener);
104
+ google.maps.event.removeListener(centerListener);
105
+ google.maps.event.removeListener(idleListener);
106
+ };
107
+ }, [map, onCameraMove, onCameraIdle]);
108
+
109
+ return null;
110
+ }
111
+
112
+ export class MapView
113
+ extends React.Component<MapViewProps>
114
+ implements MapViewRef
115
+ {
116
+ static defaultProps: Partial<MapViewProps> = {
117
+ provider: 'google',
118
+ initialZoom: 10,
119
+ zoomEnabled: true,
120
+ scrollEnabled: true,
121
+ rotateEnabled: true,
122
+ pitchEnabled: true,
123
+ };
124
+
125
+ private mapInstance: google.maps.Map | null = null;
126
+
127
+ private handleMapReady = (map: google.maps.Map) => {
128
+ this.mapInstance = map;
129
+ };
130
+
131
+ moveCamera(coordinate: Coordinate, options: MoveCameraOptions) {
132
+ const map = this.mapInstance;
133
+ if (!map) return;
134
+
135
+ const { zoom, duration = -1 } = options;
136
+ const center = { lat: coordinate.latitude, lng: coordinate.longitude };
137
+
138
+ if (duration > 0) {
139
+ map.panTo(center);
140
+ map.setZoom(zoom);
141
+ } else {
142
+ map.setCenter(center);
143
+ map.setZoom(zoom);
144
+ }
145
+ }
146
+
147
+ fitCoordinates(coordinates: Coordinate[], options?: FitCoordinatesOptions) {
148
+ const map = this.mapInstance;
149
+ const first = coordinates[0];
150
+ if (!map || !first) return;
151
+
152
+ const { padding, duration = -1 } = options ?? {};
153
+
154
+ if (coordinates.length === 1) {
155
+ const zoom = this.props.initialZoom ?? 10;
156
+ this.moveCamera(first, { zoom, duration });
157
+ return;
158
+ }
159
+
160
+ const bounds = new google.maps.LatLngBounds();
161
+ coordinates.forEach((coord) => {
162
+ bounds.extend({ lat: coord.latitude, lng: coord.longitude });
163
+ });
164
+
165
+ map.fitBounds(bounds, {
166
+ top: padding?.top ?? 0,
167
+ left: padding?.left ?? 0,
168
+ bottom: padding?.bottom ?? 0,
169
+ right: padding?.right ?? 0,
170
+ });
171
+ }
172
+
173
+ render() {
174
+ const {
175
+ mapId,
176
+ initialCoordinate,
177
+ initialZoom,
178
+ minZoom,
179
+ maxZoom,
180
+ zoomEnabled,
181
+ scrollEnabled,
182
+ pitchEnabled,
183
+ padding,
184
+ onCameraMove,
185
+ onCameraIdle,
186
+ onReady,
187
+ children,
188
+ style,
189
+ } = this.props;
190
+
191
+ const gestureHandling =
192
+ scrollEnabled === false && zoomEnabled === false
193
+ ? 'none'
194
+ : scrollEnabled === false
195
+ ? 'none'
196
+ : 'auto';
197
+
198
+ const defaultCenter = initialCoordinate
199
+ ? { lat: initialCoordinate.latitude, lng: initialCoordinate.longitude }
200
+ : undefined;
201
+
202
+ // Separate map children (Marker, Polyline) from overlay children (regular Views)
203
+ const mapChildren: React.ReactNode[] = [];
204
+ const overlayChildren: React.ReactNode[] = [];
205
+
206
+ React.Children.forEach(children, (child) => {
207
+ if (!React.isValidElement(child)) return;
208
+ if (isMapComponent(child)) {
209
+ mapChildren.push(child);
210
+ } else {
211
+ overlayChildren.push(child);
212
+ }
213
+ });
214
+
215
+ const mapContainerStyle: ViewStyle = {
216
+ position: 'absolute',
217
+ top: padding?.top ?? 0,
218
+ left: padding?.left ?? 0,
219
+ right: padding?.right ?? 0,
220
+ bottom: padding?.bottom ?? 0,
221
+ };
222
+
223
+ const mapStyle: React.CSSProperties = {
224
+ width: '100%',
225
+ height: '100%',
226
+ };
227
+
228
+ return (
229
+ <View style={style}>
230
+ <View style={mapContainerStyle}>
231
+ <Map
232
+ mapId={mapId}
233
+ defaultCenter={defaultCenter}
234
+ defaultZoom={initialZoom}
235
+ minZoom={minZoom}
236
+ maxZoom={maxZoom}
237
+ gestureHandling={gestureHandling}
238
+ disableDefaultUI
239
+ tilt={pitchEnabled === false ? 0 : undefined}
240
+ style={mapStyle}
241
+ >
242
+ <MapController
243
+ onMapReady={this.handleMapReady}
244
+ onCameraMove={onCameraMove}
245
+ onCameraIdle={onCameraIdle}
246
+ onReady={onReady}
247
+ />
248
+ {mapChildren}
249
+ </Map>
250
+ </View>
251
+ {overlayChildren}
252
+ </View>
253
+ );
254
+ }
255
+ }
@@ -25,6 +25,10 @@ export interface MarkerProps {
25
25
  * Anchor point for custom marker views
26
26
  */
27
27
  anchor?: Point;
28
+ /**
29
+ * Z-index for marker ordering. Higher values render on top.
30
+ */
31
+ zIndex?: number;
28
32
  /**
29
33
  * Custom marker view
30
34
  */
@@ -33,12 +37,12 @@ export interface MarkerProps {
33
37
 
34
38
  export class Marker extends React.Component<MarkerProps> {
35
39
  render() {
36
- const { name, coordinate, title, description, anchor, children } =
40
+ const { name, coordinate, title, description, anchor, zIndex, children } =
37
41
  this.props;
38
42
 
39
43
  return (
40
44
  <LuggMarkerViewNativeComponent
41
- style={styles.marker}
45
+ style={[{ zIndex }, styles.marker]}
42
46
  name={name}
43
47
  coordinate={coordinate}
44
48
  title={title}
@@ -0,0 +1,32 @@
1
+ import React from 'react';
2
+ import { AdvancedMarker } from '@vis.gl/react-google-maps';
3
+ import type { MarkerProps } from './Marker';
4
+
5
+ /**
6
+ * Converts point to % anchor for web.
7
+ * e.g. `0.5` to `-50%`
8
+ */
9
+ const toWebAnchor = (value: number) => `-${value * 100}%`;
10
+
11
+ export class Marker extends React.Component<MarkerProps> {
12
+ render() {
13
+ const { coordinate, title, anchor, zIndex, children } = this.props;
14
+
15
+ const position = {
16
+ lat: coordinate.latitude,
17
+ lng: coordinate.longitude,
18
+ };
19
+
20
+ return (
21
+ <AdvancedMarker
22
+ position={position}
23
+ title={title}
24
+ zIndex={zIndex}
25
+ anchorLeft={anchor ? toWebAnchor(anchor.x) : undefined}
26
+ anchorTop={anchor ? toWebAnchor(anchor.y) : undefined}
27
+ >
28
+ {children}
29
+ </AdvancedMarker>
30
+ );
31
+ }
32
+ }
@@ -0,0 +1,206 @@
1
+ import {
2
+ Component,
3
+ useCallback,
4
+ useEffect,
5
+ useMemo,
6
+ useRef,
7
+ useState,
8
+ } from 'react';
9
+ import { useMap } from '@vis.gl/react-google-maps';
10
+ import type { PolylineProps } from './Polyline';
11
+
12
+ const ANIMATION_DURATION = 1500;
13
+
14
+ function interpolateColor(color1: string, color2: string, t: number): string {
15
+ const hex = (c: string) => parseInt(c, 16);
16
+ const r1 = hex(color1.slice(1, 3));
17
+ const g1 = hex(color1.slice(3, 5));
18
+ const b1 = hex(color1.slice(5, 7));
19
+ const r2 = hex(color2.slice(1, 3));
20
+ const g2 = hex(color2.slice(3, 5));
21
+ const b2 = hex(color2.slice(5, 7));
22
+
23
+ const r = Math.round(r1 + (r2 - r1) * t);
24
+ const g = Math.round(g1 + (g2 - g1) * t);
25
+ const b = Math.round(b1 + (b2 - b1) * t);
26
+
27
+ return `#${r.toString(16).padStart(2, '0')}${g
28
+ .toString(16)
29
+ .padStart(2, '0')}${b.toString(16).padStart(2, '0')}`;
30
+ }
31
+
32
+ function getGradientColor(colors: string[], position: number): string {
33
+ if (colors.length === 0) return '#000000';
34
+ if (colors.length === 1 || position <= 0) return colors[0]!;
35
+ if (position >= 1) return colors[colors.length - 1]!;
36
+
37
+ const scaledPos = position * (colors.length - 1);
38
+ const index = Math.floor(scaledPos);
39
+ const t = scaledPos - index;
40
+
41
+ return interpolateColor(colors[index]!, colors[index + 1]!, t);
42
+ }
43
+
44
+ function PolylineImpl({
45
+ coordinates,
46
+ strokeColors,
47
+ strokeWidth = 1,
48
+ animated,
49
+ }: PolylineProps) {
50
+ const map = useMap();
51
+ const polylinesRef = useRef<google.maps.Polyline[]>([]);
52
+ const animationRef = useRef<number>(0);
53
+
54
+ const colors = useMemo(
55
+ () =>
56
+ strokeColors && strokeColors.length > 0
57
+ ? (strokeColors as string[])
58
+ : ['#000000'],
59
+ [strokeColors]
60
+ );
61
+
62
+ const hasGradient = colors.length > 1;
63
+
64
+ // Refs for animation loop access
65
+ const propsRef = useRef({ map, colors, strokeWidth, hasGradient });
66
+ const [mapReady, setMapReady] = useState(!!map);
67
+
68
+ useEffect(() => {
69
+ propsRef.current = { map, colors, strokeWidth, hasGradient };
70
+ if (map && !mapReady) setMapReady(true);
71
+ }, [map, colors, strokeWidth, hasGradient, mapReady]);
72
+
73
+ const updatePath = useCallback((path: google.maps.LatLngLiteral[]) => {
74
+ const {
75
+ map: currentMap,
76
+ colors: currentColors,
77
+ strokeWidth: currentStrokeWidth,
78
+ hasGradient: currentHasGradient,
79
+ } = propsRef.current;
80
+ if (!currentMap || path.length < 2) return;
81
+
82
+ const neededSegments = currentHasGradient ? path.length - 1 : 1;
83
+ const existing = polylinesRef.current;
84
+
85
+ // Update or create segments
86
+ for (let i = 0; i < neededSegments; i++) {
87
+ const segmentPath = currentHasGradient ? [path[i]!, path[i + 1]!] : path;
88
+ const color = currentHasGradient
89
+ ? getGradientColor(currentColors, i / (path.length - 1))
90
+ : currentColors[0]!;
91
+
92
+ const segment = existing[i];
93
+ if (segment) {
94
+ segment.setPath(segmentPath);
95
+ segment.setOptions({ strokeColor: color });
96
+ } else {
97
+ existing.push(
98
+ new google.maps.Polyline({
99
+ path: segmentPath,
100
+ strokeColor: color,
101
+ strokeWeight: currentStrokeWidth,
102
+ strokeOpacity: 1,
103
+ map: currentMap,
104
+ })
105
+ );
106
+ }
107
+ }
108
+
109
+ // Remove extra segments
110
+ for (let i = neededSegments; i < existing.length; i++) {
111
+ existing[i]?.setMap(null);
112
+ }
113
+ existing.length = neededSegments;
114
+ }, []);
115
+
116
+ // Cleanup on unmount
117
+ useEffect(() => {
118
+ const polylines = polylinesRef.current;
119
+ return () => {
120
+ cancelAnimationFrame(animationRef.current);
121
+ polylines.forEach((p) => p.setMap(null));
122
+ };
123
+ }, []);
124
+
125
+ // Main effect
126
+ useEffect(() => {
127
+ if (!propsRef.current.map || coordinates.length === 0) return;
128
+
129
+ const fullPath = coordinates.map((c) => ({
130
+ lat: c.latitude,
131
+ lng: c.longitude,
132
+ }));
133
+
134
+ cancelAnimationFrame(animationRef.current);
135
+
136
+ if (!animated) {
137
+ updatePath(fullPath);
138
+ return;
139
+ }
140
+
141
+ const totalPoints = fullPath.length;
142
+ const cycleDuration = ANIMATION_DURATION * 2;
143
+
144
+ const animate = (time: number) => {
145
+ const progress = (time % cycleDuration) / ANIMATION_DURATION;
146
+ const startIdx = progress <= 1 ? 0 : (progress - 1) * totalPoints;
147
+ const endIdx = progress <= 1 ? progress * totalPoints : totalPoints;
148
+
149
+ const partialPath: google.maps.LatLngLiteral[] = [];
150
+ const startFloor = Math.floor(startIdx);
151
+ const endFloor = Math.floor(endIdx);
152
+
153
+ // Start point (interpolated)
154
+ if (startFloor < totalPoints) {
155
+ const frac = startIdx - startFloor;
156
+ const from = fullPath[startFloor]!;
157
+ const to = fullPath[Math.min(startFloor + 1, totalPoints - 1)]!;
158
+ partialPath.push(
159
+ frac > 0
160
+ ? {
161
+ lat: from.lat + (to.lat - from.lat) * frac,
162
+ lng: from.lng + (to.lng - from.lng) * frac,
163
+ }
164
+ : from
165
+ );
166
+ }
167
+
168
+ // Middle points
169
+ for (
170
+ let i = startFloor + 1;
171
+ i <= Math.min(endFloor, totalPoints - 1);
172
+ i++
173
+ ) {
174
+ partialPath.push(fullPath[i]!);
175
+ }
176
+
177
+ // End point (interpolated)
178
+ if (endFloor < totalPoints - 1) {
179
+ const frac = endIdx - endFloor;
180
+ const from = fullPath[endFloor]!;
181
+ const to = fullPath[endFloor + 1]!;
182
+ if (frac > 0) {
183
+ partialPath.push({
184
+ lat: from.lat + (to.lat - from.lat) * frac,
185
+ lng: from.lng + (to.lng - from.lng) * frac,
186
+ });
187
+ }
188
+ }
189
+
190
+ updatePath(partialPath);
191
+ animationRef.current = requestAnimationFrame(animate);
192
+ };
193
+
194
+ animationRef.current = requestAnimationFrame(animate);
195
+
196
+ return () => cancelAnimationFrame(animationRef.current);
197
+ }, [coordinates, animated, hasGradient, updatePath, mapReady]);
198
+
199
+ return null;
200
+ }
201
+
202
+ export class Polyline extends Component<PolylineProps> {
203
+ render() {
204
+ return <PolylineImpl {...this.props} />;
205
+ }
206
+ }
@@ -0,0 +1,4 @@
1
+ export { Marker } from './Marker.web';
2
+ export { Polyline } from './Polyline.web';
3
+ export type { MarkerProps } from './Marker';
4
+ export type { PolylineProps } from './Polyline';
package/src/index.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export { MapView } from './MapView';
2
+ export { MapProvider } from './MapProvider';
3
+ export type { MapProviderProps } from './MapProvider.types';
2
4
  export * from './components';
3
5
  export type {
4
6
  MapViewProps,
@@ -7,4 +9,9 @@ export type {
7
9
  FitCoordinatesOptions,
8
10
  CameraEventPayload,
9
11
  } from './MapView.types';
10
- export type { MapProvider, Coordinate, Point, EdgeInsets } from './types';
12
+ export type {
13
+ MapProvider as MapProviderType,
14
+ Coordinate,
15
+ Point,
16
+ EdgeInsets,
17
+ } from './types';
@@ -0,0 +1,17 @@
1
+ export { MapView } from './MapView.web';
2
+ export { MapProvider } from './MapProvider.web';
3
+ export type { MapProviderProps } from './MapProvider.types';
4
+ export * from './components/index.web';
5
+ export type {
6
+ MapViewProps,
7
+ MapViewRef,
8
+ MoveCameraOptions,
9
+ FitCoordinatesOptions,
10
+ CameraEventPayload,
11
+ } from './MapView.types';
12
+ export type {
13
+ MapProvider as MapProviderType,
14
+ Coordinate,
15
+ Point,
16
+ EdgeInsets,
17
+ } from './types';