Creating a Location-Based Search using Geocoding

Felix Seip
Geek Culture
Published in
4 min readMar 6, 2021

--

https://miro.medium.com/max/1181/1*AS1LAUPL5gfRCZb1M2T1gg.jpeg

I love boats. If you have looked at my Medium stories, you’ll see one of the first stories that I posted was about me buying a yacht. And let me reiterate that I am not rich. It’s an investment into my future instead of buying a house.

Anyway, I started a project that was supposed to something like AirBnB just for boat slips. The idea was to let people put their slips up on the site and be able to rent them out while they’re away and not using them.

Turns out, that marinas don’t allow this and explicitly let it be known that it’s not allowed in their contracts. Due to this restriction, I decided to give up the project before spending any more money on it for marketing.

However, I did learn a lot about location-based search and wanted to share my experience in how to go about implementing something like this.

What is location-based search?

You probably already know what it is. You use it every day when you look for a new restaurant in your area to try out or when you’re looking for the nearest gas station when you’re out and about on the road.

Location-based search is, simply put, searching for something based on yours and its relative physical locations. Location-based searches help us to see that anything can be within our reach, no matter our locations.

How I went about implementing a location-based search?

Gathering Data

The first thing you need is some data. Whether it be fake locations that you create with some random GPS coordinates or real data that was scraped from websites, you definitely need some kind of data that contains GPS coordinates.

For me, this data was a list slip that users would have posted on the platform to rent out. When the user posts a slip, they are required to enter GPS coordinates of where exactly the slip is so that it can be shown on a satellite image using Leafletjs. With the GPS coordinates, I was also able to implement the search functionality.

Turning a Search String to GPS Coordinates

Now that I had the list of slips with their coordinates, I needed to figure out how to get the GPS coordinates of the location that the user had entered.

Luckily, there are services that offer geocoding or turning a location into GPS coordinates.

At first, I took a look at the Google Geocoding API. However, I decided not to go with it at first, since I would have had to pay a little bit for each request. And since I was just testing at first and wasn’t expecting much load right after the platform would have gone live, it was alright for me to use something a little more limiting.

That’s when I stumbled upon the OpenCage Geocoder API.

OpenCage is a geocoding API that aggregates data from multiple sources and rates each of the results using a confidence score. The higher the confidence, the more likely it is that that result is the one you are looking for. Even more awesome is that OpenCage offers a free trial that allows you to send 2,500 requests/day at 1 request/sec.

That was good enough for launching the platform. If I had needed more, I would have switched to some other service, but 2500 requests were enough to start with.

The OpenCage community also offers SDKs and tutorials for 30+ languages and frameworks which was pretty awesome.

Since I wrote my backend in Java, I used the JOpenCage SDK to handle the requests. It was honestly a very simple and straightforward implementation.

Here’s a sneak peak into the usage of JOpenCage:

All I had to do was create an instance of the JOpenCageGeocoder, create a forwarding request with the desired location to geocode and then get the response. The method “getFirstPosition” returns the result with the highest confidence, which was exactly what I wanted.

Doing Some Math

Now that I had a list of locations with their GPS coordinates and the GPS coordinates of the searched location, I had to figure out which slips were in a certain radius of the searched location.

A simple google search gave me a StackOverflow post with exactly what I was looking for:

I will admit that I don’t understand the math behind it anymore, but the code snippet surely did work. To ensure that it worked, I wrote Unit Tests with real-world locations and they all passed.

Finalizing Everything

All I had to do was go through each slip and check whether the geocoded search location was within a given radius and that was it.

At first, implementing something like this seemed like a daunting task that only experts were able to implement, but it turns out that it was much easier than expected.

So, if you ever have the need of implementing a location-based search, my advice would be to just go for it. It’s not difficult and it feels very rewarding to have done something like this even if it’s not production code.

--

--

Felix Seip
Geek Culture

Freelance Full Stack Software Engineer based in Frankfurt Germany. Expanding my horizon in any way I can. https://felixseip.de