Last week, I wrote about creating a cloudflare worker. When I created that worker, I knew that I wanted to leverage that for the state parks app. This week, I created three new workers: one to get the user’s location, one to get the list of parks, and one to get an individual park. Let’s take a look.
Geolocation API
The app is going to need to know where the user physically is so that it can figure out how far away the park is. This script handles that. If the app passes in the user’s latitude and longitude, it makes a mapquest api call to determine the city, state, and zip code. If the latitude and longitude aren’t provided, the script tries to determine it based upon the user’s IP address.
You might notice the precisionConfidence
variable. That value is meant to provide an indicator for how accurate the IP-based geolocation is likely to be.
Park Listing API
This API should return an array of parks details. Like the geolocation API, it can take the user’s physical latitude and longitude but if it isn’t provided, it uses coordinates that are based upon the user’s IP address. It can also take a propertyType
value, so that what is returned can be limited to state parks, recreation areas, or state forests.
You will notice that it uses the JSON document that we created back in September but it then does some additional processing that was in the client app.
Park Details API
Right now, this one takes a slug value and returns just the park details for one park but I eventually want to expand it out to also return photos, detailed driving directions, etc.
As long as you provide a valid slug value, it should return a park.
So, what do you think? Do you have any questions, comments, concerns? Feel free to drop a comment, below.