5

I am using google-maps-react in my project.

Now i need to integrate places api inside the maps.

But it seems there is some load order issue and so i am getting the following error.

Error: [react-places-autocomplete]: Google Maps JavaScript API library must be loaded. See: https://github.com/kenny-hibino/react-places-autocomplete#load-google-library

If i remove the places component, the maps works fine.

I need both the components to work together.

Any idea on how to fix this?

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
prajeesh
  • 2,202
  • 6
  • 34
  • 59

3 Answers3

9

As the error states Google Maps JavaScript API library must be loaded, the following options could be considered. To load statically by adding a reference to public/index.html:

<script src="https://maps.googleapis.com/maps/api/js?key=--GOOGLE-MAPS-KEY--&libraries=places"></script-->

or, for example, via google-maps-react GoogleApiWrapper HOC like this:

export default GoogleApiWrapper({
  apiKey: "--GOOGLE-MAPS-KEY--",
  libraries: ["places"]
})(MapContainer);

This demo demonstrates how to integrate react-places-autocomplete and google-maps-react

Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
5

i use @react-google-maps/api. the solution for me was to add places to libraries field

const {isLoaded} = useJsApiLoader({
    id: 'google-map-script',
    googleMapsApiKey: process.env.REACT_APP_GOOGLE_API_KEY as string,
    libraries: ["places"]
});
Ivan Yulin
  • 794
  • 7
  • 15
3

I was having this issue because I was loading google-map-react and react-google-autocomplete on the same page. I had to tell my google-map-react component to load the places library as well like this:

<GoogleMapReact 
   bootstrapURLKeys={{ 
      key: API_KEY,
      libraries:['places'],
   }} 
/>

The Map component loads the google script first, but does not include the places library unless specified. When you load the autocomplete component after, google sees you already have the map script loaded, so it does not add it again. This is why you must specify all the libraries on the component that loads the google maps script first.

You can read more about this issue here

Dustin Spengler
  • 5,478
  • 4
  • 28
  • 36