google-maps-react-markers 2.0.15 → 3.0.0

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/LICENCE.md CHANGED
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
18
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
19
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
20
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,228 +1,90 @@
1
- <h1 align="center">Google Maps React Markers</h1>
2
-
3
1
  <p align="center">
4
- <a href="https://www.npmjs.com/package/google-maps-react-markers" target="_blank">
5
- <img src="https://img.shields.io/npm/v/google-maps-react-markers.svg?style=for-the-badge&labelColor=000000" alt="NPM" />
6
- </a>
7
- <a href="https://npm-stat.com/charts.html?package=google-maps-react-markers" target="_blank">
8
- <img src="https://img.shields.io/npm/dt/google-maps-react-markers.svg?style=for-the-badge&labelColor=000000" alt="NPM total downloads" title="last year">
9
- </a>
10
- <img src="https://img.shields.io/badge/Maintained%3F-yes-green.svg?style=for-the-badge&labelColor=000000" alt="Maintained" />
11
- <a href="https://github.com/giorgiabosello/google-maps-react-markers/blob/master/LICENCE.md" target="_blank">
12
- <img src="https://img.shields.io/github/license/giorgiabosello/google-maps-react-markers?style=for-the-badge&labelColor=000000" alt="GitHub license: MIT" />
13
- </a>
14
- <img src="https://img.shields.io/github/stars/giorgiabosello/google-maps-react-markers?style=for-the-badge&labelColor=000000" alt="GitHub stars" />
15
- <a href="https://github.com/giorgiabosello/google-maps-react-markers/issues" target="_blank">
16
- <img src="https://img.shields.io/github/issues/giorgiabosello/google-maps-react-markers?style=for-the-badge&labelColor=000000" alt="GitHub open issues" />
17
- </a>
18
- <a href="https://makeapullrequest.com/">
19
- <img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=for-the-badge&labelColor=000000" alt="PRs welcome" />
20
- </a>
2
+ <img src="https://raw.githubusercontent.com/giorgiabosello/google-maps-react-markers/main/.github/cover.png" alt="Google Maps React Markers" width="100%" />
21
3
  </p>
22
4
 
23
- Google Maps library that accepts markers as react components and works with React 18+.
24
-
25
- It supports a small set of the props of [Google Map React](https://github.com/google-map-react/google-map-react). Clustering also is possible.
26
- The library implements [Google Maps Custom Overlays](https://developers.google.com/maps/documentation/javascript/customoverlays) official library.
27
-
28
- **If you like this library, please consider supporting me ❤️**
29
-
30
- [![Buy me a Coffee](https://img.shields.io/badge/Buy_Me_A_Coffee-FFDD00?style=for-the-badge&logo=buy-me-a-coffee&logoColor=black)](https://www.buymeacoffee.com/giorgiabosello)
31
- [![PayPal](https://img.shields.io/badge/PayPal-00457C?style=for-the-badge&logo=paypal&logoColor=white)](https://www.paypal.me/giorgiabosello)
32
-
33
- ## 💬 Discussion
34
-
35
- > **Released TypeScript version of the library!**
36
-
37
- Feedbacks are welcome in [this discussion](https://github.com/giorgiabosello/google-maps-react-markers/discussions/109).
38
-
39
- ## 🚀 Demo
40
-
41
- <a href="https://giorgiabosello.github.io/google-maps-react-markers/" target="blank">
42
- <img src="https://img.shields.io/website?url=https%3A%2F%2Fgiorgiabosello.github.io%2Fgoogle-maps-react-markers&logo=github&style=for-the-badge&labelColor=000000" />
43
- </a>
5
+ <p align="center">
6
+ <a href="https://www.npmjs.com/package/google-maps-react-markers"><img src="https://img.shields.io/npm/v/google-maps-react-markers.svg?style=flat-square&labelColor=000000" alt="npm version" /></a>
7
+ <a href="https://npm-stat.com/charts.html?package=google-maps-react-markers"><img src="https://img.shields.io/npm/dm/google-maps-react-markers.svg?style=flat-square&labelColor=000000" alt="npm downloads" /></a>
8
+ <a href="https://bundlephobia.com/package/google-maps-react-markers"><img src="https://img.shields.io/bundlephobia/minzip/google-maps-react-markers?style=flat-square&labelColor=000000" alt="bundle size" /></a>
9
+ <a href="https://github.com/giorgiabosello/google-maps-react-markers/actions/workflows/ci.yml"><img src="https://img.shields.io/github/actions/workflow/status/giorgiabosello/google-maps-react-markers/ci.yml?style=flat-square&labelColor=000000&label=CI" alt="CI" /></a>
10
+ <a href="https://github.com/giorgiabosello/google-maps-react-markers/blob/main/LICENCE.md"><img src="https://img.shields.io/github/license/giorgiabosello/google-maps-react-markers?style=flat-square&labelColor=000000" alt="MIT license" /></a>
11
+ </p>
44
12
 
45
- See it in action [here](https://giorgiabosello.github.io/google-maps-react-markers/) _(API KEY not provided)_.
13
+ **Render any React component as a Google Maps marker.** React 19, Next.js 16, advanced markers or custom overlays, drag and drop, clustering-ready, SSR safe, fully typed.
46
14
 
47
- Demo source code is available [here](https://github.com/giorgiabosello/google-maps-react-markers/tree/master/docs/src).
15
+ **[Documentation and live demos →](https://giorgiabosello.github.io/google-maps-react-markers/)**
48
16
 
49
- ## 🛠 Install
17
+ ## Install
50
18
 
51
19
  ```bash
20
+ npm i google-maps-react-markers
21
+ # or
52
22
  pnpm add google-maps-react-markers
53
- ```
54
-
55
- or
56
-
57
- ```bash
58
23
  yarn add google-maps-react-markers
24
+ bun add google-maps-react-markers
59
25
  ```
60
26
 
61
- or
27
+ - **React 19** (`react` and `react-dom` are peer dependencies).
28
+ - **ESM only.** Node.js 22.12+ for server rendering and builds.
29
+ - A **Google Maps Platform API key**, restricted to your sites. A [map ID](https://giorgiabosello.github.io/google-maps-react-markers/docs/renderers) is optional: it enables advanced markers.
62
30
 
63
- ```bash
64
- npm install --save google-maps-react-markers
65
- ```
31
+ Works with Next.js (App Router and Server Components), Vite, React Router, TanStack Start and Astro.
66
32
 
67
- ## 💻 Usage
33
+ ## Usage
68
34
 
69
- ```jsx
70
- import GoogleMap from 'google-maps-react-markers'
71
-
72
- const App = () => {
73
- const mapRef = useRef(null)
74
- const [mapReady, setMapReady] = useState(false)
75
-
76
- /**
77
- * @description This function is called when the map is ready
78
- * @param {Object} map - reference to the map instance
79
- * @param {Object} maps - reference to the maps library
80
- */
81
- const onGoogleApiLoaded = ({ map, maps }) => {
82
- mapRef.current = map
83
- setMapReady(true)
84
- }
85
-
86
- const onMarkerClick = (e, { markerId, lat, lng }) => {
87
- console.log('This is ->', markerId)
88
-
89
- // inside the map instance you can call any google maps method
90
- mapRef.current.setCenter({ lat, lng })
91
- // ref. https://developers.google.com/maps/documentation/javascript/reference?hl=it
92
- }
35
+ ```tsx
36
+ import { GoogleMap, Marker } from 'google-maps-react-markers'
93
37
 
38
+ export function StoreMap({ stores, onSelect }) {
94
39
  return (
95
- <>
96
- {mapReady && <div>Map is ready. See for logs in developer console.</div>}
97
- <GoogleMap
98
- apiKey=""
99
- defaultCenter={{ lat: 45.4046987, lng: 12.2472504 }}
100
- defaultZoom={5}
101
- options={mapOptions}
102
- mapMinHeight="100vh"
103
- onGoogleApiLoaded={onGoogleApiLoaded}
104
- onChange={(map) => console.log('Map moved', map)}
105
- >
106
- {coordinates.map(({ lat, lng, name }, index) => (
107
- <Marker
108
- key={index}
109
- lat={lat}
110
- lng={lng}
111
- markerId={name}
112
- onClick={onMarkerClick} // you need to manage this prop on your Marker component!
113
- // draggable={true}
114
- // onDragStart={(e, { latLng }) => {}}
115
- // onDrag={(e, { latLng }) => {}}
116
- // onDragEnd={(e, { latLng }) => {}}
117
- />
118
- ))}
119
- </GoogleMap>
120
- </>
40
+ <GoogleMap
41
+ apiKey="YOUR_API_KEY"
42
+ mapId="YOUR_MAP_ID"
43
+ defaultCenter={{ lat: 45.44, lng: 12.32 }}
44
+ defaultZoom={6}
45
+ style={{ height: 480 }}
46
+ onChange={({ center, zoom, bbox }) => console.log(center, zoom, bbox)}
47
+ >
48
+ {stores.map((store) => (
49
+ <Marker
50
+ key={store.id}
51
+ lat={store.lat}
52
+ lng={store.lng}
53
+ anchor="bottom"
54
+ title={`${store.price} at ${store.name}`}
55
+ onClick={() => onSelect(store.id)}
56
+ >
57
+ <PriceTag price={store.price} />
58
+ </Marker>
59
+ ))}
60
+ </GoogleMap>
121
61
  )
122
62
  }
123
-
124
- export default App
125
- ```
126
-
127
- ## 🧐 Props
128
-
129
- ### GoogleMap component
130
-
131
- | Prop | Type | Required | Default | Description |
132
- | -------------------- | -------- | -------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
133
- | **apiKey** | string | **yes** | `''` | API Key to load Google Maps |
134
- | **defaultCenter** | object | **yes** | `{ lat: 0, lng: 0 }` | Default center of the map |
135
- | **defaultZoom** | number | **yes** | `1-20` | Default zoom of the map |
136
- | libraries | array | no | `['places', 'geometry']` | Libraries to load |
137
- | options | object | no | `{}` | Options for the map |
138
- | onGoogleApiLoaded | function | no | `() => {}` | Callback when the map is loaded |
139
- | onChange | function | no | `() => {}` | Callback when the map has changed |
140
- | events | array | no | `[]` | Array of objects name/handler of [DOM events](https://en.wikipedia.org/wiki/DOM_event) to pass down to the `div` overlay. Example: `events: [{ name: 'onClick', handler: () => {} }]` |
141
- | children | node | no | `null` | Markers of the map |
142
- | loadScriptExternally | bool | no | `false` | Whether to load the Google Maps script externally.<br>If `true`, the `status` prop is required and it will be used to control the loading of the script |
143
- | status | string | no | `idle` | The forced status of the Google Maps script. Depends on `loadScriptExternally`.<br>It can be one of `idle`, `loading`, `ready`, `error` |
144
- | loadingContent | node | no | `'Google Maps is loading'` | Content to show while the map is loading |
145
- | idleContent | node | no | `'Google Maps is on idle'` | Content to show when the map is idle |
146
- | errorContent | node | no | `'Google Maps is on error'` | Content to show when the map has an error |
147
- | mapMinHeight | string | no | `'unset'` | Min height of the map |
148
- | containerProps | object | no | `{}` | Props for the div container of the map |
149
- | scriptCallback | function | no | `() => {}` | window global callback passed to the Google Script |
150
- | externalApiParams | object | no | `undefined` | Optional params to pass to the Google API script. Eg. `{region: 'IT', language: 'it'}` |
151
-
152
- ### Markers
153
-
154
- | Prop | Type | Required | Default | Description |
155
- | ----------- | ------ | -------- | ----------- | -------------------------------------------------------------------------------------------------------- |
156
- | **lat** | number | **yes** | `undefined` | Latitude of the marker |
157
- | **lng** | number | **yes** | `undefined` | Longitude of the marker |
158
- | draggable | bool | no | `false` | If true, the marker can be dragged |
159
- | onDragStart | func | no | `() => {}` | This event is fired when the user starts dragging the marker |
160
- | onDrag | func | no | `() => {}` | This event is repeatedly fired while the user drags the marker |
161
- | onDragEnd | func | no | `() => {}` | This event is fired when the user stops dragging the marker |
162
- | zIndex | number | no | 0 | The z-index of the marker. To bring the marker to the front, e.g. when it is active, set a higher value. |
163
-
164
- ## 📍 Clustering
165
-
166
- For clustering, follow this [guide](https://www.leighhalliday.com/google-maps-clustering) using [useSupercluster Hook](https://github.com/leighhalliday/use-supercluster), but use bounds in this way:
167
-
168
- ```jsx
169
- const [mapBounds, setMapBounds] = useState({
170
- bounds: [0, 0, 0, 0],
171
- zoom: 0,
172
- })
173
- const onMapChange = ({ bounds, zoom }) => {
174
- const ne = bounds.getNorthEast()
175
- const sw = bounds.getSouthWest()
176
- /**
177
- * useSupercluster accepts bounds in the form of [westLng, southLat, eastLng, northLat]
178
- * const { clusters, supercluster } = useSupercluster({
179
- * points: points,
180
- * bounds: mapBounds.bounds,
181
- * zoom: mapBounds.zoom,
182
- * })
183
- */
184
- setMapBounds({ ...mapBounds, bounds: [sw.lng(), sw.lat(), ne.lng(), ne.lat()], zoom })
185
- }
186
63
  ```
187
64
 
188
- ## 👥 Contributing
65
+ ## Features
189
66
 
190
- To run the project locally, clone the repo and run:
67
+ - **Any component is a marker.** Markers are React portals: state, hooks, context and your CSS all work.
68
+ - **Two renderers, one API.** `AdvancedMarkerElement` when the map has a `mapId`, a custom overlay when it does not (so JSON `styles` keep working).
69
+ - **Drag and drop** with mouse, touch and keyboard, `anchor`, `zIndex` and clicks.
70
+ - **Fast.** Markers are created once and updated in place. The [stress test](https://giorgiabosello.github.io/google-maps-react-markers/docs/examples/many-markers) renders up to 5,000 markers.
71
+ - **Accessible.** Clickable and draggable markers are keyboard buttons with screen reader names and announcements, on both renderers.
72
+ - **SSR and React Server Components.** No `window` during render, clean hydration, `'use client'` included.
73
+ - **Hooks and controls.** `useMap()`, `useMapsLibrary()`, `useGoogleMapsStatus()`, `<MapControl>`.
74
+ - **Small and typed.** About 6 kB brotli with the official `@googlemaps/js-api-loader` included, ESM only.
191
75
 
192
- ```bash
193
- # in the root directory
194
- yarn --frozen-install
195
- yarn dev
196
- ```
197
-
198
- ```bash
199
- # in another tab (with NextJS and SSR)
200
- cd docs
201
- yarn install
202
- yarn dev
203
- ```
204
-
205
- Do your changes to `src/` or `docs/src` directory, commits all files in the root directory (`yarn.lock` and ones in `dist` too) and open a PR.
76
+ ## Upgrading from v2
206
77
 
207
- ## 💻 Built with
78
+ v3 is a breaking release: markers go inside `<Marker>`, the import is named, and a few props changed. Follow the [migration guide](https://giorgiabosello.github.io/google-maps-react-markers/docs/migration/v3).
208
79
 
209
- - [React](https://reactjs.org/)
210
- - [TypeScript](https://www.typescriptlang.org/)
211
- - [Google Maps Custom Overlays](https://developers.google.com/maps/documentation/javascript/customoverlays)
212
- - [tsup](https://github.com/egoist/tsup): for bundling
213
- - [ESLint](https://eslint.org/): for linting
214
- - [Prettier](https://prettier.io/): for code formatting
80
+ ## Contributing
215
81
 
216
- ## 🗒 License
82
+ See [CONTRIBUTING.md](https://github.com/giorgiabosello/google-maps-react-markers/blob/main/CONTRIBUTING.md).
217
83
 
218
- MIT © [giorgiabosello](https://github.com/giorgiabosello)
84
+ ## Support
219
85
 
220
- ## 🙏 Support
86
+ If this library saves you time, you can [buy me a coffee](https://www.buymeacoffee.com/giorgiabosello) or [send a PayPal tip](https://www.paypal.me/giorgiabosello).
221
87
 
222
- [![Buy me a Coffee](https://img.shields.io/badge/Buy_Me_A_Coffee-FFDD00?style=for-the-badge&logo=buy-me-a-coffee&logoColor=black)](https://www.buymeacoffee.com/giorgiabosello)
223
- [![PayPal](https://img.shields.io/badge/PayPal-00457C?style=for-the-badge&logo=paypal&logoColor=white)](https://www.paypal.me/giorgiabosello)
88
+ ## License
224
89
 
225
- <hr>
226
- <p align="center">
227
- Developed with ❤️ in Italy 🇮🇹
228
- </p>
90
+ MIT © [Giorgia Bosello](https://github.com/giorgiabosello)