Python Google Maps API Directions | How To Get Started and How To Implement Google APIs 2017

Python Google Maps API Directions | How To Get Started and How To Implement Google APIs 2017

Hey guys it’s Samuel here from HowToFAQ and today I want to show you how you can get started with api’s and in
particular how you can get started with some of Google’s APIs and I’m
going to be showing you some things like the Google Maps API so let’s get started
so the easiest way to get to the Google Maps API is just type Google Maps API
and it’s the first result Google Maps API Google developers who click on that
and you can see the get started and you can can scroll down you can see that
lift actually uses this you know some companies use it so you can see the real
world implications of this so click get started and we want to use the
directions API and this is just going to tell us the exact directions between two
given points that we can define either as actual longitude latitude points or
just in kind of words you know names of towns names of airports etc so you can
see some sample code actually gives a JSON response and that’s a top in Python
terms is a dictionary so how we’re going to get started so first of all I’m just
gonna open new window of Python and I’m gonna have this here and we can start
coding some stuff and I get the I’ll up as well and yeah so first we’re just
gonna click it key and just click I’m just going to call this directions API
yeah that’ll do and it will load and you’ll see it loading and it will create
the project and then it should give us an API key what we can do with that API
key is we can actually so now we have it okay we can actually access the Google
Maps directions API get directions between two places okay so now we have
the API key and we have a rough idea what sort of output to get sort of along
this of time so we can use this base URL in all of the work stuff out
so if we open up – and I’m just going to start off by saying import URL Lib dot
request because we’re going to need that for sure because we’re doing an API
request and this is probably the easiest way of doing it and I’m going to
actually make an end point for our API which it an endpoint is essentially
where you send the request to so I’m just gonna use their endpoint here so
they’ve basically used this base URL you give the formatting in JSON there we go
JSON so that means we’re going to get a response in JSON rather than say XML
okay so we have the base endpoint here and I’m actually going to get the API
key as well just get that back if we click get key click back on this project
and we can just quickly copy it across into our Python file and then we can
actually start and we’ll just copy it across into here
so that’s our API key yeah and we have that endpoint and we’re gonna now think
about how the API requests are set up so in the API requests they basically have
an origin and a destination so in this example I’m going to actually ask the
user for the origin just to make it a nice little project where are you now
we’re not going to use longitude latitude because I think it probably
will work better if we just make it so it uses the basic search that we use in
Google Maps that you were using it on your phone for example where do you want
to go question mark : okay and now we can build up on
navigation requests so navigation on the school request and we could just
simplify this down to now request I guess and this is going to involve the
origin so to define the origin and we need to define the destination and you
can see it kind of builds up in like a in a standard way that a URL would and
we if you’re wondering how I’m working this out if you just click done for a
second if you look down here the example request is very helpful we can see we
need to add the key at the end and we have origin and destination so that’s
the kind of format I’m following any spaces we replace with a plus yeah okay
and if we want to put postcode then that’s you can put like a comma rustling
yeah okay so we have the destination and you can see from request here we’re also
going to need to add the API key so I’m going to put a little ampersand sign key
equals and you can see I’m adding these curly brackets and what that’s going to
do is it’s going to allow me to use the string format in Python and input
then input the strings I want in these places it’s kind of a nice way of doing
it so I’m gonna put origin comma destination comma and then API key there
we go so now we have our request built up and we now need to start actually
building the request so we can send it so we’ve got the built request we just
need to send it so also gonna get JSON because that will be useful when
receiving the actual request into a dick string okay so I’m gonna do request request equals now it will equal the end
point plus the nav request and we’re gonna have to use I’m going to do
response day we’re gonna use the URL Lib dot request to do this I’m gonna set
variable response to be the response of the API request we make so request URL
Lib request actually request and I’ll leave this along on my github page if
you have any questions do put them down in the comments sure we got dot URL open
and now we just put request so that will send that request opening the URL and
we’re going to read it and now have the response as a string so if we want our
to get the directions with do directions equals JSON that loads the response and
I suppose we can print print that and you’ll see that it’ll be pretty
extensive because they do give you a lot of information okay so now I think we’re
pretty much done the API keys ready the last thing I’d say that we might be a
good idea to do is on each of these that just maybe put
replace the spaces with plus because it’s obviously a relic can’t have any
spaces has to have a plus in this sort of request and now we’re just going to
run it click f5 and you can see it says where are you and I’m just gonna put
London Heathrow and so I’ve said I want to navigate from London to Heathrow it’s
gonna make an estimate of where I mean and it’s gonna guess Heathrow Airport
most likely and it’s going to print the different types of ways I could go and
you can see there’s a quite a number of different ways that I could go in this
case and you can see all the directions are here and what we’re going to do is
we’re going to just decipher this so we can find out how long it will take to
walk so you can see the main structure involves first of all several different
routes so we can set the routes if you scroll down you’ll see there’s a key
called roots so if we go the nice way of navigating this will be code directions
keys you can see that there’s three main keys of this dictionary geocoded
waypoints routes and status status is basically saying whether the routes find
to take or whether it’s impossible basically so we’re going to want to go
for routes are their variable routes equals directions routes and we’ll know
the routes dot keys oops accidentally forgot that part the
rootstock keys because it’s a list of all the different routes we’re gonna
want to just go to the first one and then see what we options we have now
we’re in the first one you can see we have the option to go four legs and the
legs refers to the walking distance in this state and there’s obviously a
summary which would obviously tell you how roughly how far or what sort of
directions you need to take etc in terms of walking okay so if we
just go oops routes and we’re just going to go legs
and just like a little key in the dictionary now when we print this out
you’ll see there’s multiple different routes you can go by walking so we’re
actually gonna I’m just gonna take this set this equal to legs and what we’re
gonna do is we’re gonna so I’m gonna call this legs and we’re just gonna test
what the length of legs is to see if there’s a more than one group which is
let is probably likely there’s only one route but we’ll see yeah there’s only
one route so we can go legs zero first index and we want to go find the
distance now we want it in text format so we’ll give you our the value in
meters or the actual text format which is a rounded value so let me give you
this so you can see right there that is the distance you take so if you want to
use this for some sort of API then you’re going to have to look through
this code understand how it’s working and how it’s how it’s been developed
with these kind of these keys in this JSON format and you’ll work out where
the parts you need to access are so it’s pretty straightforward and there’s a lot
of good documentation on Google’s website and a lot of people on Stack
Overflow as well have talked about this so this is just a really easy
implementation of the Google Maps API these eleven lines of code here could be
used to implement most of Google’s API s for example another API they do is the
street view API you’ll show pictures around the country and you can download
them so that’s pretty cool and you know there’s other Google Maps API eyes as
well if you’re feeling a bit ambitious and you want to try out some other API
is just go API Google and it will show you you know where the api’s are and you
can see API allowed the library is a good one to look at and you can see
all the different API is they serve and how you can use it and obviously this is
brilliant for all types of Android apps like I mentioned earlier lyft actually
use this so guys I hope this has been a helpful learning experience for you and
yeah if you want to see any cool projects with this do you have any
queries or problems with this code likewise it’ll be on my github page and
yeah see you guys in the next video make sure you get subscribed and so you
don’t miss future content and I’ll see you in the next video guys goodbye

25 thoughts on “Python Google Maps API Directions | How To Get Started and How To Implement Google APIs 2017”

  1. I'm getting following error:
    {'error_message': 'The provided API key is invalid.', 'routes': [], 'status': 'REQUEST_DENIED'}

  2. can we plot the directions on the google map? how to find the optimised path from those directions? thanks in advnce

  3. I need your help,
    I used Python to get directions, and I got some direction data. Now the question is, how to put this data on google maps so I can see directions?

  4. Hello, kindly advise: what recording software did you use? and how do you zoom-in on the code window?​

  5. how to read latitude and longitude from a json file and displaying that location on HTML file using google maps?? can u make a video by taking sample json file of lat and log of 5 cities and display the map location on HTML file??

  6. Getting the output:
    {'destination_addresses': [], 'origin_addresses': [], 'rows': [], 'status': 'INVALID_REQUEST'}
    but I am using my API key

  7. Does google earth also have its own API? Im planning to use python codes to access or manipulate or use google earth pro's (desktop applications) features. Is there a way to do that?

  8. I want to use the python code in azure ML studio and use Distance matrix api. how can i find the distance between 2 addresses in the dataset and put it in a new column in the row with the 2 addresses?

Leave a Reply

Your email address will not be published. Required fields are marked *