@ttoss/google-maps 0.8.3 → 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.
Files changed (2) hide show
  1. package/README.md +227 -14
  2. package/package.json +3 -3
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/google-maps",
3
- "version": "0.8.3",
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.8.3",
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": "4a0b5b1f51c9241b0b8d4a35b88a84d856d46d44"
41
+ "gitHead": "9d8124a3110e71122dbdeebc791bac649e43791c"
42
42
  }