Javascript Distance Matrix API Example to Find Distance & Routes Between Two Places and Show in Maps
13K views
Jan 9, 2025
Buy the full source code of the application here: https://procodestore.com/index.php/product/javascript-distance-matrix-api-example-to-find-distance-routes-between-two-places-and-show-in-google-maps/ Official Website: https://freemediatools.com
View Video Transcript
0:00
uh hello guys welcome to this video so in this video I will talk about uh how to use distance Matrix API of Google
0:07
Maps API so actually calculate the distance between two locations and also show the different routes and directions
0:14
in the Google Maps it's a full project right here and you can see the interface of the application we have two Fields
0:20
out there we can enter the from location and the two location and we will also be
0:25
having autocomplete right here so let's suppose if I enter any location I need to calculate the distance between Delhi
0:32
and Bal so I will just simply write the location so as you can see as I writing the locations autoc complete suggestions
0:38
are coming right here from Google Maps autoc complete API so if I just enter
0:44
these two locations and if I want to get the directions driving distance so as I
0:50
click the button so what you will see that it will show you me the actual distance in the Google Map how basically
0:58
we can travel from one location to other the location and there is a second button out there which will be actually
1:04
calculating the distance so as you click the distance it will show you the distance between these two locations
1:09
which is 791 kilm so you can see you can move from this location and as you zoom
1:16
this it will actually tell you the actual locations and the different routes that you need to travel to
1:23
actually travel from this Delhi to bopal so you will see this is very useful if
1:28
you are traveling from one location to other location you can use this Google Maps simple route and it will actually
1:35
you can see if you zoom in right here it will tell you the exact
1:41
routes how to travel this you can zoom in right here
1:47
more this is really useful guys if you can basically take a screenshot of it
1:53
and then if you are just traveling it you can see in which location you need to move so it's very useful
2:06
so in this way we basically we doing this in this video we can calculating
2:12
distance and the different routes so for this we are using Google distance Matrix
2:18
API so you can just change this location to any
2:24
location now if you click get direction so you will now see it will now be showing the direction C from this
2:33
location calculate distance and now this distance becomes 1,484 kilm
2:42
so you can see that so it basically shows you the full route and the
2:50
directions and the API is called as distance Matrix API guys so if you go to Google Maps platform documentation and
2:59
it basically it's not a free API guys it does give you $200 free credits after
3:05
that you need to upgrade to a plate paid plan so you do need to wisely call these API calls so if you're just testing out
3:13
out you can have $200 I think 1,000 request are free after that you need to go to the monthly
3:20
plan and uh you need to now create basically this
3:26
uh Cloud console account I've already created that and then you need to go to
3:31
API services and enable this API called as Google distance Matrix API so just
3:37
search for this API called as distance Matrix and uh the very this
3:44
is the API that we are using to actually calculate the distance and get the directions I've already enabled this API
3:51
and then you need to also create API key simply go to create credentials and
3:58
click on this button API key so it's it will give you your API key simply copy that and store it at a
4:05
location I've already copy pasted this right here if you see that so if you need the full source code of this
4:11
project the link is given in the description you can directly purchase the full source code the link is given in the description so now let's get
4:18
started by building this application so I will simply delete all the source code and start from
4:26
scratch I will delete all this
4:35
so for basically styling this application guys we are actually using Tailwind CSS I've already included the
4:42
CDN for this simply include the CDN for tailwind and that's all that we need to
4:48
do so the very first thing we need to do after the title we just need to include the CDM for the Google Maps API so https
4:58
maps. Google ap.com slmap SL API
5:04
sljs and then we need to paste the API key so this API key you just need to
5:11
copy from Google Cloud console so I already created this API
5:18
key simply I will copy this so I will delete this API key guys so just don't
5:24
copy my API key and then we will simply say
5:30
we will also be including the places API as well so just need to include this and
5:38
libraries is equal to places we are also using Google Places API to actually fetch the information about the
5:44
places so after you do this we will now be writing our
5:51
body so we will attach this onload event so whenever your body is loaded we will
5:57
be actually be executing this function initialize map so with this function we will be executing code to actually
6:06
display the Google Map in the browser so you will basically be attaching this
6:11
Tailwind class BG gray 100 and then this will be a container
6:20
class MX Auto these are all Tailwind
6:27
classes so inside this we will have Flex justify Center just to basically Center
6:35
everything mb4 so input type text so here we will
6:42
be giving an ID to it of
6:48
from and we'll give it a placeholder so here the user will be allowed to enter
6:56
their from locations so simply I will say from
7:05
so we'll be giving a class to it Tailwind class so w width will be
7:12
64 padding y from 2 PX4 and it should be rounded large Shadow will be medium and
7:21
it will be Focus outline n Focus
7:28
ring FOC Focus border blue 300 margin from the right four these are
7:36
just all Tailwind classes guys so which is very simple if I save this project and so you will basically see this from
7:44
is added right here similarly we will be adding the
7:50
next location same styling will be there so I will copy this paste
7:56
it so we'll change the ID this will be two and here the ID will change from
8:03
from to two so there will be two input Fields
8:09
added the from and the two if you see now we just need a simple div
8:17
section where we will be actually be displaying the map so Flex
8:24
justify Center inside this we will have a button
8:30
this will have a class of BG blue 500 so when we hover onto this butter
8:37
the background color will change BG blue 700 shade and the color of the button will
8:44
be white text white font of the button will be font
8:50
bold py2 PX 4 button will be
8:57
rounded and focus outline
9:03
none and
9:08
focus Shadow outline margin from right so all the we have given all the same
9:15
styling that we have given to the input field and uh we will simply say get
9:26
directions so we have this button right here if you
9:37
see so same thing we will be giving the second button which will be useful for
9:43
calculating
9:49
the distance so I will copy this so
9:57
this this is called as calate
10:02
distance so get directions calculate distance so these two buttons are
10:12
there so after
10:18
this we will actually be giving a div an ID of
10:24
map and we also be giving a class to it of margin top top eight
10:31
this is mt8 margin from the top position 8 and we'll also be giving a style to it of
10:38
height will be 600 pixel this is overall uh the UI of the
10:45
application if you see uh this is from and this is
10:52
to I think I made a mistake right
10:58
here let me copy
11:17
this refresh now you can see this is the correct interface you can directly purchase the link is given in the
11:24
description so now what we will do
11:29
we now need to actually basically write this function in it map so whenever your body is loaded this function will
11:35
automatically execute so what I will do is that I will simply include our
11:40
Javascript file right here in a separate script.js so just we have our script.js
11:54
file so here you'll actually write the JavaScript code which is required so first of all we will declare some
12:00
variables the map directions
12:07
service directions render these are all variables which will be used in this application so I
12:13
will just declaring it at the very top from input two input to actually get the
12:20
from location and the two location so these are all the variables now we'll be declaring the init map
12:27
function which will get execute it automatically so we are basically first
12:33
of all be showing the map so for showing the map you can use google. maps.
12:40
map here we need to Simply set the reference so we are given this reference
12:45
in the HTML the map element we have given an ID to it we are targeting it by
12:50
the this function and the second parameter it takes is the actual zoom level which is I will set it to eight
12:58
and Center we will Center out the location by providing latitude which is
13:06
37.72 7749 and the longitude which will be -
13:13
122 41
13:18
94 so after you do this if you save this
13:23
application your map will be rendered out
13:35
so it is saying that Google is not
13:50
defined google.
13:58
maps.
14:11
uh let me copy this I think I made a mistake right here simply copy
14:19
here okay okay I think this we need to
14:25
go we need to paste this code right at the very top whenever
14:32
body is not loaded so just write here the ordering is very important
14:38
simply paste
14:50
it if this is not loading guys what we can do we can also do this like
14:55
this we can attach this call back function right here call back is equal
15:00
to init map and then we can say async
15:07
differ just we can simply add theate call back we can write and then followed by the function name and then we can
15:13
write async AER to actually load this asynchronously and then we can remove
15:18
this on load this simply means the same thing I think I'm making some typo mistake
15:25
that's why don't want to waste your time so what I will do save this
15:46
code I think
15:56
our div ID and let me paste this code here think I'm
16:03
making some kind of mistake right here
16:09
so and just paste your API key so API credentials so please show key
16:24
copy so you can see map is now showing here
16:37
you can zoom in so now the map is showing so what I will do
16:45
uh so we have written this code up till now I will
16:55
simply up till now we have written this code we have declared these variables we have written up this code right here so
17:03
after this what we need to do we need to declare these
17:11
uh Direction service so this is equal to new Google maps. direction
17:19
service so we are actually invoking that distance Matrix API we are invoking this
17:24
Constructor this is actually method here and then Direction renderer which is a
17:30
new Google maps. directions
17:42
render so after that direction renderer do set map we are invoking this to the
17:49
actual map that we created initializing the service and now we will take the
17:56
locations the user has submitted from and the two
18:07
input so whatever locations the User submitted this will be there right
18:13
here and we will simply say we will attach this autocomplete
18:19
API because if you just see right here if
18:26
I if I try to enter some locations it will not work autocomplete is not working right here so we need to attach
18:33
the autocomplete first of all to this so what we will do autocomplete
18:39
from so we will attach this autocomplete API by using Google maps. places.
18:47
autocomplete from input and then simply you'll declare it
18:53
for the two as well which is same code Google maps. places.
19:02
autocomplete and we'll pass this from sorry two
19:08
input and then we will write these two lines autoc complete from and we will bind this bind to bounce and Then
19:16
followed by the map and same code autoc complete two bind
19:23
two so if you just try to reload the application now you will be able to see
19:29
the auto complete
19:43
working Google Maps direction service is not a Constructor uh let me
19:56
see uh Direction service directions sorry this needs to be Capital just make
20:03
sure Direction service and this also directions
20:09
render just change
20:15
this if you type now you will see autoc complete is looking right here all the
20:20
auto complete is right this also work so now as I select some locations
20:28
right here so whatever what we need to do right here we will be binding a event
20:33
handler each of these
20:43
event uh just write here we will basically bind a event handle to these buttons right here so whenever we click
20:50
this uh first button which is calculate route so these buttons will be right
20:56
here you need to attach on on click to the to these two buttons so on click so
21:02
whenever we click the first button we will execute a function called as
21:07
calculate
21:12
route and the second will be if you click this button it will
21:20
basically execute a function calculate
21:26
distance so these two functions we have attached so now we just need to Define these two functions so first we will
21:32
Define this calculate
21:39
route so inside this very first function what we will do we will basically make a
21:45
simple request to the Google distance Matrix API so three
21:52
things we need to pass right here which is the origin which is the from input do value
21:59
second is the destination which is the two input. value just put a comma this object here
22:07
the third property is the travel mode there are various travel modes out there bicycle if you're traveling it using
22:14
walking driving bicycling Transit so I will be selecting the
22:19
driving distance that I want to calculate and then we will simply say Direction
22:25
service. route and here we will pass this request to actually get the
22:32
routes between the destination and the origin and this will have a result and
22:40
the
22:46
status so we are actually using the route method of the distance Matrix API to actually and based upon that status
22:53
if the status is equal to okay in that case
22:59
you will simply say that directions renter do set
23:06
directions result and if the status is not okay in
23:12
that case you will simply alert the user that directions
23:20
fiiled so if you just uh save this and try to enter a
23:26
location I want to travel from ockland to Wellington so I will
23:33
simply click on get directions as you see guys basically it will show to
23:47
you this is Valentine if you
23:54
see it will show you the correct route which whichever route that you want to take if you're traveling it through car
24:01
car driving distance you can see
24:08
that so you can do it for any
24:16
location so if you want to have toq and
24:27
then
24:39
so as you can see the message will come if the directions are not valid so you will see
24:46
that this is Melbourne this is
24:53
Sydney so in this way guys you can do this uh now the second button is
24:59
calculate distance so if I enter basically you need to Define this function which is calculate
25:06
distance the second button this button we'll basically be using the again this same
25:14
API Google maps. distance
25:20
Matrix Service we'll execute the Constructor right here so we are using
25:26
distance Matrix Service API guys so inside this service.
25:33
get distance Matrix and this actually takes an
25:40
object which first is the origin which is the from input dot
25:46
value and then the destinations this will be two input do
25:53
value and again it takes the travel mode so I will simply say I am traveling
25:59
through car so I will set driving and then unit system fourth property it
26:05
takes and this I want to take the distance I calculate the distance
26:10
in Google maps. unit system and I want to have the in metrix
26:17
so it will calculate in kilometer so we'll say avoid highways this property
26:23
is also there I will set it to false I don't want to avoid the highways and
26:29
avoid tolls this is also a uh property so I will set it to again
26:39
false and then it will now execute basically the call back it will contain
26:44
our response in the status Arrow function is this
26:55
and so you can see that guys it takes we are calling get distance Matrix we are
27:01
providing origin destination travel mode unit system avoid highways avoid tools
27:06
these five properties six properties we are providing it to have calculate the distance and now it will say if status
27:13
is not okay in that
27:25
case you can simply say to the user
27:31
that location is uh not correct and in the else block if the
27:39
status is okay in that case we will calculate distance at kilometer so what
27:44
I will say response. row zero elements
27:51
zero distance. text and then the info window
27:58
we will just show the distance in the info window so that the user can clearly see what is the distance so new Google
28:05
maps. info window in this way we can construct info window in the Google Map
28:11
and the content right here what is the content that you want
28:18
to show in the distance window
28:25
so this is a distance it will so
28:34
distance that's all and after we just need to open this info window automatically so what I will say info
28:41
window do set position map. get
28:49
Center info window
28:55
open that's all so now it will calculate the distance it will show the distance in the info window if I just refresh the
29:04
application now if I basically write the location let's suppose
29:09
Texas and then I will need to Los vas click on that so if you see this is
29:18
basically the distance it calculated sorry the
29:26
directions this is a Source location you can zoom in right here check the doutes
29:31
directions it will drive in distance if I click this calculate distance and now
29:36
it will show you the distance which is 1,722 kilom and it is using the distance
29:43
Matrix API to calculate the driving distance between these two locations and just showing it in the info window this
29:49
is the overall project in the browser using vanilla JavaScript guys we are using distance Matrix API places API
29:56
autocomplete API and also so Google embed API so this is a complete project
30:02
if you are interested you can purchase the full source code the link is given in the description please hit that like
30:08
button subscribe the channel and I will be seeing you in the next video