1 | # @react-google-maps/api
|
2 |
|
3 | ![logo](https://raw.githubusercontent.com/JustFly1984/react-google-maps-api/master/logo.png)
|
4 |
|
5 | [![npm package](https://img.shields.io/npm/v/@react-google-maps/api)](https://www.npmjs.com/package/@react-google-maps/api)
|
6 | [![npm downloads](https://img.shields.io/npm/dt/@react-google-maps/api)](https://www.npmjs.com/package/@react-google-maps/api)
|
7 | [![npm bundle size](https://img.shields.io/bundlephobia/min/@react-google-maps/api)](https://www.npmjs.com/package/@react-google-maps/api)
|
8 | [![Join the community on Spectrum](https://withspectrum.github.io/badge/badge.svg)](https://spectrum.chat/react-google-maps)
|
9 | [![DeepScan grade](https://deepscan.io/api/teams/6449/projects/8455/branches/101268/badge/grade.svg)](https://deepscan.io/dashboard#view=project&tid=6449&pid=8455&bid=101268)
|
10 |
|
11 | @react-google-maps/api
|
12 |
|
13 | > This library requires React v16.6 or later. To use the latest features (including hooks) requires React v16.8+. If you need support for earlier versions of React, you should check out [react-google-maps](https://github.com/tomchentw/react-google-maps)
|
14 |
|
15 | This is complete re-write of the (sadly unmaintained) `react-google-maps` library. We thank [tomchentw](https://github.com/tomchentw/) for his great work that made possible.
|
16 |
|
17 | @react-google-maps/api provides very simple bindings to the google maps api and lets you use it in your app as React components.
|
18 |
|
19 | Here are the main additions to react-google-maps that were the motivation behind this re-write
|
20 |
|
21 | ## Install @react-google-maps/api
|
22 |
|
23 | with NPM
|
24 |
|
25 | ```#!/bin/bash
|
26 | npm i -S @react-google-maps/api
|
27 | ```
|
28 |
|
29 | or Yarn
|
30 |
|
31 | ```#!/bin/bash
|
32 | yarn add @react-google-maps/api
|
33 | ```
|
34 |
|
35 | ## Migration from react-google-maps@9.4.5
|
36 |
|
37 | if you need an access to map object, instead of `ref` prop, you need to use `onLoad` callback on `<GoogleMap />` component.
|
38 |
|
39 | Before:
|
40 |
|
41 | ```jsx
|
42 | // before
|
43 | <GoogleMap
|
44 | ref={map => {
|
45 | const bounds = new window.google.maps.LatLngBounds();
|
46 |
|
47 | map.fitBounds(bounds);
|
48 | }}
|
49 | />
|
50 | ```
|
51 |
|
52 | After:
|
53 |
|
54 | ```jsx
|
55 | <GoogleMap
|
56 | onLoad={map => {
|
57 | const bounds = new window.google.maps.LatLngBounds();
|
58 | map.fitBounds(bounds);
|
59 | }}
|
60 | onUnmount={map => {
|
61 | // do your stuff before map is unmounted
|
62 | }}
|
63 | />
|
64 | ```
|
65 |
|
66 | ## Main features
|
67 |
|
68 | - Simplified API
|
69 | - Uses the new Context API
|
70 | - Supports async React (StrictMode compliant)
|
71 | - Removes lodash dependency =>
|
72 | smaller bundle size `12.4kb` gzip, tree-shakeable [https://bundlephobia.com/result?p=@react-google-maps/api](https://bundlephobia.com/result?p=@react-google-maps/api)
|
73 | - forbids loading of Roboto fonts, if you set property preventGoogleFonts on `<LoadScript preventGoogleFonts />` component
|
74 |
|
75 | ## Examples
|
76 |
|
77 | Examples can be found in two places:
|
78 |
|
79 | 1. [Official docs](https://react-google-maps-api-docs.netlify.com/) (powered by [react-styleguidist](https://github.com/styleguidist/react-styleguidist).
|
80 | 2. A Gatsby app including some examples. See the [examples](https://github.com/JustFly1984/react-google-maps-api/tree/master/packages/react-google-maps-api-gatsby-example/src/examples) folder
|
81 | 3. [Gatsby.js Demo](https://react-google-maps-api-gatsby-demo.netlify.com/)
|
82 |
|
83 | ## Advice
|
84 |
|
85 | > Using the examples requires you to generate a google maps api key. For instructions on how to do that please see the following [guide](https://developers.google.com/maps/documentation/embed/get-api-key)
|
86 |
|
87 | ## Community Help Resource
|
88 |
|
89 | You can join the community at [Spectrum.chat](https://spectrum.chat/react-google-maps) to ask questions and help others with your experience or join our [Slack channel](https://join.slack.com/t/react-google-maps-api/shared_invite/enQtODc5ODU1NTY5MzQ4LTBiNTYzZmY1YmVjYzJhZThkMGU0YzUwZjJkNGJmYjk4YjQyYjZhMDk2YThlZGEzNDc0M2RhNjBmMWE4ZTJiMjQ)
|
90 |
|
91 | ## Contribute
|
92 |
|
93 | Maintainers and contributors are very welcome! See [this issue](https://github.com/JustFly1984/react-google-maps-api/issues/18) to get started.
|
94 |
|
95 | ## How to test changes locally
|
96 |
|
97 | When working on a feature/fix, you're probably gonna want to test your changes. This workflow is a work in progress. Please feel free to improve it!
|
98 |
|
99 | 1. In the file `packages/react-google-maps-api/package.json` change `main` to `"src/index.ts"`
|
100 | 2. In the same file, delete the `module` field
|
101 | 3. You can now use the package `react-google-maps-api-gatsby-example` to test your changes. Just make sure you change the import from `@react-google-maps/api` to `../../../react-google-maps-api`
|
102 |
|
103 | Since 1.2.0 you can use onLoad and onMount props for each @react-google-maps/api component, ref does not contain API methods anymore.
|
104 |
|
105 | Since version 1.2.2 We added useGoogleMap hook, which is working only with React@16.8.1 and later versions.
|
106 |
|
107 | ## Websites made with @react-google-maps-api
|
108 |
|
109 | [DriveFromTo.com](https://www.drivefromto.com/en) Transfer Booking service PWA.
|
110 |
|
111 | [Shipwrecks.cc](https://shipwrecks.cc/) Shipwrecks from Wikipedia visualized on the map [(Github)](https://github.com/lauriharpf/shipwrecks)
|
112 |
|
113 | add your website by making PR!
|