Get a Maps Demo Key: Try out select Maps JavaScript API and Places UI Kit features at no cost with a Maps Demo Key—no billing information required.

React Google Maps Library - Basic Map

  • This example demonstrates how to render a basic Google Map in a React application using the vis.gl/react-google-maps library.

  • The vis.gl/react-google-maps library 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

importReactfrom'react';
import{createRoot}from'react-dom/client';
import{APIProvider,Map,AdvancedMarker}from'@vis.gl/react-google-maps';
constAPI_KEY='GOOGLE_MAPS_API_KEY';
exportdefaultfunctionApp(){
return(
<APIProviderapiKey={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>
);
}
exportfunctionrenderToDom(container:HTMLElement){
constroot=createRoot(container);
root.render(
<React.StrictMode>
<App/>
</React.StrictMode>
);
}

JavaScript

importReactfrom'react';
import{createRoot}from'react-dom/client';
import{APIProvider,Map,AdvancedMarker}from'@vis.gl/react-google-maps';
constAPI_KEY='GOOGLE_MAPS_API_KEY';
exportdefaultfunctionApp(){
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"}))));
}
exportfunctionrenderToDom(container){
constroot=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.

gitclonehttps://github.com/googlemaps-samples/js-api-samples.git
cdsamples/rgm-basic-map
npmi
npmstart

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 defaultCenter and defaultZoom, we create an "uncontrolled" map component where the map manages its own state internally, while still initializing it to the correct location.
  • The mapId property 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.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026年09月01日 UTC.