google-maps-loader icon indicating copy to clipboard operation
google-maps-loader copied to clipboard

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

Open mrleblanc101 opened this issue 3 years ago • 3 comments

In Nuxt I have this error, I can check for window.google before loading, but should the plugin handle this all on it's own ?

mrleblanc101 avatar Nov 05 '21 18:11 mrleblanc101

@mrleblanc101 would you mind sharing how you fixed this on your end. I have the same issues in the VueJS, Laravel, InertiaJS app and get the same issue when going back to a page that loaded previously.

Thank you!

Orrison avatar Dec 16 '21 00:12 Orrison

Use the official @googlemaps/js-api-loader

mrleblanc101 avatar Dec 16 '21 01:12 mrleblanc101

I dunno, if there are others struggling with this still, but the work-around is to pass back google maps from the window if it exists, then if not load it, and wait for it to load before trying to check again.

  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);
    }
  }

popcorn245 avatar Jun 18 '22 21:06 popcorn245