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

Package detail

@vis.gl/react-google-maps

visgl742.4kMIT1.5.0TypeScript support: included

React components and hooks for the Google Maps JavaScript API

React, React Components, React Hooks, Google Maps, Google Maps JavaScript API, Maps

readme

React Components for the Google Maps JavaScript API

MIT License

This is a TypeScript / JavaScript library to integrate the Maps JavaScript API into your React application. It comes with a collection of React components to create maps, markers and infowindows, and a set of hooks to use some of the Maps JavaScript API Services and Libraries.

Installation

This library is available on npm as @vis.gl/react-google-maps.

npm install @vis.gl/react-google-maps

or

yarn add @vis.gl/react-google-maps

(PowerShell users: since @ has a special meaning in PowerShell, the package name has to be quoted)

Usage

Import the APIProvider and wrap it around all components that should have access to the Maps JavaScript API. Any component within the context of the APIProvider can use the hooks and components provided by this library.

To render a simple map, add a Map component inside the APIProvider. Within the Map component, you can then add further components like Marker, AdvancedMarker, or InfoWindow to render content on the map.

For more advanced use-cases you can even add your own components to the map that make use of google.maps.OverlayView or google.maps.WebGlOverlayView.

import {AdvancedMarker, APIProvider, Map} from '@vis.gl/react-google-maps';

function App() {
  const position = {lat: 53.54992, lng: 10.00678};

  return (
    <APIProvider apiKey={'YOUR API KEY HERE'}>
      <Map defaultCenter={position} defaultZoom={10} mapId="DEMO_MAP_ID">
        <AdvancedMarker position={position} />
      </Map>
    </APIProvider>
  );
}

export default App;

Please see our documentation or examples for more in-depth information about this library.

Using other libraries of the Maps JavaScript API

Besides rendering maps, the Maps JavaScript API has a lot of additional libraries for things like geocoding, routing, the Places API, Street View, and a lot more.

These libraries are not loaded by default, which is why this module provides the useMapsLibrary() hook to handle dynamic loading of additional libraries.

For example, if you just want to use the google.maps.geocoding.Geocoder class in a component and you don't even need a map, it can be implemented like this:

import {useMap, useMapsLibrary} from '@vis.gl/react-google-maps';

const MyComponent = () => {
  // useMapsLibrary loads the geocoding library, it might initially return `null`
  // if the library hasn't been loaded. Once loaded, it will return the library
  // object as it would be returned by `await google.maps.importLibrary()`
  const geocodingLib = useMapsLibrary('geocoding');
  const geocoder = useMemo(
    () => geocodingLib && new geocodingLib.Geocoder(),
    [geocodingLib]
  );

  useEffect(() => {
    if (!geocoder) return;

    // now you can use `geocoder.geocode(...)` here
  }, [geocoder]);

  return <></>;
};

const App = () => {
  return (
    <APIProvider apiKey={'YOUR API KEY HERE'}>
      <MyComponent />
    </APIProvider>
  );
};

Examples

Explore our examples directory on GitHub or the examples on our website for full implementation examples.

Supported Browsers

Being a library built around the Google Maps JavaScript API, we follow the same browser-support policy as the Google Maps Team, available here. Generally, the last two versions of the major browsers are officially supported.

It is not unlikely that browsers even far outside the given range will still work. We try our best to support as many browsers and versions as reasonably possible, but we won't actively investigate issues related to outdated browser versions.

However, if you can suggest small changes that could be made to even increase that range, we will be happy to include them, as long as they don't negatively affect the supported browsers.

Terms of Service

`@vis.gl/react-google-maps` uses Google Maps Platform services. Use of Google Maps Platform services through this library is subject to the Google Maps Platform Terms of Service.

This library is not a Google Maps Platform Core Service. Therefore, the Google Maps Platform Terms of Service (e.g., Technical Support Services, Service Level Agreements, and Deprecation Policy) do not apply to this library.

Help and Support

This library is offered via an open source license. It is not governed by the Google Maps Platform Technical Support Services Guidelines, the SLA, or the Deprecation Policy (however, any Google Maps Platform services used by this library remain subject to the Google Maps Platform Terms of Service).

If you find a bug or have a feature request, please file an issue on GitHub. If you would like to get answers to technical questions from other Google Maps Platform developers, feel free to open a thread in the discussions section on GitHub or ask a question through one of our developer community channels.

If you'd like to contribute, please check the Contributing guide.

You can also discuss this library on our Discord server.

changelog

Changelog

1.5.0 (2025-01-10)

Features

  • add StaticMap component with SSR support (#633) (55acea2)

1.4.3 (2025-01-07)

Bug Fixes

  • deps-dev: bump react, react-dom and corresponding types (#650) (02fe28f)
  • use deep compare effect to prevent infowindow close (#642) (bfa85c1)

1.4.2 (2024-11-28)

Bug Fixes

1.4.1 (2024-11-22)

Bug Fixes

1.4.0 (2024-10-24)

Features

Bug Fixes

  • adjust advanced marker markup to fix anchoring & collision behavior (#577) (97a98b2)

1.3.0 (2024-09-30)

Features

  • add new onError prop for api provider (#541) (bbe5709)

Bug Fixes

  • avoid unnecessary state-updates in api-provider (#551) (46068c9)

1.2.0 (2024-09-16)

Features

  • add hover events and anchor points to advanced markers (#472) (cc4a397)

1.1.3 (2024-09-08)

Bug Fixes

1.1.2 (2024-09-06)

Bug Fixes

  • map: undefined rendering-type and color-scheme (#515) (c288d15)

1.1.1 (2024-09-02)

Bug Fixes

1.1.0 (2024-06-17)

Features

1.0.2 (2024-05-30)

Bug Fixes

  • info-window: fix reappearing InfoWindows (#393) (dc51eb9)

1.0.1 (2024-05-27)

Bug Fixes

  • advanced-marker: apply marker class when rendering a Pin (#384) (e8a4cc3)

1.0.0 (2024-05-11)

Miscellaneous Chores

0.11.2 (2024-05-10)

Bug Fixes

  • map: set other container position to relative (#357) (8e77d70)

0.11.1 (2024-05-10)

Bug Fixes

  • advanced-marker: remove content element in cleanup (#351) (128df87)
  • map: set container position to relative (#356) (7fa2b71)

0.11.0 (2024-05-08)

⚠ BREAKING CHANGES

  • map: Introduction of map instance caching needed a change to the DOM-Structure produced by the map component (added a div-element owned by the Map component to contain the map instance).
  • map: The type passed to the onProjectionChange is changed from MapCameraChangedEvent to MapEvent, so there are no longer camera-props available for this event

Features

  • advanced-marker: add support for clickable option (#341) (ca96e54)
  • map: implement initial version of map-instance caching (#349) (4a6e83a)

Bug Fixes

  • map: change event-type of projectionChanged event to MapEvent (#346) (83f9309)

0.10.0 (2024-05-03)

Features

  • add solution-channel parameter (#334) (f93e43e)
  • advanced-marker: add style prop to add styles to content-element (#337) (e942fb5)
  • infowindow: add className and style props (92854c9)
  • infowindow: add missing options and events (92854c9)
  • infowindow: InfoWindow overhaul (#335) (92854c9)

Bug Fixes

  • infowindow: add missing cleanup for infowindow (92854c9)
  • infowindow: better dependency checks, using useDeepCompareEffect where needed (92854c9)
  • infowindow: removed unneeded dependency in infowindow hooks (92854c9)

0.9.0 (2024-04-18)

Features

  • better handling for missing map configuration (#308) (b318d67)

Bug Fixes

0.8.3 (2024-04-04)

Bug Fixes

  • api-loader didn't call callback on repeat load calls (743878a)
  • map controls crashing when invalid key is provided (#290) (5052dfb)

0.8.2 (2024-03-29)

Bug Fixes

  • memoize context-values to avoid excessive re-renders (#287) (bea68f9), closes #285

0.8.1 (2024-03-26)

Bug Fixes

  • InfoWindow.shouldFocus doesn't work with false as value (#278) (2f4b508)

0.8.0 (2024-03-20)

Features

0.7.1 (2024-02-23)

Bug Fixes

0.7.0 (2024-02-15)

⚠ BREAKING CHANGES

  • removed the useStreetViewPanorama() and useDirectionsService() hooks.

Features

Code Refactoring

0.6.1 (2024-02-08)

Bug Fixes

  • remove deep-link into fast-deep-equal package (#208) (f0be380)

0.6.0 (2024-02-07)

⚠ BREAKING CHANGES

  • The behaviour of the props controlling camera parameters (center, zoom, heading and tilt) changed. Unless you are using controlled props, you have to change the prop names from e.g. center to defaultCenter (the same goes for zoom, heading and tilt).

Code Refactoring

  • improved state-handling implementation (#181) (904b918)

0.5.4 (2024-02-01)

Bug Fixes

  • prevent passing empty parameters to ApiLoader (#193) (0601753)

0.5.3 (2024-02-01)

Bug Fixes

  • add loading=async to maps API url (cb1336f)
  • use parameter v instead of version (0626fb6)

0.5.2 (2024-02-01)

Bug Fixes

  • trigger release for new library function from commit 31f2655 (b5a13e5)

0.5.1 (2024-01-31)

Bug Fixes

  • infowindow double rendering and eslint warnings (#185) (404cc06)

0.5.0 (2024-01-18)

Features

  • handle API-key errors in map-component (#165) (26ccc15)

Bug Fixes

0.4.3 (2024-01-05)

Bug Fixes

  • allow AdvancedMarker to accept space-separated multiple class names (#143) (eab53e2)

0.4.2 (2023-12-22)

Bug Fixes

  • avoid re-render on every importLibrary() call (#135) (32b5894)

0.4.1 (2023-12-01)

Bug Fixes

  • move @types/google.maps to dependencies (#115) (9b788e1), closes #106
  • output an error when useMap is called outside APIProvider (#117) (5c30c3d)

0.4.0 (2023-11-28)

Features

  • Allow <Pin> glyphs to be passed as children (close #98) (#99) (6374453)

0.3.3 (2023-11-13)

Bug Fixes

0.3.2 (2023-11-09)

Bug Fixes

  • use moveCamera and useLayoutEffect for faster map-updates (e493d5f)

0.3.1 (2023-11-09)

Bug Fixes

0.3.0 (2023-11-09)

⚠ BREAKING CHANGES

  • removed MapProps.onLoadMap

Features

  • cleanup map, remove onLoadMap prop (d5e7dfd)
  • implement props for all map-events with custom MapEvent type (820a301)
  • update map viewport when props are changed (0b1d800)

0.2.1 (2023-11-07)

Bug Fixes

0.2.0 (2023-11-07)

Features

Bug Fixes

  • replace prop gmpDraggable with draggable in AdvancedMarker (#53) (1dbf477)
  • update usage of useMapsLibrary in AdvancedMarker (#55) (b01fc8b)

0.1.2 (2023-11-01)

Miscellaneous Chores

  • add registry-url to release action (9fa403b)

0.1.1 (2023-11-01)

Bug Fixes

  • empty commit to trigger release-please (b04a942)

0.1.0 (2023-10-27)

⚠ BREAKING CHANGES

  • loading multiple libraries at once is no longer supported, changed the return type of useMapsLibrary.

Features

  • useMapsLibrary returns API object instead of boolean (#26) (a3aa4c5)