Integrating Google Maps in vue.js

后端 未结 6 1233
情书的邮戳
情书的邮戳 2020-12-08 15:24

I\'ve been trying to initialize a Google map on my vue.js project while including the script :



        
6条回答
  •  抹茶落季
    2020-12-08 15:45

    I was searching for a different issue and found this one, there is another way that you could achieve this without having to add it in index.html.

    I faced a similar problem where I had to use two Google API keys for different environments so hard-coding it in to index.html was not a good idea, I did this if it helps anyone -

    main.js

    export const loadedGoogleMapsAPI = new Promise( (resolve,reject) => {
    
          window['GoogleMapsInit'] = resolve;
    
          let GMap = document.createElement('script');
    
          GMap.setAttribute('src',
         `https://maps.googleapis.com/maps/api/js?key=${process.env.GOOGLE_API_KEY}&callback=GoogleMapsInit®ion=IN`);
    
          document.body.appendChild(GMap); 
    });
    

    MapElem.vue

    
    
    
    

    It's pretty straightforward, you write a Promise in main.js which will resolve to the callback initiated by Google script ( which we dynamically appended to the body ). Once the Promise is resolved you can access the map object in your component.

提交回复
热议问题