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

Package detail

google-map-react

Isomorphic component that allows rendering react components on a google map

react, reactjs, google, map, maps, isomorphic, render, component, javascript, react-component

readme

Google Map React · npm version Build Status PRs Welcome

google-map-react is a component written over a small set of the Google Maps API. It allows you to render any React component on the Google Map. It is fully isomorphic and can render on a server. Additionally, it can render map components in the browser even if the Google Maps API is not loaded. It uses an internal, tweakable hover algorithm - every object on the map can be hovered.

It allows you to create interfaces like this example (You can scroll the table, zoom/move the map, hover/click on markers, and click on table rows)

Getting started

In the simple case you just need to add lat and lng props to any child of GoogleMapReact component.

See it in action at jsbin

import React from "react";
import GoogleMapReact from 'google-map-react';

const AnyReactComponent = ({ text }) => <div>{text}</div>;

export default function SimpleMap(){
  const defaultProps = {
    center: {
      lat: 10.99835602,
      lng: 77.01502627
    },
    zoom: 11
  };

  return (
    // Important! Always set the container height explicitly
    <div style={{ height: '100vh', width: '100%' }}>
      <GoogleMapReact
        bootstrapURLKeys={{ key: "" }}
        defaultCenter={defaultProps.center}
        defaultZoom={defaultProps.zoom}
      >
        <AnyReactComponent
          lat={59.955413}
          lng={30.337844}
          text="My Marker"
        />
      </GoogleMapReact>
    </div>
  );
}

My map doesn't appear!

  • Make sure the container element has width and height. The map will try to fill the parent container, but if the container has no size, the map will collapse to 0 width / height. This is not a requirement for google-map-react, it's a requirement for google-maps in general.

Installation

npm:

npm install --save google-map-react

yarn:

yarn add google-map-react

Features

Works with your Components

Instead of the default Google Maps markers, balloons and other map components, you can render your cool animated react components on the map.

Isomorphic Rendering

It renders on the server. (Welcome search engines) (you can disable javascript in browser dev tools, and reload any example page to see how it works)

Component Positions Calculated Independently of Google Maps API

It renders components on the map before (and even without) the Google Maps API loaded.

Google Maps API Loads on Demand

There is no need to place a <script src= tag at top of page. The Google Maps API loads upon the first usage of the GoogleMapReact component.

Use Google Maps API

You can access to Google Maps map and maps objects by using onGoogleApiLoaded, in this case you will need to set yesIWantToUseGoogleMapApiInternals to true

...

const handleApiLoaded = (map, maps) => {
  // use map and maps objects
};

...

<GoogleMapReact
  bootstrapURLKeys={{ key: /* YOUR KEY HERE */ }}
  defaultCenter={this.props.center}
  defaultZoom={this.props.zoom}
  yesIWantToUseGoogleMapApiInternals
  onGoogleApiLoaded={({ map, maps }) => handleApiLoaded(map, maps)}
>
  <AnyReactComponent
    lat={59.955413}
    lng={30.337844}
    text="My Marker"
  />
</GoogleMapReact>

PST: Remember to set yesIWantToUseGoogleMapApiInternals to true.

Example here

Internal Hover Algorithm

Now every object on the map can be hovered (however, you can still use css hover selectors if you want). If you try zooming out here example, you will still be able to hover on almost every map marker.

Examples

Documentation

You can find the documentation here:

Contribute

Local development is broken into two parts (ideally using two tabs).

First, run rollup to watch your src/ module and automatically recompile it into dist/ whenever you make changes.

npm start # runs rollup with watch flag

The second part will be running the example/ create-react-app that's linked to the local version of your module.

# (in another tab)
cd example
npm start # runs create-react-app dev server

Now, anytime you make a change to your library in src/ or to the example app's example/src, create-react-app will live-reload your local dev server so you can iterate on your component in real-time.

If you get the error Module not found: Can't resolve 'google-react-map'... while trying to run the example app, you need to manually link your local development module, try the following steps:

  1. In the root folder:
    npm link
  2. Go into example/ and (after installing other dependencies) execute:
    npm link google-map-react

License

MIT

Known Issues

!!! We are looking for contributors

We're actively looking for contributors, please send a message to the Owner or any of the Collaborators.

changelog

[2.1.10] - 2021-06-22

  • 1028 Update peerDependencies for React 17

[2.1.0] - 2020-08-31

  • 921 Add support to libraries: places, visualization, places, and geomerty.

    And keeps support for previous heatMapLibrary prop to avoid breaking older usage.

[2.0.4] - 2020-07-30

  • Fix default and lib imports

[2.0.2] - 2020-07-30

  • 878 Fix heatmap options not being updated

[2.0.1] - 2020-07-30

  • 864 On resize it's getting undefined minZoom

  • 873 Moves marker dispatcher binds to componentDidMount

[2.0.0] - 2020-07-30

Changed

  • Rewrite library using create-react-library
  • Breaking Move google-map-react/utils to module import, meaning that instead of doing: import { utilName } from 'google-map-react/utils you will do import { utilName } from google-map-react

[1.1.7] - 2020-03-17

  • 835 Add dev folders to .gitignore

[1.1.6] - 2020-02-04

  • 827 Updated China base url

[1.1.5] - 2019-09-20

  • 778 Add UNSAFE_ prefix to deprecated lifecycle methods

  • 769 Move prop-types to dependencies

  • 759 Add shouldUnregisterMapOnUnmount prop to persist map object on unmount

  • 754 Add prop 'onDragEnd'

[1.1.4] - 2019-04-03

  • 738 Revert #726 Google Map loader respecting the app language change

  • 739 Revert #722 Add shouldUnregisterMapOnUnmount prop to persist map object on unmount

[1.1.3] - 2019-04-01

  • 726 Google Map loader respecting the app language change

  • 722 Add shouldUnregisterMapOnUnmount prop to persist map object on unmount

  • 728 Fix: Update heatmap layer when heatmap positions prop changes

[1.1.2] - 2018-12-18

  • 696 Add React 16 createPortal with backwards compatibility

[1.1.1] - 2018-10-21

  • 679 Revert #593 (had a problem with the way of importing libraries)

[1.1.0] - 2018-10-08

  • 655 Add math abs to avoid negative values when calculating zoom

  • 656 Pass map instance to onDrag handler

  • 593 Added feature: update heat map on data change + fix linting

[1.0.9] - 2018-09-29

  • 634 Custom div style options

[1.0.8] - 2018-09-21

  • 645 Revert #643 Use React 16 map portal to render map overlay. Was causing problems for users using React version < 16

[1.0.7] - 2018-09-20

  • 631 Add passive scroll

  • 643 Use React 16 map portal to render map overlay

[1.0.6] - 2018-08-03

  • 620 Fix bug in fromContainerPixelToLatLng()

  • 615 Add prop onTilesLoaded

[1.0.5] - 2018-06-26

  • 605 Fix build

  • 603 Remove marker jiggle, Issue #575

[1.0.4] - 2018-06-11

  • 570 Avoid null error

  • 594 Add guard around mapDom event listener

  • 592 Move dep for react-dom

[1.0.3] - 2018-05-17

  • 580: Scales tiles properly

[1.0.2] - 2018-05-13

  • 577 Move react-dom to dependencies

[1.0.1] - 2018-05-12

  • 572 Add weights to the heatmap

[1.0.0] - 2018-04-27

  • 559 Fix soom animation for google maps' version 3.32

  • 548 Improves to documentation

  • 535 Remove lodash completely, using our own functions, now the build size is 4 times smaller.

  • 533 Fixes some of the examples in the documentation

  • 555 & #563 Fix error Target container is not a DOM element

[0.34.0] - 2018-04-27

  • 521 Upgrade .babelrc

  • 211 Call zoomControlClickTime on all clicks

  • 530 Set latest release version as default if user does not specify a version

[0.33.0] - 2018-03-09

  • 504 Added loose to es2015 for IE10, IE11 support

  • 517 Fix error about fromLatLngToDivPixel

  • 525 Accept 0 as zoom value

  • 529 Update package.json

[0.30.0] - 2018-02-12

  • 441 Add heatmap functionality

  • 496 Fix support for china

[0.28.0] - 2018-01-18

  • 494 Add support for China

[0.27.0] - 2018-01-04

  • 485 Add fullscreen check for Internet explorer 11

  • 482 Make resetBoundsOnResize preserve center when full-screened

  • 483 Updates yarn.lock to fix @mapbox/point-geometry warning

[0.9]

Add prop onDragEnd to react on the dragend event

Add google-map-clustering-example

Add prop onTilesLoaded to react on the tilesloaded event

Add: bootstrapURLKeys (object) instead of apiKey prop

(apiKey prop is now deprecated)

Example:

<GoogleMap
  bootstrapURLKeys={{
    key: API_KEY,
    language: 'ru',
    ...otherUrlParams,
  }}
>

[0.8]

Draggable markers support, examples comig soon.

Mon Oct 5 2015

Add OnChange({center, zoom, bounds: {nw, se}, size, ...oherMapProps})

Add deprecation warning to onBoundsChange

Add utils functions, with fitBounds and other functions

  import { fitBounds } from 'google-map-react/utils';

  const bounds = {
    nw: {
      lat: 50.01038826014866,
      lng: -118.6525866875,
     },
     se: {
      lat: 32.698335045970396,
      lng: -92.0217273125,
     },
  };

  const size = {
    width: 640, // Map width in pixels
    height: 380, // Map height in pixels
  };

  const {center, zoom} = fitBounds({nw, se}, size);

Sun Oct 4 2015

Add minZoom calculation, to prevent situations when one map point can have multiple screen coordinates.

Sun Oct 4 2015

Add ability to access to internal google api

<GoogleMap  onGoogleApiLoaded={({map, maps}) => console.log(map, maps)} />

(to prevent warn message add yesIWantToUseGoogleMapApiInternals property to GoogleMap)

Sun Oct 4 2015

Add: defaultZoom defaultCenter properties, it closes #9 #10

Sat Oct 3 2015

Support center prop as {lat, lng} object

Thu Oct 1 2015

Add onClick, onZoomAnimationStart, onZoomAnimationEnd events.