Closed Lars-Sommer closed 1 year ago
The GoogleMap component can accept a promise that resolves to the global google
object if you need to use the google api elsewhere. See the example here https://github.com/inocan-group/vue3-google-map/issues/99#issuecomment-1237395931
Thanks alot - it works 👍
But doing so then the duplicate console warning appears:
index.ts:8 Google Maps already loaded outside @googlemaps/js-api-loader.This may result in undesirable behavior as options and script parameters may not match.
I guess this is because I first load google maps via the loader, and when the <GoogleMap>
component loads it also loads another google maps instance.
Any idea?
Can you provide a minmial reproduction so I can take a look? You can use vite.new/vue
While creating a reproduction repo, I found out what the error was. I was not returning a promise to the <GoogleMap
component, but the result of the promise:
const loader = await new Loader({
apiKey: 'MYKEY',
libraries: ['drawing', 'geometry', 'places'],
language: 'da-DK',
region: 'DA'
});
const googleMapsLoader = await loader.load();
window.google = googleMapsLoader;
this.vueMapsPromise = googleMapsLoader;
Returning the promise instead, made the warning disappear:
const loader = await new Loader({
apiKey: 'MYKEY',
libraries: ['drawing', 'geometry', 'places'],
language: 'da-DK',
region: 'DA'
});
const googleMapsLoader = loader.load();
this.vueMapsPromise = googleMapsLoader;
window.google = await googleMapsLoader;
this.center = new google.maps.LatLng(56.26392, 9.501785);
Thanks alot for your help :)
Glad you got it sorted :)
I have installed vue3-google-map with yarn which now works in my .vue file:
In another .ts file I try to access the google namespace which is not a problem in the editor linting-wise:
But when the site is compiled in the browser I get this error:
Adding a reference to the google api in the index.html file removes the problem:
<script src="https://maps.googleapis.com/maps/api/js?key=myKey"></script>
... but I guess this is pretty bad practice, and the console also throws me a warning:
Google Maps already loaded outside @googlemaps/js-api-loader.This may result in undesirable behavior as options and script parameters may not match.
This is my package.json:
So how do I make the google namespace accessible from other than .vue files where I directly import
'GoogleMap'
?