@ttoss/google-maps 2.1.13 → 2.1.15
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 +105 -59
- package/dist/esm/index.js +3 -7
- package/dist/index.d.ts +5 -5
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
# @ttoss/google-maps
|
|
2
2
|
|
|
3
|
-
<strong>@ttoss/google-maps</strong>
|
|
3
|
+
<strong>@ttoss/google-maps</strong> provides a concise, opinionated way to use Google Maps in React apps. This guide covers setup, usage, and key API features so you can get started quickly.
|
|
4
4
|
|
|
5
5
|
## Installing
|
|
6
6
|
|
|
7
|
-
Install
|
|
7
|
+
Install the package:
|
|
8
8
|
|
|
9
9
|
```shell
|
|
10
|
-
|
|
10
|
+
pnpm add @ttoss/google-maps
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
For TypeScript support:
|
|
14
14
|
|
|
15
15
|
```shell
|
|
16
|
-
|
|
16
|
+
pnpm add -D @types/google.maps
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
Add this to a declaration file (e.g., `typings.d.ts`):
|
|
20
20
|
|
|
21
21
|
```typescript title="typings.d.ts"
|
|
22
22
|
/// <reference types="google.maps" />
|
|
@@ -24,31 +24,29 @@ Then, add the following to a declaration file (generally `typings.d.ts`):
|
|
|
24
24
|
|
|
25
25
|
## Getting Started
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
At the root of your application, configure `GoogleMapsProvider` with your Google Maps API key. This way, the whole application can access the `google` variable.
|
|
27
|
+
Set up `GoogleMapsProvider` at your app root to provide the Google Maps context. This enables all child components to access the Google Maps API.
|
|
30
28
|
|
|
31
29
|
```tsx
|
|
32
30
|
import { GoogleMapsProvider } from '@ttoss/google-maps';
|
|
33
31
|
|
|
34
|
-
const App = ({ children }) =>
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
</GoogleMapsProvider>
|
|
40
|
-
</OtherProviders>
|
|
41
|
-
);
|
|
42
|
-
};
|
|
32
|
+
const App = ({ children }) => (
|
|
33
|
+
<GoogleMapsProvider apiKey={process.env.GOOGLE_MAPS_API_KEY}>
|
|
34
|
+
{children}
|
|
35
|
+
</GoogleMapsProvider>
|
|
36
|
+
);
|
|
43
37
|
```
|
|
44
38
|
|
|
45
|
-
|
|
39
|
+
## Rendering the Map
|
|
46
40
|
|
|
47
|
-
|
|
41
|
+
Use the `useMap` hook to render a map in your component. Define `height` and `width` for the map container:
|
|
48
42
|
|
|
49
43
|
```tsx
|
|
50
44
|
import { Box, Text } from '@ttoss/ui';
|
|
51
45
|
import { useMap } from '@ttoss/google-maps';
|
|
46
|
+
import * as React from 'react';
|
|
47
|
+
|
|
48
|
+
const height = 400;
|
|
49
|
+
const width = '100%';
|
|
52
50
|
|
|
53
51
|
const MyComponent = () => {
|
|
54
52
|
const { ref, map } = useMap();
|
|
@@ -71,59 +69,45 @@ const MyComponent = () => {
|
|
|
71
69
|
};
|
|
72
70
|
```
|
|
73
71
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
### Retrieve `google.maps` object
|
|
72
|
+
## Accessing the `google.maps` Object
|
|
77
73
|
|
|
78
|
-
|
|
74
|
+
Use the `useGoogleMaps` hook to access the `google.maps` object for advanced API usage:
|
|
79
75
|
|
|
80
76
|
```tsx
|
|
81
77
|
import { useGoogleMaps } from '@ttoss/google-maps';
|
|
82
78
|
|
|
83
79
|
const MyComponent = () => {
|
|
84
80
|
const { google } = useGoogleMaps();
|
|
85
|
-
|
|
86
81
|
return <Text>{google.maps.version}</Text>;
|
|
87
82
|
};
|
|
88
83
|
```
|
|
89
84
|
|
|
90
|
-
With this, you can perform any operation that the `google.maps` object allows, such as creating markers, drawing polygons, etc.
|
|
91
|
-
|
|
92
85
|
## Advanced Usage
|
|
93
86
|
|
|
94
|
-
### Using with Next.js
|
|
87
|
+
### Using with Next.js
|
|
95
88
|
|
|
96
|
-
If you use Next.js,
|
|
89
|
+
If you use Next.js, pass the [Next.js `Script`](https://nextjs.org/docs/app/api-reference/components/script) component to `GoogleMapsProvider`:
|
|
97
90
|
|
|
98
91
|
```tsx
|
|
99
92
|
import { GoogleMapsProvider } from '@ttoss/google-maps';
|
|
100
93
|
import Script from 'next/script';
|
|
101
94
|
|
|
102
|
-
const App = ({ children }) =>
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
Script={Script}
|
|
108
|
-
>
|
|
109
|
-
{children}
|
|
110
|
-
</GoogleMapsProvider>
|
|
111
|
-
</OtherProviders>
|
|
112
|
-
);
|
|
113
|
-
};
|
|
95
|
+
const App = ({ children }) => (
|
|
96
|
+
<GoogleMapsProvider apiKey={process.env.GOOGLE_MAPS_API_KEY} Script={Script}>
|
|
97
|
+
{children}
|
|
98
|
+
</GoogleMapsProvider>
|
|
99
|
+
);
|
|
114
100
|
```
|
|
115
101
|
|
|
116
|
-
### Reusing `map`
|
|
102
|
+
### Reusing the `map` Object
|
|
117
103
|
|
|
118
|
-
|
|
104
|
+
Use `MapProvider` to share the map object between components:
|
|
119
105
|
|
|
120
106
|
```tsx
|
|
121
107
|
import { MapProvider, useMap } from '@ttoss/google-maps';
|
|
122
108
|
|
|
123
109
|
const ChildComponent = () => {
|
|
124
|
-
// Access the map object created by the parent component
|
|
125
110
|
const { map } = useMap();
|
|
126
|
-
|
|
127
111
|
React.useEffect(() => {
|
|
128
112
|
if (map) {
|
|
129
113
|
map.setOptions({
|
|
@@ -132,13 +116,13 @@ const ChildComponent = () => {
|
|
|
132
116
|
});
|
|
133
117
|
}
|
|
134
118
|
}, [map]);
|
|
135
|
-
|
|
136
119
|
return null;
|
|
137
120
|
};
|
|
138
121
|
|
|
139
122
|
const ParentComponent = () => {
|
|
140
123
|
const { ref, map } = useMap();
|
|
141
|
-
|
|
124
|
+
const height = 400;
|
|
125
|
+
const width = '100%';
|
|
142
126
|
return (
|
|
143
127
|
<MapProvider map={map} ref={ref}>
|
|
144
128
|
<Box>
|
|
@@ -151,12 +135,80 @@ const ParentComponent = () => {
|
|
|
151
135
|
};
|
|
152
136
|
```
|
|
153
137
|
|
|
138
|
+
### Adding a Marker
|
|
139
|
+
|
|
140
|
+
To use markers, include the `marker` library in `GoogleMapsProvider`:
|
|
141
|
+
|
|
142
|
+
```tsx
|
|
143
|
+
<GoogleMapsProvider
|
|
144
|
+
apiKey={process.env.GOOGLE_MAPS_API_KEY}
|
|
145
|
+
libraries={['marker']}
|
|
146
|
+
>
|
|
147
|
+
{children}
|
|
148
|
+
</GoogleMapsProvider>
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Add a marker using `google.maps.marker.AdvancedMarkerElement`:
|
|
152
|
+
|
|
153
|
+
```tsx
|
|
154
|
+
import { useMap, useGoogleMaps } from '@ttoss/google-maps';
|
|
155
|
+
import React from 'react';
|
|
156
|
+
|
|
157
|
+
const height = 400;
|
|
158
|
+
const width = '100%';
|
|
159
|
+
|
|
160
|
+
const MyMapWithMarker = ({ location }) => {
|
|
161
|
+
const { ref, map } = useMap();
|
|
162
|
+
const { google } = useGoogleMaps();
|
|
163
|
+
const markerRef = React.useRef(null);
|
|
164
|
+
|
|
165
|
+
React.useEffect(() => {
|
|
166
|
+
if (map) {
|
|
167
|
+
map.setOptions({
|
|
168
|
+
center: {
|
|
169
|
+
lat: location.latitude,
|
|
170
|
+
lng: location.longitude,
|
|
171
|
+
},
|
|
172
|
+
zoom: location.zoom || 13,
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
if (google?.maps && map && location) {
|
|
176
|
+
const marker = new google.maps.marker.AdvancedMarkerElement({
|
|
177
|
+
position: {
|
|
178
|
+
lat: location.latitude,
|
|
179
|
+
lng: location.longitude,
|
|
180
|
+
},
|
|
181
|
+
map,
|
|
182
|
+
title: location.name,
|
|
183
|
+
});
|
|
184
|
+
markerRef.current = marker;
|
|
185
|
+
}
|
|
186
|
+
}, [map, location, google]);
|
|
187
|
+
|
|
188
|
+
return <div ref={ref} style={{ height, width }} />;
|
|
189
|
+
};
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Error Handling
|
|
193
|
+
|
|
194
|
+
You can handle script loading errors using the `onError` prop in `GoogleMapsProvider`:
|
|
195
|
+
|
|
196
|
+
```tsx
|
|
197
|
+
<GoogleMapsProvider
|
|
198
|
+
apiKey={process.env.GOOGLE_MAPS_API_KEY}
|
|
199
|
+
onError={(error) => {
|
|
200
|
+
// Handle error
|
|
201
|
+
console.error(error);
|
|
202
|
+
}}
|
|
203
|
+
>
|
|
204
|
+
{children}
|
|
205
|
+
</GoogleMapsProvider>
|
|
206
|
+
```
|
|
207
|
+
|
|
154
208
|
## API
|
|
155
209
|
|
|
156
210
|
### `GoogleMapsProvider`
|
|
157
211
|
|
|
158
|
-
#### Props
|
|
159
|
-
|
|
160
212
|
- `apiKey`: string - Google Maps API key.
|
|
161
213
|
- `libraries`: string[] - [Libraries to load](https://developers.google.com/maps/documentation/javascript/libraries).
|
|
162
214
|
- `language`: string - [Language](https://developers.google.com/maps/documentation/javascript/localization).
|
|
@@ -165,29 +217,23 @@ const ParentComponent = () => {
|
|
|
165
217
|
|
|
166
218
|
### `MapProvider`
|
|
167
219
|
|
|
168
|
-
#### Props
|
|
169
|
-
|
|
170
220
|
- `map`: google.maps.Map | null - Google Maps object.
|
|
171
221
|
- `children`: React.ReactNode - Children to render.
|
|
172
222
|
- `ref`: `React.RefObject<HTMLDivElement>` - Reference to the map container.
|
|
173
223
|
|
|
174
224
|
### `useMap`
|
|
175
225
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
#### Returns
|
|
226
|
+
Returns:
|
|
179
227
|
|
|
180
228
|
- `ref`: `React.RefObject<HTMLDivElement>` - Reference to the map container.
|
|
181
229
|
- `map`: google.maps.Map | null - Google Maps object.
|
|
182
230
|
|
|
183
231
|
### `useGoogleMaps`
|
|
184
232
|
|
|
185
|
-
|
|
233
|
+
Returns:
|
|
186
234
|
|
|
187
235
|
- `google`: typeof google - `google.maps` object.
|
|
188
236
|
- `status`: 'idle' | 'error' | 'loading' | 'ready' - Status of the script loading.
|
|
189
|
-
- `isReady`: boolean - Whether the script is ready
|
|
237
|
+
- `isReady`: boolean - Whether the script is ready (`status === 'ready'`).
|
|
190
238
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
```
|
|
239
|
+
_For more on product development principles that guide our approach, see [Product Development Principles](https://ttoss.dev/docs/product/product-development/principles)._
|
package/dist/esm/index.js
CHANGED
|
@@ -6,8 +6,8 @@ var __name = (target, value) => __defProp(target, "name", {
|
|
|
6
6
|
});
|
|
7
7
|
|
|
8
8
|
// src/GoogleMapsProvider.tsx
|
|
9
|
-
import * as React from "react";
|
|
10
9
|
import { useScript } from "@ttoss/react-hooks";
|
|
10
|
+
import * as React from "react";
|
|
11
11
|
var GoogleMapsContext = /* @__PURE__ */React.createContext({
|
|
12
12
|
status: "idle",
|
|
13
13
|
google: {
|
|
@@ -119,11 +119,7 @@ var useGoogleMaps = /* @__PURE__ */__name(() => {
|
|
|
119
119
|
return {
|
|
120
120
|
isReady,
|
|
121
121
|
status,
|
|
122
|
-
google
|
|
123
|
-
/**
|
|
124
|
-
* @deprecated Use google.maps instead.
|
|
125
|
-
*/
|
|
126
|
-
googleMaps: google.maps
|
|
122
|
+
google
|
|
127
123
|
};
|
|
128
124
|
}, "useGoogleMaps");
|
|
129
125
|
|
|
@@ -173,7 +169,7 @@ var useMap = /* @__PURE__ */__name((options = {}) => {
|
|
|
173
169
|
if (!isReady) {
|
|
174
170
|
return;
|
|
175
171
|
}
|
|
176
|
-
if (!google.maps) {
|
|
172
|
+
if (!google.maps?.Map) {
|
|
177
173
|
return;
|
|
178
174
|
}
|
|
179
175
|
setMap(new google.maps.Map(ref.current, options));
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,11 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
|
2
2
|
import * as React from 'react';
|
|
3
3
|
|
|
4
4
|
type GoogleMaps = typeof google.maps;
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* The libraries that can be loaded from the Google Maps JavaScript API.
|
|
7
|
+
* https://developers.google.com/maps/documentation/javascript/libraries?hl=pt-br#available-libraries
|
|
8
|
+
*/
|
|
9
|
+
type Libraries = 'core' | 'maps' | 'maps3d' | 'places' | 'geocoding' | 'routes' | 'marker' | 'geometry' | 'elevation' | 'streetView' | 'journeySharing' | 'visualization' | 'airQuality' | 'addressValidation';
|
|
6
10
|
type ScriptProps = {
|
|
7
11
|
src: string;
|
|
8
12
|
onReady: () => void;
|
|
@@ -87,10 +91,6 @@ declare const useGoogleMaps: () => {
|
|
|
87
91
|
} | {
|
|
88
92
|
maps: null;
|
|
89
93
|
};
|
|
90
|
-
/**
|
|
91
|
-
* @deprecated Use google.maps instead.
|
|
92
|
-
*/
|
|
93
|
-
googleMaps: typeof google.maps | null;
|
|
94
94
|
};
|
|
95
95
|
|
|
96
96
|
export { GoogleMapsProvider, MapProvider, useGeocoder, useGoogleMaps, useMap, usePlacesAutocomplete };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttoss/google-maps",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.15",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "ttoss",
|
|
6
6
|
"contributors": [
|
|
@@ -36,8 +36,8 @@
|
|
|
36
36
|
"jest": "^30.0.4",
|
|
37
37
|
"react": "^19.1.0",
|
|
38
38
|
"tsup": "^8.5.0",
|
|
39
|
-
"@ttoss/
|
|
40
|
-
"@ttoss/
|
|
39
|
+
"@ttoss/config": "^1.35.8",
|
|
40
|
+
"@ttoss/test-utils": "^2.1.28"
|
|
41
41
|
},
|
|
42
42
|
"keywords": [
|
|
43
43
|
"Google",
|