@ttoss/google-maps 0.6.1 → 0.8.4

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/README.md CHANGED
@@ -1,8 +1,4 @@
1
- <h1 align="center">
2
-
3
- `@ttoss/google-maps`
4
-
5
- </h1>
1
+ # @ttoss/google-maps
6
2
 
7
3
  ## 📚 About
8
4
 
@@ -10,18 +6,235 @@
10
6
 
11
7
  ## 🚀 Get Started
12
8
 
13
- - Install Dependencies
9
+ ### Install @ttoss/google-maps
14
10
 
15
- ```sh
16
- $ yarn
11
+ ```shell
12
+ $ yarn add
13
+ # or
14
+ $ npm install @ttoss/google-maps
17
15
  ```
18
16
 
19
- - Build @ttoss/google-maps
17
+ ## 📄 Examples of use
20
18
 
21
- ```sh
22
- $ yarn build
23
- # or
24
- $ npm run build
19
+ ### Map
20
+
21
+ ```tsx
22
+ export type MapProps = {
23
+ width?: number | string;
24
+ height?: number | string;
25
+ image?: Omit<MapImageOptions, 'map'>;
26
+ options?: google.maps.MapOptions;
27
+ };
28
+
29
+ const Map = ({ height, width, image, options }: MapProps) => {
30
+ const { googleMaps } = useGoogleMaps();
31
+
32
+ const [mapDivRef, setMapDivRef] = React.useState<HTMLDivElement | null>(null);
33
+
34
+ const map = React.useMemo(() => {
35
+ if (googleMaps && mapDivRef) {
36
+ const googleMapsMap = new googleMaps.Map(mapDivRef, defaultOptions);
37
+ return googleMapsMap;
38
+ }
39
+
40
+ return null;
41
+ }, [googleMaps, mapDivRef]);
42
+
43
+ React.useEffect(() => {
44
+ if (map && options?.center) {
45
+ map.setCenter(options.center);
46
+ }
47
+ }, [map, options?.center]);
48
+
49
+ return (
50
+ <div id="google-maps" ref={(r) => setMapDivRef(r)} sx={{ width, height }} />
51
+ );
52
+ };
53
+
54
+ Map.defaultProps = {
55
+ width: 400,
56
+ height: 300,
57
+ } as MapProps;
58
+
59
+ export default Map;
60
+ ```
61
+
62
+ ### Latitude and Longitude
63
+
64
+ ```ts
65
+ import { useGeocoder } from '@ttoss/google-maps';
66
+
67
+ type MapGeocoderResult = google.maps.GeocoderResult;
68
+ type MapGeocoderStatus = google.maps.GeocoderStatus;
69
+
70
+ export type LatLng = {
71
+ lat: number;
72
+ lng: number;
73
+ };
74
+
75
+ export const useGeocoding = () => {
76
+ const { geocoder } = useGeocoder();
77
+
78
+ const getLatLong = ({ address }: { address: string }): Promise<LatLng> => {
79
+ return new Promise((resolve, reject) => {
80
+ if (!geocoder) {
81
+ return reject(new Error('Geocoder not defined'));
82
+ }
83
+
84
+ geocoder.geocode(
85
+ { address },
86
+ (results: MapGeocoderResult[] | null, status: MapGeocoderStatus) => {
87
+ if (results && status === window.google.maps.GeocoderStatus.OK) {
88
+ const lat = results[0].geometry.location.lat();
89
+ const lng = results[0].geometry.location.lng();
90
+
91
+ return resolve({
92
+ lat,
93
+ lng,
94
+ });
95
+ } else {
96
+ console.log(
97
+ 'Geocode was not successful for the following reason: ',
98
+ status
99
+ );
100
+ return reject(new Error('Result not found'));
101
+ }
102
+ }
103
+ );
104
+ });
105
+ };
106
+
107
+ return { getLatLong };
108
+ };
109
+ ```
110
+
111
+ ### Providers
112
+
113
+ ```tsx
114
+ import * as React from 'react';
115
+
116
+ import { GoogleMapsProvider } from '@ttoss/google-maps';
117
+
118
+ const Providers: React.FC = ({ children }) => {
119
+ const apiKey = 'YOUR_API_KEY';
120
+
121
+ if (!apiKey) {
122
+ return <>{children}</>;
123
+ }
124
+
125
+ return (
126
+ <GoogleMapsProvider apiKey={apiKey} libraries={['places']}>
127
+ {children}
128
+ </GoogleMapsProvider>
129
+ );
130
+ };
131
+
132
+ export default Providers;
133
+ ```
134
+
135
+ ### Autocomplete
136
+
137
+ ```tsx
138
+ import React from 'react';
139
+
140
+ import { usePlacesAutocomplete } from '@ttoss/google-maps';
141
+
142
+ const Component = () => {
143
+ const [text, setText] = React.useState('');
144
+ const clearText = () => setText('');
145
+
146
+ const apiKey = 'YOUR_API_KEY';
147
+
148
+ const {
149
+ ready,
150
+ value,
151
+ suggestions: { status, data },
152
+ setValue,
153
+ clearSuggestions,
154
+ } = usePlacesAutocomplete({
155
+ debounce: 300,
156
+ cacheKey: apiKey,
157
+ });
158
+
159
+ const handleInput = (e: any) => {
160
+ setValue(e.target.value);
161
+ };
162
+
163
+ const handleSelect =
164
+ ({ description }: { description: string }) =>
165
+ () => {
166
+ setValue(description, false);
167
+ clearSuggestions();
168
+ };
169
+
170
+ const renderSuggestions = () =>
171
+ data.map((suggestion) => {
172
+ const {
173
+ place_id,
174
+ structured_formatting: { main_text, secondary_text },
175
+ } = suggestion;
176
+
177
+ return (
178
+ <li key={place_id} onClick={() => handleSelect(suggestion)}>
179
+ <strong>{main_text}</strong>
180
+
181
+ <small>{secondary_text}</small>
182
+ </li>
183
+ );
184
+ });
185
+
186
+ return (
187
+ <div>
188
+ <input value={value} onChange={handleInput} disabled={!ready} />
189
+
190
+ {status === 'OK' && <ul>{renderSuggestions()}</ul>}
191
+ </div>
192
+ );
193
+ };
194
+
195
+ export default Component;
196
+ ```
197
+
198
+ ## 📘 Types
199
+
200
+ ```ts
201
+ import { ScriptStatus } from '@ttoss/hooks';
202
+
203
+ const useMap: (options: google.maps.MapOptions) => {
204
+ map: google.maps.Map | null;
205
+ ref: React.RefObject<HTMLDivElement>;
206
+ isMapInitialized: boolean;
207
+ };
208
+
209
+ const useGeocoder: () => {
210
+ geocoder: google.maps.Geocoder | null;
211
+ isGeocoderInitialized: boolean;
212
+ };
213
+
214
+ type Libraries = 'places' | 'visualization' | 'drawing' | 'geometry';
215
+
216
+ type Extends<T, U extends T> = U;
217
+
218
+ type LoadedMapsStatus = Extends<ScriptStatus, 'ready'>;
219
+
220
+ type NotLoadedMapStatus = Extends<ScriptStatus, 'idle' | 'error' | 'loading'>;
221
+
222
+ export type GoogleMaps = typeof google.maps;
223
+
224
+ const GoogleMapsProvider: React.FC<{
225
+ apiKey: string;
226
+ libraries?: Libraries[] | undefined;
227
+ }>;
228
+
229
+ const useGoogleMaps: () =>
230
+ | {
231
+ status: LoadedMapsStatus;
232
+ googleMaps: GoogleMaps;
233
+ }
234
+ | {
235
+ status: NotLoadedMapStatus;
236
+ googleMaps: null;
237
+ };
25
238
  ```
26
239
 
27
240
  ---
@@ -33,5 +246,5 @@ $ npm run build
33
246
  <br/>
34
247
 
35
248
  <h4 align="center">
36
- Special thanks for the inspiring <a href="https://github.com/jmarceli/react-hook-google-maps">react-hook-google-maps</a> documentation.
249
+ Special thanks for the inspiring <a href="https://github.com/jmarceli/react-hook-google-maps">react-hook-google-maps</a> and <a href="https://github.com/wellyshen/use-places-autocomplete">use-places-autocomplete</a> documentation.
37
250
  </h4>
package/dist/esm/index.js CHANGED
@@ -40,18 +40,37 @@ var GoogleMapsProvider = ({ children, apiKey, libraries }) => {
40
40
  };
41
41
  var useGoogleMaps = () => useContext(GoogleMapsContext);
42
42
 
43
- // src/useMap.ts
43
+ // src/useGeocoder.ts
44
44
  import {
45
45
  useMemo as useMemo2,
46
- useRef,
47
46
  useState
48
47
  } from "react";
48
+ var useGeocoder = () => {
49
+ const { googleMaps } = useGoogleMaps();
50
+ const [isGeocoderInitialized, setIsGeocoderInitialized] = useState(false);
51
+ const geocoder = useMemo2(() => {
52
+ if (googleMaps) {
53
+ const googleMapsGeocoder = new googleMaps.Geocoder();
54
+ setIsGeocoderInitialized(true);
55
+ return googleMapsGeocoder;
56
+ }
57
+ return null;
58
+ }, [googleMaps]);
59
+ return { geocoder, isGeocoderInitialized };
60
+ };
61
+
62
+ // src/useMap.ts
63
+ import {
64
+ useMemo as useMemo3,
65
+ useRef,
66
+ useState as useState2
67
+ } from "react";
49
68
  var useMap = (options) => {
50
69
  const ref = useRef(null);
51
70
  const { googleMaps } = useGoogleMaps();
52
- const [isMapInitialized, setIsMapInitialized] = useState(false);
71
+ const [isMapInitialized, setIsMapInitialized] = useState2(false);
53
72
  const optionsStringify = JSON.stringify(options);
54
- const map = useMemo2(() => {
73
+ const map = useMemo3(() => {
55
74
  if (googleMaps && ref.current) {
56
75
  const parsedOptions = JSON.parse(optionsStringify);
57
76
  const googleMapsMap = new googleMaps.Map(ref.current, parsedOptions);
@@ -64,6 +83,7 @@ var useMap = (options) => {
64
83
  };
65
84
  export {
66
85
  GoogleMapsProvider,
86
+ useGeocoder,
67
87
  useGoogleMaps,
68
88
  useMap
69
89
  };
package/dist/index.d.ts CHANGED
@@ -23,10 +23,15 @@ declare const useGoogleMaps: () => {
23
23
  googleMaps: null;
24
24
  };
25
25
 
26
+ declare const useGeocoder: () => {
27
+ geocoder: google.maps.Geocoder | null;
28
+ isGeocoderInitialized: boolean;
29
+ };
30
+
26
31
  declare const useMap: (options: google.maps.MapOptions) => {
27
32
  map: google.maps.Map | null;
28
33
  ref: React.RefObject<HTMLDivElement>;
29
34
  isMapInitialized: boolean;
30
35
  };
31
36
 
32
- export { GoogleMapsProvider, useGoogleMaps, useMap };
37
+ export { GoogleMapsProvider, useGeocoder, useGoogleMaps, useMap };
package/dist/index.js CHANGED
@@ -40,6 +40,25 @@ var GoogleMapsProvider = ({ children, apiKey, libraries }) => {
40
40
  };
41
41
  var useGoogleMaps = () => _react.useContext.call(void 0, GoogleMapsContext);
42
42
 
43
+ // src/useGeocoder.ts
44
+
45
+
46
+
47
+
48
+ var useGeocoder = () => {
49
+ const { googleMaps } = useGoogleMaps();
50
+ const [isGeocoderInitialized, setIsGeocoderInitialized] = _react.useState.call(void 0, false);
51
+ const geocoder = _react.useMemo.call(void 0, () => {
52
+ if (googleMaps) {
53
+ const googleMapsGeocoder = new googleMaps.Geocoder();
54
+ setIsGeocoderInitialized(true);
55
+ return googleMapsGeocoder;
56
+ }
57
+ return null;
58
+ }, [googleMaps]);
59
+ return { geocoder, isGeocoderInitialized };
60
+ };
61
+
43
62
  // src/useMap.ts
44
63
 
45
64
 
@@ -66,4 +85,5 @@ var useMap = (options) => {
66
85
 
67
86
 
68
87
 
69
- exports.GoogleMapsProvider = GoogleMapsProvider; exports.useGoogleMaps = useGoogleMaps; exports.useMap = useMap;
88
+
89
+ exports.GoogleMapsProvider = GoogleMapsProvider; exports.useGeocoder = useGeocoder; exports.useGoogleMaps = useGoogleMaps; exports.useMap = useMap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/google-maps",
3
- "version": "0.6.1",
3
+ "version": "0.8.4",
4
4
  "author": "ttoss",
5
5
  "contributors": [
6
6
  {
@@ -31,12 +31,12 @@
31
31
  "build": "tsup"
32
32
  },
33
33
  "dependencies": {
34
- "@ttoss/hooks": "^0.6.1",
34
+ "@ttoss/hooks": "^0.8.4",
35
35
  "@types/google.maps": "^3.45.6"
36
36
  },
37
37
  "peerDependencies": {
38
38
  "react": ">=16.8.0",
39
39
  "react-dom": ">=16.8.0"
40
40
  },
41
- "gitHead": "a348cbdd81ca30040a403d491724b7c5dd0d8395"
41
+ "gitHead": "9d8124a3110e71122dbdeebc791bac649e43791c"
42
42
  }
@@ -20,7 +20,7 @@ const googleMock = {
20
20
  };
21
21
 
22
22
  beforeAll(() => {
23
- (global.google as any) = googleMock;
23
+ (global.google as unknown) = googleMock;
24
24
  });
25
25
 
26
26
  const RenderStatus = () => {
@@ -0,0 +1,8 @@
1
+ import * as googleMaps from './index';
2
+
3
+ test('should export all methods', () => {
4
+ expect(googleMaps.GoogleMapsProvider).toBeDefined();
5
+ expect(googleMaps.useGeocoder).toBeDefined();
6
+ expect(googleMaps.useGoogleMaps).toBeDefined();
7
+ expect(googleMaps.useMap).toBeDefined();
8
+ });
package/src/index.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export { GoogleMapsProvider, useGoogleMaps } from './GoogleMapsProvider';
2
+ export { useGeocoder } from './useGeocoder';
2
3
  export { useMap } from './useMap';
@@ -0,0 +1,22 @@
1
+ import * as React from 'react';
2
+
3
+ import { useGoogleMaps } from './GoogleMapsProvider';
4
+
5
+ export const useGeocoder = () => {
6
+ const { googleMaps } = useGoogleMaps();
7
+
8
+ const [isGeocoderInitialized, setIsGeocoderInitialized] =
9
+ React.useState(false);
10
+
11
+ const geocoder = React.useMemo(() => {
12
+ if (googleMaps) {
13
+ const googleMapsGeocoder = new googleMaps.Geocoder();
14
+ setIsGeocoderInitialized(true);
15
+ return googleMapsGeocoder;
16
+ }
17
+
18
+ return null;
19
+ }, [googleMaps]);
20
+
21
+ return { geocoder, isGeocoderInitialized };
22
+ };