Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
benderlidze opened this issue Dec 16, 2020 · 29 comments

Comments

@benderlidze
Copy link

Hi, thanks for your component, it is great.
I have a leaflet map with react-leaflet-google-layer and it is using google API too.
Got a problem with this You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors.
I tried to add/remove <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key="> to different parts of the index.html, does not help.
"react-google-autocomplete": "^1.2.6",
Is there any way to fix this?
Thanks.

@cgmndev
Copy link

cgmndev commented Jan 11, 2021

I have the same problem when I use it in conjunction with the react-google-places-autocomplete library

@adrielgro
Copy link

I have the same problem when i use with react-google-autocomplete

@ErrorPro
Copy link
Owner

@adrielgro @claudiomnec @benderlidze Hi! I've just released a new version(v2) which should fix that issue. Please let me know if you still encounter it. Thanks!

@pekac
Copy link

pekac commented May 14, 2021

Hey @ErrorPro!

Got something related to this issue.

I am using:

  • google-map-react v2.1.9
  • react-google-autocomplete v2.1.2

The error I'm getting is:
Google maps places API must be loaded

Ofc, once the map is removed from the same page - the autocomplete works properly!
Also, the map component works both with the autocomplete and without it :)

@ErrorPro
Copy link
Owner

ErrorPro commented May 19, 2021

hey @pekac ! Thanks for your message. We tried to reproduce the issue but unfortunately, we could not. Could you add some more info or show/send the piece of code how you are using it?
Also, how do you add the google script to your website? It seems like you have google but do not have google maps places
https://monosnap.com/file/RDqGF7o5Dx4kyivGCykZNMAxJoBRow
https://monosnap.com/file/kskdTqChukqlS7Wbaccfcy1uL7bGdg

@pekac
Copy link

pekac commented May 20, 2021

Hey @ErrorPro ! Thanks for looking into it so deeply!

It seems like you have google but do not have google maps places

Definitely looks that way.

Give me some time to spin up a min-reproducible repo. I believe this will be the easiest way to pinpoint the issue.
Will share it with you afterwards :)

@thebiltheory
Copy link

✋🏽 Same here.

Using:

  • "@react-google-maps/api": "^2.1.1",
  • "react-google-autocomplete": "^2.1.2",
You have included the Google Maps JavaScript API multiple times on this page.
This may cause unexpected errors.


Uncaught (in promise) TypeError: Cannot read property 'um' of undefined

@ErrorPro
Copy link
Owner

ErrorPro commented May 21, 2021

@thebiltheory Hey, is it possible to share a piece of code(if it's the same component). I want to understand the flow, which lib first gets to load google API, thanks! P.S. I have some ideas about what might cause it and I am checking it out

@ErrorPro
Copy link
Owner

I just found out the way to reproduce it. The problem is that autocomplete library needs places API to be loaded. The only way to load places API via URL. So what happens is: somewhere in the code you already require google map API(any map library or directly) without places API in the URL so that this library sees google available but cannot get access to google.maps.places. The easiest way would be to paste the full URL with all libraries included in the HTML e.g.

<script
  type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=apiKey&libraries=places,anyOtherLib"
></script>

@GarryOne
Copy link

GarryOne commented Oct 19, 2021

If you're using this package alongside with google-map-react, you can just tell the last package to include the places api.

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

@izzyworks89
Copy link

How? @GarryOne
I am using both.
"@react-google-maps/api": "^2.1.1",
"react-google-autocomplete": "^2.1.2",
How do i do that

@JAndresHJGlobant
Copy link

@izzyworks89 You could try by setting the API key and the places library by using the useLoadScript provided by @react-google-maps/api. I mean something like this:

const { isLoaded, loadError } = useLoadScript({ googleMapsApiKey: API_KEY, libraries: ['places'], });

@im-tridevsharma
Copy link

Use this code
const { isLoaded, loadError } = useLoadScript({ googleMapsApiKey: API_KEY, libraries: ['places'], });

And when rendering Autocomplete, Please enclose Autocomplete in
{isLoaded && <AutoComplete />}

@bek-sidikovich02
Copy link

Hi guys!
I have been using react-google-autocomplete so far, but it can not show usa regions and cities also street number, pls
I need to have your favour

@mschipperheyn
Copy link

One thing to be aware of that even with useLoadScript, you have to make sure that both urls are identical, i.e. contain the same parameters in the same order. I recommend loading the script in a separate component

const MyPlacesComponentImpl = ({ googleMapsScriptBaseUrl }) => { 
    const {
        placesService,
        placePredictions,
        getPlacePredictions,
        isPlacePredictionsLoading,
    } = usePlacesService({
        googleMapsScriptBaseUrl,
    });
   [...] 
}

const libraries = ['places'];
const MyPlacesComponent = ({ googleMapsApiKey, language }) => {
    const { isLoaded, loadError, url } = useLoadScript({
        googleMapsApiKey,
        id: 'google-map',
        libraries,
        language,
    });

    if (!isLoaded) {
        return <Loading />;
    }
    if (loadError) {
        return <LoadingError />;
    }
    return (
        <MyPlacesComponentImpl googleMapsScriptBaseUrl={url} />
    );
}

export default MyPlacesComponent;

@popcorn245
Copy link

If using one of the loaders you will need to do a check to see if google maps is already loaded and then use the already existing instance, and you will also need to check to see if the loader exists so that you can wait for the scripts to finish loading before trying again. Here is my code, hope this helps someone who is as lost as I was lol. ^_^

  sleep(ms) {
    return new Promise((resolve) => setTimeout(resolve, ms));
  }

  async loadGoogleMaps(options?: LoaderOptions) {
    if (window?.google) return window.google;
    if ((window as any)?._dk_google_maps_loader_cb) {
      await this.sleep(200);
      return this.loadGoogleMaps();
    }
    try {
      const loader = new Loader(this.googleMapsKey, {
        libraries: ["places"],
        ...options,
      });

      return loader.load();
    } catch (e) {
      console.log(e);
      setTimeout(this.loadGoogleMaps.bind(this), 2000);
    }
  }

NOTE: I am using google-maps package
https://github.com/davidkudera/google-maps-loader#readme

@vitorcosta039
Copy link

@thebiltheory did you manage to solve the problem? i have the same settings as you

@bek-sidikovich02
Copy link

bek-sidikovich02 commented Jul 19, 2022 via email

@bek-sidikovich02
Copy link

bek-sidikovich02 commented Oct 11, 2022 via email

@9alex12
Copy link

9alex12 commented Dec 12, 2022

I had the same problem, to solve it replace 'useJsApiLoader' by 'useLoadScript' both are from the @react-google-maps/api library.
const {isLoaded} = useJsApiLoader({ googleMapsApiKey: YOUR_API_KEY, libraries: ['places'], })

const {isLoaded} = useLoadScript({ googleMapsApiKey: YOUR_API_KEY, libraries: ['places'], })

@raja-muhammad-asher
Copy link

I had the same problem because of using useLoadScript with and without places in two different components. In one component, places were not required but in the other I needed 'places'. I made the call to useLoadScript by including 'places' in both components and that removed this problem.

const {isLoaded} = useLoadScript({ googleMapsApiKey: YOUR_API_KEY, libraries: ['places'], })

@AlainYRS
Copy link

AlainYRS commented Feb 8, 2023

I have the same problem but:
-I am working in two independent components to be used on demand when they were required at any moment in any project so they can't share a url or anything.
-What could I do when both components are needed in the same project since they suppose to be provided with props/parameters (ApiKey) without any code modification?

@raja-muhammad-asher
Copy link

I have the same problem but: -I am working in two independent components to be used on demand when they were required at any moment in any project so they can't share a url or anything. -What could I do when both components are needed in the same project since they suppose to be provided with props/parameters (ApiKey) without any code modification?

Please use
const {isLoaded} = useLoadScript({ googleMapsApiKey: YOUR_API_KEY, })

in both components. It should resolve your problem

@AlainYRS
Copy link

I have the same problem but: -I am working in two independent components to be used on demand when they were required at any moment in any project so they can't share a url or anything. -What could I do when both components are needed in the same project since they suppose to be provided with props/parameters (ApiKey) without any code modification?

Please use const {isLoaded} = useLoadScript({ googleMapsApiKey: YOUR_API_KEY, })

in both components. It should resolve your problem

Thanks, it can fix my multiple Api calls but unfortunately there is a new error "ReferenceError: google is not defined" since I use GoogleMap too.

Do you have an idea of how could it be fixed?

@hayatkhan1196
Copy link

it working for me
const {isLoaded} = useLoadScript({ googleMapsApiKey: YOUR_API_KEY, libraries: ['places'], })

@ewarrenG
Copy link

Has anyone experienced this across two separate components? In one I use autocomplete, in another I use maps. In the autocomplete example, I follow the example material ui provides. In the maps example, I follow the example react google maps provides.

Has anyone come up with a solution to load the Google Maps API once for these two different usecases?

Thanks in advance!

@CamiloTass
Copy link

CamiloTass commented Jul 29, 2024

Hi, I see that there are many examples but I keep getting the same problem. When I load the google-places-autocomplete component I keep getting the problem that: You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors. And when both things get blocked, the Google map stops searching for places. Here I leave the code to know if I am doing something wrong or if someone found the solution and can help me.


const API_KEY_GOOGLE_MAPS = import.meta.env.VITE_API_KEY_GOOGLE_MAPS

export const CreateTravel = () => {
	return (
<div className='w-[40%]'>
<APIProvider apiKey={API_KEY_GOOGLE_MAPS}>
					<MapGoogle
						customControlPermission
						MapHandlerPermission
						UndoRedoControlPermission
						dispatch={dispatch}
						state={state}
						selectedPlace={selectedPlace}
					/>
				</APIProvider>
								<span>lugar de inicio</span>
								<GooglePlacesAutocomplete
									apiKey={API_KEY_GOOGLE_MAPS}
									autocompletionRequest={{
										bounds: [{ lat: 50, lng: 50 }]
									}}
									onLoadFailed={(error) =>
										console.error('Could not inject Google script --------------', error)
									}
									selectProps={{
										value,
										onChange: setValue
									}}
								/>
							</div>
	)
}

@gorillah
Copy link

gorillah commented Sep 12, 2024

"use client";

import { LoadScript, Libraries } from "@react-google-maps/api";
import React, { useState, useEffect } from "react";

const GOOGLE_LIBRARIES: Libraries = ["places", "marker"];

const LoadGoogleProvider = ({ children }: { children: React.ReactNode }) => {
  const [isLoaded, setIsLoaded] = useState(false);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    if (!process.env.NEXT_PUBLIC_GOOGLE_API_KEY) {
      setError("Google Maps API key is missing.");
    }
  }, []);

  const onLoad = () => setIsLoaded(true);
  const onError = () => setError("Failed to load Google Maps");

  return process.env.NEXT_PUBLIC_GOOGLE_API_KEY ? (
    <LoadScript
      googleMapsApiKey={process.env.NEXT_PUBLIC_GOOGLE_API_KEY}
      libraries={GOOGLE_LIBRARIES}
      onLoad={onLoad}
      onError={onError}
    >
      {isLoaded ? children : <p>Loading Google Maps...</p>}
      {error && <p>{error}</p>}
    </LoadScript>
  ) : (
    <p>{error}</p>
  );
};

export default LoadGoogleProvider;

this worked for me, make sure to wrap with layout where your using google components

@penderuk
Copy link

penderuk commented Oct 10, 2024

I just found out the way to reproduce it. The problem is that autocomplete library needs places API to be loaded. The only way to load places API via URL. So what happens is: somewhere in the code you already require google map API(any map library or directly) without places API in the URL so that this library sees google available but cannot get access to google.maps.places. The easiest way would be to paste the full URL with all libraries included in the HTML e.g.

<script
  type="text/javascript"
  src="https://maps.googleapis.com/maps/api/js?key=apiKey&libraries=places,anyOtherLib"
></script>

Thanks, @ErrorPro! This worked for me. I'm using react-google-autocomplete and @vis.gl/react-google-maps on the same page.

Update:
I tested it further and encountered the same errors again. What I did was pass apiKey="" to the APIProvider of @vis.gl/react-google-maps since react-google-autocomplete already initializes the Maps script before the map is rendered. This means the Maps script is already in the DOM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests