Much of our lives, and the lives of people we create services for, revolve around the unique context of our places. Our communities and neighbourhoods, perhaps now more than we ever realised before, are more than where we live but also where people work, socialise and learn. A common need we find when working with local authorities is a need to connect residents with services, organisations or events beyond the council and in their local area. We see user stories like:
- I need to find services in my local area
- I need to know what’s going on around me
- I need to see the closest events to me
Increasingly, we expect the apps and platforms we use to be location-aware, highlighting services within a specific geographic area. Be that regionally, city-wide or at a neighbourhood level. Driven by apps like Airbnb, Uber or Google Maps, this raised expectation is a dataset we can build into council platforms, supporting residents with the kind of connectivity they expect.
In this post, we’ll look at a few ways to add location-aware search, comparing two main approaches: one with Ruby on Rails with PostgreSQL, and one with Node.js and MongoDB.
First, let’s consider the kind of data we’ll need.
Preparing our data
Let’s say that our users are parents, and we want to show them nearby childminders. We might have data like this:
- childminder name
- contact phone number
- street address
- town
- postcode
The first step is geocoding the data: turning those address fields into coordinates we can use.
Google has a geocoding API we’ll use. Because it has the power of Google Maps behind it, it’s fairly resilient to slight misspellings and misformatting that will probably be common to any real-world dataset.
There are plenty of alternatives, but they all have idiosyncrasies. It’s worth playing around and seeing which one gives the most valuable results for your data.
Some of these APIs can get expensive if we have thousands of data points to geocode, but it’s possible to use Google Sheets to do the hard work for us.
Once we have a fully geocoded dataset, it’s time to think about querying it.
The Rails/Postgres approach
Let’s look at how to work with our data in a fresh Rails app.
We’ll start by making a new model called Service, which we’ll add extra columns onto to represent our coordinates:
- rails g model Service name:string phone:string address:string town:string postcode:string latitude:float longitude:float
There’s a great Rails gem called geocoder which is easy to set up and adds all sorts of useful geospatial methods to our models.
Getting the straight-line distance to each result is normally enough to meet the user’s need, and it substantially simplifies the computations we need to make.
Share this