question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Warning: Google Maps already loaded outside @googlemaps/js-api-loader.

See original GitHub issue

Describe the bug 🐛

I use @googlemaps/js-api-loader explicitly on a page where google-map-react is not used. However, when I navigate between this page and one that does use google-map-react (or vice-versa) I get the following warning:

Google Maps already loaded outside @googlemaps/js-api-loader. This may result in undesirable behavior as options and script parameters may not match.

I noticed that I can easily prevent this warning from showing by conditionally loading Google Maps on the page that explicitly uses @googlemaps/js-api-loader by checking to see if window.google exists or not (see minimal reproducible example below). However, I couldn’t find a way to do something similar with google-map-react?

Any help or suggestions would be greatly appreciated!

To Reproduce 🔍 Minimal reproducible example:

File A.js (conditionally uses @googlemaps/js-api-loader):

import React, { useState, useEffect } from 'react'
import { Loader } from '@googlemaps/js-api-loader'
import Link from 'components/elements/Link'

const A = () => {
    const [isMapApiLoaded, setIsMapApiLoaded] = useState(false)

    useEffect(() => {
        if (isMapApiLoaded || window.google) return
        const loader = new Loader({
            apiKey: process.env.API_KEY,
            libraries: ['places']
        })
        loader.load().then(() => setIsMapApiLoaded(true))
    }, [isMapApiLoaded])
    return (
        <div>
            <h1>A</h1>
            <Link href="/b">b</Link>
        </div>
    )
}

export default A

File B.js (uses google-map-react)

import React from 'react'
import GoogleMapReact from 'google-map-react'
import Link from 'components/elements/Link'

const B = () => {
    const apiHasLoaded = (map, maps) => {
        console.log('Loaded!')
    }

    return (
        <div>
            <GoogleMapReact
                defaultZoom={10}
                bootstrapURLKeys={{
                    key: process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY,
                    libraries: ['places']
                }}
                yesIWantToUseGoogleMapApiInternals
                onGoogleApiLoaded={({ map, maps }) => apiHasLoaded(map, maps)}
                options={() => ({
                    fullscreenControl: false
                })}
            ></GoogleMapReact>
            <Link href="/a">a</Link>
        </div>
    )
}

export default B

Environment:

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:19
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

14reactions
thalladacommented, Jun 18, 2021

I’m also getting this warning because I’m loading the Google Maps API via a script tag in the header of every page of my application:

<script
    type="text/javascript"
    src={`https://maps.googleapis.com/maps/api/js?key=${process.env.GOOGLE_API_KEY!}&libraries=places`}
/>

I’m doing this because I also need the API for react-google-places-autocomplete. And, this script tag is one method they suggest for loading the API.

My problem is that I also need to use the onGoogleApiLoaded function to center the map once loaded, and it’s not clear if that would work if I loaded the Google Maps API through the script tag instead of this library.

Does anyone actually know what issues this warning is alluding to? If I have two different components that are doing different things and only communicating to each other through state in my app, is there really a risk of things breaking by having both use different instances of the API? Or is it even two instances or one component overriding the version of the API the other component is using? If that’s the case I would just need to make sure both components expect the same version of the Google Maps API and I can safely ignore this warning right? I have yet to notice any issues in my application with this warning but I don’t know if I’m sitting on a ticking time bomb.

Also FYI, there’s already been a lot of discussion about this issue in https://github.com/google-map-react/google-map-react/issues/954. The author says that this issue was fixed in 2.1.9 but it seems like a lot of people are still reporting the warning.

9reactions
khevamanncommented, Aug 18, 2021

@itsmichaeldiego I looked into it and it looks like it already works as is. All additional props from bootstrapURLKeys are automatically passed into the loader, and js-api-loader accepts an id like I mentioned above. So the code below works correctly. I put in a PR to update this in the types definition. Is there somewhere I should add this to documentation.

<GoogleMapReact
        bootstrapURLKeys={{ key: API_KEY, libraries: ['places'], id: 'CUSTOM_SCRIPT_ID' }}
        defaultCenter={defaultProps.center}
        defaultZoom={defaultProps.zoom}
      />

Then to use google maps script anywhere else on the page, you can use

const loader = new Loader({
  apiKey: "",
  id: "__googleMapsScriptId",
  version: "weekly",
  libraries: ["places"]
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to check if Google Maps API is loaded? - Stack Overflow
I don't think checking the existence of google.maps is enough to be sure the library has finished loading. Here are the network requests...
Read more >
Error Messages | Maps JavaScript API - Google Developers
A warning is a supplemental message about the loading of the Maps JavaScript API. The warning describes the possible reasons for an error,...
Read more >
Turn Google Maps notifications on or off - Android
On your Android phone or tablet, open the Google Maps app Maps . Tap your profile picture or ... Navigation warnings for nearby...
Read more >
@googlemaps/js-api-loader - npm
Wrapper for the loading of Google Maps JavaScript API script in the browser. Latest version: 1.15.1, last published: 2 months ago.
Read more >
React Google Maps Api Style Guide
React hook based on new official way to load googlemaps script. ... This event is fired when the mouse moves out of a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found