Search Nearby Businesses With Google Maps API and Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going guys in this video i'm going to show you how to search for nearby business using google maps api in python before we dive into the tutorial make sure that i have a google cloud platform account creator and you also need to create a google cloud project in this video i'm using my google cloud demo project now i want to dive into the documentation first just in case if you want to reference the parameters or the document information then you will know how to navigate to the information right so the api we'll be using is called nearby search classes i'll post the link in the description below and this api is found under the google play search api i want to scroll down to the api itself right so let's take a look at the required parameters so i'll be using google maps api library and the nearby method requires location and radius for the radius which is the distance we need to provide the distance in meters and for the location we need to provide the latitude and longer two coordinates now let's look at the optional parameters so for the optional parameters keyword is probably the one that i going to use the most we can also specify the language the minimum and maximum price so i've never used this one before but i'm going to assume that you can specify the price range i sense the dollar sign if you already know the business name then you can use the name parameter to search for the business open now indicating that whatever knife the business is opening right now and we have the rank by parameter and the type parameter so for each business google maps engine actually uh categorized each business based on the business operation so for each api request the request will return up to 20 results if there are additional batches or results that we need to retrieve then we need to provide the next page token and that's basically everything i want to cover in terms of the documentation now let's go to the uh google cloud platform console so first thing first i need to enable the api so what you want to do is you want to go to navigation menu apis and services oops let me go back i want to click on library in the search field want to search for places and want to enable the places api so just make sure that places api is enabled for your project you also need to download your api key if you don't know where to download your api key you want to go to apis and services credentials on the top you can click on create credentials and click on api key and that generates a new set of api key and i already created my so i can just click on this clipboard icon to copy my api key similar to a notepad to a json file so that's all the population works that we need to do in terms of in google cloud platform console now open a blank python script so here i'm going to import my google maps api library first so the library name is called google maps if you don't have the library installed you can install the library using the command pip install google maps i also want to export the results to excel file and to do that i'm going to use the pandas library and this one is going to be pip install pandas so let me show you the results set all right so this is what the excel file looks like once we export the information so if we look at the excel spreadsheet we actually have a lot more information than the usual google maps engine or google maps website so using the google maps api we'll be able to extract a lot more information to perform any types of analysis or to search for business based on different criteria so here we have the business name the business icon and geometry is basically the business location coordinate oh and he'll have business status whether if the business is operational or close temporary or permanent and here's the business name uh these are the hyperlinks to access the photos and many other fields which i'll let you to navigate on your own right so here let's go back to our python script so here i want to create a function to convert a miles to meters i'm going to name this function miles to meter and for the parameter name i'm going to name that mile actually let's do mouse and the function is really straightforward so to convert miles to meters actually this should be pure we can simply times the miles to one point actually one thousand six 609 da 344 so this is the formula to convert miles to meter if we run into any air i want to return 0 and that's it next i want to import my api key i'll just store my api key in a text file so i'm going to open the text file diary and once i have the api key i can create my google maps instance so name the instance map client it goes to google maps dot client i need to provide my api key all right so if i run this code block if i print the members of map client so here are some of the apis that we can use and here's the places api now i want to create my location variable and because i need to provide the location in corners so here i'm going to click anywhere on my google maps and right click and here's the corner so i'm going to copy the corner and i'll send the corner to my location variable and we need to pass this as a tuple the next variable i want to create is the search string and it's basically the the keyword i want to use to search for a business unless i want to search for all the ramen shops in san francisco or in the bay area i want to set my distance to 15 miles so from miles 2 meters function i'm going to enter 15 and here i'm going to create empty list i'll name this list business list now i'm going to insert the map client object da places nearby so this is the api name inside the places nearby method we need to provide the location and the keyword is going to be for the keyword parameter and it's coming from the search string variable if you only know the business name then you can use the name parameter instead of the keyword parameter so let's say i want to search for a business called ramen shop and this is going to be radius which is my distance and i'll name the outpost response and here i'm going to append the records to my business list object so i'm going to use the extend method inform response that gets here let me run this code block first location is not fine oh here we typo should be location let me try again now if i print the response to option and let me print the keys first so here we have three keys html attribution next page token stylus means that what i'm like if the api code is successful and the results key is going to return the business items and this will be response since i see the next page token key is available then i know the additional information that i need to retrieve so here i'm going to create my next page token variable so for response that gets i'll pass the next page token key and i'll create my next page token variable here oh and this one too all right so here i'm going to insert while loop i'm going to say that while next page token is not known here i forgot to input one more module i'll input a time module i notice that every time one make the next api class too fast for some reason i'm going to get duplicate records so here i'm going to delay the next batch execution by 2 seconds then i'll actually i'll grab this code block and here i'm going to provide the next page token to the page token parameter and i'll copy these two lines our paste is over once i retrieve all the business data i can create my data from object so from pd dot data frame i'll pass the business list object i also want to create a new column i'll name the comment url and hear me paste the let me paste the code over so basically i want to create hyperlink to allows me to automatically click on the link to open the business page so here's the website of the base url followed by the place id so if we go back to the excel spreadsheet one of the column is the place id so this is basically the unique id assigned to each business right now let's finish the the script so here i want to export uh the result set to an excel spreadsheet and i'll name the excel spreadsheet business actually let's do ramen shop blue excel sx i'm going to ignore the index and that's it where i'm going to terminate the session i'm going to press f5 to run script it looks like the file is created so here let me go back to my project folder and here's the ramen shop learns excel file now if i open the file and here's the business url so let's say if i want to look at one of the highest rating ramen shop so here we have these two ramen bars so i can simply click on the url and that'll open the page on google maps it takes me directly to the uh business page all right so this service i want to share in this video and hopefully you guys found this video useful and as always see you guys watching i'll see you guys on the next video
Info
Channel: Jie Jenn
Views: 26,372
Rating: undefined out of 5
Keywords:
Id: YwIu2Rd0VKM
Channel Id: undefined
Length: 13min 38sec (818 seconds)
Published: Wed May 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.