Just in time for Valentine’s Day
, if you happen to be planning a trip across the United States and want to offer your companion a rose at every degree of latitude traveled, then this walk-through is for you!
The title says it all, we’re going to use the Yelp v3 API
to cross the United States from San Francisco, CA to New York City, NY, and be 60 miles from a florist at all times. This is an updated take on the previous walk-though I did 3 years ago (see original here
). This one uses the new Yelp v3 API and is written in Python. The second part will extend this into an interactive web-app on Google Cloud.
The review site, Yelp
, should be known to most. It is listed as 'User Reviews and Recommendations of Best Restaurants, Shopping, Nightlife, Food, Entertainment, Things to Do, Services and More'. The latest Yelp v3 API is easier to use than the previous versions. Now you only need to pass your free API key, no more complicated auth stuff. Follow the two steps below and you're good to go:
Sign Up for Yelp: https://www.yelp.com/signup
Get an API Key: https://www.yelp.com/developers/v3/manage_app/
Sign Up for Yelp
Straightforward signup page
requires a valid email address:
Get an API Key
Click on the Yelp developer's link
and go to Manage App
. You only need to add an App Name, Industry and contact email in order to generate a valid key. Note at the bottom where it indicates the daily API limit count.
If you want more details on Yelp's v3 API, check out Get started with Yelp's Fusion API
Let's Build This
We are going to create 3 functions and use a big loop to get from one end of the USA to the other, step-by-step:
This function takes the current position of our traveler and calculates its bounded box. The default bounded box unit is 1 degree (equates loosely around 60 miles), so we add 30 miles in all four directions from the current point to created the bounded square (making it a 1x1 degree box, or 60x60 miles box). It returns the next 2 possible moves, both forward and one higher, the other, lower (see image below).
This takes a vector of two sets of geo-spatial coordinates, the upper left point and the lower right one, along with the search term and sends it to Yelp. It returns a data frame with the name, city, rating, state, latitude and longitude of the top location using the search term. It automatically picks the best one if available found in the new geo zone.
MakeAMove is the central relay that communicates with the other functions. It will get the next coordinates via GetPossibleCoordinates, figure out if it should try up or down first depending on where it is in relation to the end point’s latitude, and call on the Yelp API. If it finds a florist, it will report back the pertinent information such as the florist’s name, state, and coordinates. If it can’t find one, it will report back a None.