Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

react-google-maps-loader

cedricdelpoux18kMIT4.3.1TypeScript support: definitely-typed

React component to use google maps services into your react applications using a render prop

react, google, maps, googlemaps, loader, decorator

readme

react-google-maps-loader

npm package Travis Codecov Module formats

React Component to use google maps services into your react applications using a render prop.

Getting started

react-google-maps-loader

You can download react-google-maps-loader from the NPM registry via the npm or yarn commands

yarn add react-google-maps-loader
npm install react-google-maps-loader --save

If you don't use package manager and you want to include react-google-maps-loader directly in your html, you could get it from the UNPKG CDN

https://unpkg.com/react-google-maps-loader/dist/react-google-maps-loader.min.js.

You can also try the component's editable demo hands-on and install it from bit.dev.

Usage

Load Map Only

This renders when the map is ready, with no loading state.

import React from "react"
import ReactGoogleMapLoader from "react-google-maps-loader"

const App = () => (
    <ReactGoogleMapLoader
        params={{
            key: YOUR_API_KEY, // Define your api key here
            libraries: "places,geometry", // To request multiple libraries, separate them with a comma
        }}
        render={googleMaps => googleMaps && <div>Google Maps is loaded !</div>}
    />
)

Show Loading State

You can show a custom loading state while the user is still online by using the error values.

Error Values

  • [String] Network Error - if the user us offline.

  • [String] SDK Authentication Error - if there is a problem loading Google maps due to incorrect keys, going over quota or one of the errors listed in the Error Messages Documentation.

  • undefined - map loaded correctly.

import React from "react"
import ReactGoogleMapLoader from "react-google-maps-loader"

const App = () => (
    <ReactGoogleMapLoader
        params={{
            key: YOUR_API_KEY, // Define your api key here
            libraries: "places,geometry", // To request multiple libraries, separate them with a comma
        }}
        render={(googleMaps, error) =>
            googleMaps ? (
                <div>
                    {/*Show a custom error if SDK Authentication Error. See N/B 2 below.*/}
                    {error ? error : "Google Maps is loaded !"}
                </div>
            ) : (
                <div>
                    {/*Check for network error so loading state ends if user lost connection.*/}
                    {error === "Network Error" ? (
                        <p>{error}</p>
                    ) : (
                        <p>isLoading...</p>
                    )}
                </div>
            )
        }
    />
)

N/B:

  1. The Google Maps API does not provide errors in the callback but logs them to the console. We grouped all Google Maps errors not related to network connectivity as SDK Authentication Error. Check the console if you get this.

  2. googleMaps always loads as long as there is no Network Error and the previous state is not cached. So, handle SDK Authentication Errors (See 1. above) in the googleMaps part of the conditional rendering as shown in the code above.

Demo

See Demo page

Contributing

  • ⇄ Pull/Merge requests and ★ Stars are always welcome.
  • For bugs and feature requests, please create an issue.
  • Pull requests must be accompanied by passing automated tests (npm test).

See CONTRIBUTING.md guidelines

Changelog

See changelog

License

This project is licensed under the MIT License - see the LICENCE.md file for details

changelog

4.3.0 - 2020-04-27

  • Added: Do not load google maps multiple times
  • Updated: Update dependencies

4.2.5 - 2019-02-12

  • Updated: Update dependencies

4.2.4 - 2019-02-07

  • Fixed: Remove warning

4.2.3 - 2018-04-10

  • Updated: Move window.google initialisation

4.2.2 - 2018-04-10

  • Fixed: Tests

4.2.1 - 2018-04-10

  • Updated: Load GoogleMapsSdk in componentDidMount instead of componentWillMount

4.2.0 - 2018-03-23

  • Add: Error message and loading state by @joshuaai

4.1.0 - 2017-11-03

  • Removed: Useless wrapper

4.0.1 - 2017-10-26

  • Added: Tests

4.0.0 - 2017-10-25

  • Updated: Tranform Hight Order Component to React Component using a render prop
  • Added: CommonJS build
  • Added: ES6 build
  • Added: UMD build
  • Added: Demo page

3.0.1 - 2017-07-03

  • Updated: Documentation

3.0.0 - 2017-07-03

  • Updated: Deprecate v2.0.3

2.0.3 - 2017-06-14

  • Updated: Render component even when google maps is not loaded yet

2.0.2 - 2017-01-04

  • Updated: React peer dependency

2.0.1 - 2016-11-21

  • Added: Handle non web-browser environments

2.0.0 - 2016-10-24

  • Added: little-loader lib to fetch Google Map API
  • Added: query-string lib to transform params to query string
  • Updated: package.json description, author and dependencies
  • Updated: .babelrc
  • Updated: .eslintrc
  • Updated: src/index.js

1.0.1 - 2015-09-11

  • Added: libraryTarget into Webpack config

1.0.0 - 2015-09-08

  • Initial release