There is a hidden page you can visit at /you-are-awesome. Didn't want you to
miss out on the fun!
Content has not been updated for more than 2 years, proceed with
caution.
Geocoding with Gatsby and SANITY.io
Geocoding is the process of turning a regular address into latitude and longtitude coordinates that can be used in a mapping application like Google Maps or LeafletJS. I am going to walk through setting up a SANITY.io schema, querying the SANITY.io API via gatsby-node.js, passing the query results to the OpenCage geocoder API, and then finally passing those combined results into a brand new GraphQL node that can be queried inside your Gatsby site.
In a future post I will talk about using these coordinates to create a map using LeafletJS.
For the purpose of this tutorial we will assume you are building something like a real estate app - where you would need to map locations based on their address. But a realtor (or a typical content editor) is not going to know the latitude, longitude of a home - we need to handle that process for them behind the scenes.
There is a plugin called gatsby-transformer-opencage-geocoder which didn’t work for me. However, I did take inspiration from the plugin for how to handle this.
Add additional dependencies
The first step we will do is add a few additional dependencies we need to our project.
or
Setup your SANITY.io content structure
Next you will need to setup a SANITY.io content structure. Here is what a really basic content structure for a “home” in the database could look like.
When queried we will get a data structure back from SANITY.io that looks something like this:
Query for content in gatsby-node.js before node creation
Typically when you see queries happening in gatsby-node.js (for example in the createPages lifecycle hook) they happen later in the build process and can access the GraphQL nodes Gatsby has created earlier on. In this case we want to create custom GraphQL nodes ourself so we need to get the data straight from SANITY.io rather than using the source plugin.
We will be using a GraphQL library called graphql-request to handle the API call.
All of these steps happen inside of the sourceNodes API from Gatsby. This is the step in the build process where Gatsby actually creates the GraphQL data layer.
Here is what that query looks like:
Geocode function
Now that we have our data from SANITY.io we need a function to handle resolving the coordinates from a geocode API. In this example I am using the API from OpenCage Geocoder but you could probably adapt this example for other geocode services. We will call this function in the next step when we create the nodes.
Create the GraphQL nodes
Now we need to loop over all the “homes” using the map method to create our new GraphQL nodes. The node creation process needs to be wrapped in a Promise to ensure that it does not try to run without the data from SANITY.io and Opencage APIs. We will be creating GraphQL nodes named Home and Gatsby will also automatically create an allHome node we can query.
Here is the code with comments to explain what is happening on each line:
Catch those errors
Last but not least, we are going to wrap everything in a try…catch statement to give us better error logging. I have also included the complete code in this snippet so you can see all the steps put together.
I hope this helps in your mapping and geocoding journey in the JAMStack and as always I would be happy to answer questions on Twitter or by email.