Use Google Maps API in VueJS

Tram Ho

Introduce

Lately I have to work a bit with google map but in vue. And if you learn about google map, you also know to integrate google map into your website, you will have to insert a google script link, but if you work with SPA (here I say Laravel + Vue) then you also know there will be only one main blade page from which to render the Vue pages. You can also quickly insert the page, but obviously that’s not a good way. After that, I had to learn more and found out how to use google map in Vue more useful. Check it out below.

Well this article will only guide how to integrate google map into vue without showing how to configure google map from console. That means this article will go from the fact that you already have an api key of google map already.

vue2-google-maps Plugin

If you search google with the keyword ” using google map in vuejs ” then you will surely see the plugin name in the first results.

Choosing this way has an advantage of being fast and convenient. You just need to run the install command and with a few lines of basic code is integrated, ready to use.

https://github.com/xkjyeah/vue-google-maps

And according to its documentation, the first thing I have to do is use the command to install it into my project

npm install vue2-google-maps

Next, you will create a js file to declare to the vue that I will use this plugin, there are two directions, if you want to go anywhere you can call the vue component and do not have to declare again. then you will declare immediately in the js file used to render vue into the laravel blade. Kind of, it’s the js file with this code

Then also if you do not have the need to use map in many places but in just a few pages, it is recommended to split it into a separate js file in the directory where the shared ones can be called back (usually utils ). . utils/gmaps.js just name it utils/gmaps.js . And on the page you want to use you just call

is done. Note that the path you call to this utils directory must be correct.

The content of the declaration will be as follows.

And the setup is just like that, From here you can instantiate your map using the component in the vue template.

However, besides the advantage of being quick, this method has a bit of a downside. In the process of using it, at first I thought it was okay to do so, but my demand started to get bigger. I need to use some additional google services for example. In the doc, the description is as simple as follows

However, I did it and I still struggled and was told that the value of google is null / undefine because js is asynchronous, but the google guy has not loaded, the place I need to use has already run. , and after a hard time searching and searching, there is a temporary way to fix that is to use the call this.$gmapApiPromiseLazy().then(//... your code here) to make sure your google guy you have finished loading. ( https://github.com/xkjyeah/vue-google-maps/wiki/vue-google-maps-FAQ#where-is-my-google )

This is a bit of a nuisance that I feel, and besides, surely this guy has the disadvantages of a plugin as required when you use it larger than what this plugin offers. Obviously if you need something that google service has provided but this plugin has not yet developed, you will have to wait until this developer develops to the part you need, but do not know how to wait. until ever.

Fortunately for me, after a long time searching on google, I have found a pretty good way to use map in vue. I would like to share again below.

Loading Google Maps API

References: https://markus.oberlehner.net/blog/using-the-google-maps-api-with-vue/

As far as I understand, this integration will basically rely on you creating script element and appending to the page header. You may wonder what I said at the beginning is what it means to put the script into the blade or now want to put it in. The other thing that I put in is that all the pages I will load and for this one I will write a function in my own js file. Where I need it, I declare it to append to it.

Let’s try it out together.

I will also create an utils folder with gmaps file similar to the above method with the following content:

After reading the above comments and code, you will understand all or part of the idea of ​​using google map in vue.
So now with the need to use google map, I just need to await this init function to get the google object object and use it.

Try it out:

The code above shows an example of using Geocoder to convert an address from a string to a coordinate form and thereby create a basic map.

Then now with the creation of the google object: google object, you can use google map in vue like in the google documentation already. You can freely do without fear of being restricted anymore, just google will grant: p.

Good luck!

Share the news now

Source : Viblo