Page Summary
-
This example demonstrates how to render a basic Google Map in a React application using the
vis.gl/react-google-mapslibrary. -
The
vis.gl/react-google-mapslibrary provides React components and hooks for integrating the Google Maps JavaScript API, but it's important to note it is open source and not officially supported by Google. -
The example includes code snippets for TypeScript, JavaScript, CSS, and HTML, showcasing the implementation of the map within a simple web page.
-
Users can try the sample directly in Google Cloud Shell or clone the repository and run it locally using Git and Node.js.
-
While the library itself is open source, any Google Maps Platform services utilized within it are subject to the Google Maps Platform Terms of Service.
This example uses the vis.gl/react-google-maps open source library to render a Google map in a React app. The vis.gl/react-google-maps library is a collection of React components and hooks for the Google Maps JavaScript API.
TypeScript
import React from 'react'; import { createRoot } from 'react-dom/client'; import { APIProvider, Map, AdvancedMarker } from '@vis.gl/react-google-maps'; const API_KEY = 'GOOGLE_MAPS_API_KEY'; export default function App() { return ( <APIProvider apiKey={API_KEY}> <Map defaultCenter={{ lat: 37.4220656, lng: -122.0840897 }} defaultZoom={10} mapId="DEMO_MAP_ID"> <AdvancedMarker position={{ lat: 37.4220656, lng: -122.0840897 }} title="Mountain View, CA" /> </Map> </APIProvider> ); } export function renderToDom(container: HTMLElement) { const root = createRoot(container); root.render( <React.StrictMode> <App /> </React.StrictMode> ); }
JavaScript
import React from 'react'; import { createRoot } from 'react-dom/client'; import { APIProvider, Map, AdvancedMarker } from '@vis.gl/react-google-maps'; const API_KEY = 'GOOGLE_MAPS_API_KEY'; export default function App() { return (React.createElement(APIProvider, { apiKey: API_KEY }, React.createElement(Map, { defaultCenter: { lat: 37.4220656, lng: -122.0840897 }, defaultZoom: 10, mapId: "DEMO_MAP_ID" }, React.createElement(AdvancedMarker, { position: { lat: 37.4220656, lng: -122.0840897 }, title: "Mountain View, CA" })))); } export function renderToDom(container) { const root = createRoot(container); root.render(React.createElement(React.StrictMode, null, React.createElement(App, null))); }
HTML
<html lang="en">
<head>
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, user-scalable=no" />
<title>React - Basic Map</title>
<meta name="description" content="React Basic Map Example" />
<style>
body {
margin: 0;
font-family: sans-serif;
}
#app {
width: 100vw;
height: 100vh;
}
</style>
<script type="module">
import { renderToDom } from './src/app';
renderToDom(document.querySelector('#app'));
</script>
</head>
<body>
<div id="app"></div>
</body>
</html>Clone Sample
Git and Node.js are required to run this sample locally. Follow these instructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.
git clone https://github.com/googlemaps-samples/js-api-samples.gitcd samples/rgm-basic-mapnpm inpm start
Integration notes
When integrating Google Maps within a React application, this sample implements
several key best practices using the open source @vis.gl/react-google-maps
library.
1. APIProvider
The <APIProvider> component is used at the root of the application to
load the Google Maps JavaScript API script once. It handles the API loading
state and makes the Google Maps instance available to all child components using
React Context.
2. Map Component
Instead of manually instantiating new google.maps.Map() and attaching it
to a DOM node, the <Map> component lets you define the map
declaratively.
- By using
defaultCenteranddefaultZoom, we create an "uncontrolled" map component where the map manages its own state internally, while still initializing it to the correct location. - The
mapIdproperty is required to enable Cloud-based Maps Styling and Advanced Markers.
3. Advanced Markers
The legacy google.maps.Marker class is deprecated. This sample uses the
<AdvancedMarker> component to declaratively render the modern
AdvancedMarkerElement directly on the map.