react-google-maps: [BUG] You have included the Google Maps API multiple times on this page. This may cause unexpected errors.

I’m using a basic example and the script is added each time, I don’t know if this is the intended behaviour or am I missing something.

This is my component:

import React from "react"
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from "react-google-maps"

const Map =  withScriptjs(withGoogleMap((props) =>
  <GoogleMap
    defaultZoom={17}
    defaultCenter={{lat: 24.828797, lng: -107.441531}}
    center={props.location}
    defaultOptions={{ disableDefaultUI: true, gestureHandling: 'none' }}
  >
    {props.isMarkerShown && <Marker position={props.location} />}
  </GoogleMap>
))

export default Map

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 11
  • Comments: 33

Most upvoted comments

If you are having a script for google maps in your html element, then removing withScriptjs will solve the issue.

from Step 3: “If you don’t use withScriptjs, you have to put a <script/> tag for Google Maps JavaScript API v3 in your HTML’s <head/> element”

This can be solved by separating the compose() call that creates the render environment and the layout functional component like so:

const API_KEY = "aHR0cHM6Ly95b3V0dS5iZS9kUXc0dzlXZ1hjUQ=="

const mapEnvironment = compose(
  withProps({
    googleMapURL: `https://maps.googleapis.com/maps/api/js?key=${API_KEY}&v=3.exp&libraries=geometry,drawing,places`,
    loadingElement: <div style={{ height: `100%` }} />,
    containerElement: <div style={{ height: `400px` }} />,
    mapElement: <div style={{ height: `100%` }} />
  }),
  withScriptjs,
  withGoogleMap
);

const MapLayout = props => (
  <GoogleMap defaultZoom={8} defaultCenter={{ lat: -34.397, lng: 150.644 }}>
    {props.isMarkerShown && (
      <Marker position={{ lat: -34.397, lng: 150.644 }} />
    )}
  </GoogleMap>
);

const MapComponent = mapEnvironment(MapLayout);

I was getting this error when I was also adding the google maps library in the head of my html.

If the error is real and not a bug, you could try removing the withScriptjs higher order component and see if the map still shows up.

Have same problem with new version (@react-google-maps/api). What I found when I was trying to reload api scripts (as user language changed)was that I had to add window.google = undefined (after removing script tags from document), but before load them again

This happens on every component remount

@ping4tucker when I remove withScripts it throws an error saying “you must include withScripts” high-order component…

In my react app I use multiple map instances for some of the pages, but for others I do not. I do not want to put script in head, cos it will impact first load experience worse for everybody.

I managed to solve this by rendering the google map with a bit of delay

image image

In my Angular app, I used Google map api, when am entering the input filed throws this below error

You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors.

@SyedSaifAli We’ve managed to solve this issue by cleaninig up window.google object on Unmount in @react-google-maps/api library: https://www.npmjs.com/package/@react-google-maps/api

There is no possible solution for the current lib, cos it is unmaintained more than a year.

If you’re using a pre-render you might run into this when hydration occurs.

@strarsis try moving the script loader into a separate environment and override both modules so that only your code injects the script.

I tried to recreate this error with create-react-app, but wasn’t able to reproduce the issue.
https://github.com/tuccle22/google-maps-example/blob/master/src/App.js

Anyone have a repo that shows the issue?