Places UI Kit: A ready-to-use library that provides room for customization and low-code development. Try it out, and share yourinput on your UI Kit experience.

React Google Maps Library - Place Autocomplete

  • This example demonstrates the integration of the Places Autocomplete widget within a React application to dynamically update a map and marker.

  • It leverages thevis.gl/react-google-maps library, providing React components for interacting with the Google Maps JavaScript API.

  • The provided code snippets include TypeScript, JavaScript, CSS, and HTML to showcase the complete implementation.

  • Although thevis.gl/react-google-maps library is open source and not covered by Google Maps Platform support, the underlying Google Maps services used are still subject to the Google Maps Platform Terms of Service.

This example shows using the Places Autocomplete widget to update a map andmarker ina React application. It uses thevis.gl/react-google-maps opensource library.Thevis.gl/react-google-maps libraryis a collection of React components and hooks for the Google Maps JavaScriptAPI.

Important: Thevis.gl/react-google-maps libraryis offered using an open source license. It is not governed by the Google MapsPlatform Support Technical Support Services Guidelines, the SLA, or theDeprecation Policy (however, any Google Maps Platform services used by thelibrary remain subject to the Google Maps Platform Terms of Service). If youfind a bug, or have a feature request, file an issue onGitHub .

TypeScript

importReact,{useState,useEffect,useRef}from'react';import{createRoot}from'react-dom/client';import{APIProvider,ControlPosition,MapControl,AdvancedMarker,Map,useMap,useMapsLibrary,useAdvancedMarkerRef,AdvancedMarkerRef}from'@vis.gl/react-google-maps';constAPI_KEY=globalThis.GOOGLE_MAPS_API_KEY??("YOUR_API_KEY");constApp=()=>{const[selectedPlace,setSelectedPlace]=useState<google.maps.places.PlaceResult|null>(null);const[markerRef,marker]=useAdvancedMarkerRef();return(<APIProviderapiKey={API_KEY}solutionChannel='GMP_devsite_samples_v3_rgmautocomplete'><MapmapId={'bf51a910020fa25a'}defaultZoom={3}defaultCenter={{ lat: 22.54992, lng: 0 }}gestureHandling={'greedy'}disableDefaultUI={true}><AdvancedMarkerref={markerRef}position={null}/></Map><MapControlposition={ControlPosition.TOP}><divclassName="autocomplete-control"><PlaceAutocompleteonPlaceSelect={setSelectedPlace}/></div></MapControl><MapHandlerplace={selectedPlace}marker={marker}/></APIProvider>);};interfaceMapHandlerProps{place:google.maps.places.PlaceResult|null;marker:google.maps.marker.AdvancedMarkerElement|null;}constMapHandler=({place,marker}:MapHandlerProps)=>{constmap=useMap();useEffect(()=>{if(!map||!place||!marker)return;if(place.geometry?.viewport){map.fitBounds(place.geometry?.viewport);}marker.position=place.geometry?.location;},[map,place,marker]);returnnull;};interfacePlaceAutocompleteProps{onPlaceSelect:(place:google.maps.places.PlaceResult|null)=>void;}constPlaceAutocomplete=({onPlaceSelect}:PlaceAutocompleteProps)=>{const[placeAutocomplete,setPlaceAutocomplete]=useState<google.maps.places.Autocomplete|null>(null);constinputRef=useRef<HTMLInputElement>(null);constplaces=useMapsLibrary('places');useEffect(()=>{if(!places||!inputRef.current)return;constoptions={fields:['geometry','name','formatted_address']};setPlaceAutocomplete(newplaces.Autocomplete(inputRef.current,options));},[places]);useEffect(()=>{if(!placeAutocomplete)return;placeAutocomplete.addListener('place_changed',()=>{onPlaceSelect(placeAutocomplete.getPlace());});},[onPlaceSelect,placeAutocomplete]);return(<divclassName="autocomplete-container"><inputref={inputRef}/></div>);};constroot=createRoot(document.getElementById('app')!);root.render(<App/>);exportdefaultApp;
Note: Read theguide on using TypeScript and Google Maps.

JavaScript

importReact,{useState,useEffect,useRef}from"react";import{createRoot}from"react-dom/client";import{APIProvider,ControlPosition,MapControl,AdvancedMarker,Map,useMap,useMapsLibrary,useAdvancedMarkerRef,}from"@vis.gl/react-google-maps";constAPI_KEY=globalThis.GOOGLE_MAPS_API_KEY??"YOUR_API_KEY";constApp=()=>{const[selectedPlace,setSelectedPlace]=useState(null);const[markerRef,marker]=useAdvancedMarkerRef();return(<APIProviderapiKey={API_KEY}solutionChannel="GMP_devsite_samples_v3_rgmautocomplete"><MapmapId={"bf51a910020fa25a"}defaultZoom={3}defaultCenter={{ lat: 22.54992, lng: 0 }}gestureHandling={"greedy"}disableDefaultUI={true}><AdvancedMarkerref={markerRef}position={null}/></Map><MapControlposition={ControlPosition.TOP}><divclassName="autocomplete-control"><PlaceAutocompleteonPlaceSelect={setSelectedPlace}/></div></MapControl><MapHandlerplace={selectedPlace}marker={marker}/></APIProvider>);};constMapHandler=({place,marker})=>{constmap=useMap();useEffect(()=>{if(!map||!place||!marker)return;if(place.geometry?.viewport){map.fitBounds(place.geometry?.viewport);}marker.position=place.geometry?.location;},[map,place,marker]);returnnull;};constPlaceAutocomplete=({onPlaceSelect})=>{const[placeAutocomplete,setPlaceAutocomplete]=useState(null);constinputRef=useRef(null);constplaces=useMapsLibrary("places");useEffect(()=>{if(!places||!inputRef.current)return;constoptions={fields:["geometry","name","formatted_address"],};setPlaceAutocomplete(newplaces.Autocomplete(inputRef.current,options));},[places]);useEffect(()=>{if(!placeAutocomplete)return;placeAutocomplete.addListener("place_changed",()=>{onPlaceSelect(placeAutocomplete.getPlace());});},[onPlaceSelect,placeAutocomplete]);return(<divclassName="autocomplete-container"><inputref={inputRef}/></div>);};constroot=createRoot(document.getElementById("app"));root.render(<App/>);exportdefaultApp;
Note: The JavaScript is compiled from the TypeScript snippet.

CSS

body{margin:0;font-family:sans-serif;}#app{width:100vw;height:100vh;}.autocomplete-containerinput,.autocomplete-control{box-sizing:border-box;}.autocomplete-control{margin:24px;background:#fff;}.autocomplete-container{width:300px;}.autocomplete-containerinput{width:100%;height:40px;padding:012px;font-size:18px;}.autocomplete-container.custom-list{width:100%;list-style:none;padding:0;margin:0;}.autocomplete-container.custom-list-item{padding:8px;}.autocomplete-container.custom-list-item:hover{background:lightgrey;cursor:pointer;}

HTML

<html>  <head>    <title>React Google Maps - Autocomplete</title>    <link rel="stylesheet" type="text/css" href="./style.css" />  </head>  <body>    <div></div>    <script type="module" src="./index"></script>  </body></html>

Try Sample

Clone Sample

Git and Node.js are required to run this sample locally. Follow theseinstructions to install Node.js and NPM. The following commands clone, install dependencies and start the sample application.

gitclone-bsample-rgm-autocompletehttps://github.com/googlemaps/js-samples.gitcdjs-samplesnpminpmstart

Other samples can be tried by switching to any branch beginning withsample-SAMPLE_NAME.

gitcheckoutsample-SAMPLE_NAMEnpminpmstart

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

Last updated 2025-11-21 UTC.