Back to Blog

Smarter Nearby Filtering: Using Midpoints for Multi-Location Posts

Royan Gagas
July 28, 2025
Share
nodejs
product
nextjs
Smarter Nearby Filtering: Using Midpoints for Multi-Location Posts

When I built the Nearby feature in my side project

Rekkoku
, I ran into a small but critical design challenge.

Each post in Rekkoku can contain up to 7 places from Google Maps.

So the goal was simple:

"From the user's current location, show posts that are geographically closest."

Seems easy until you realize:

Which location should I compare the user's position to?



The Problem

If I calculated the distance from the user's location to every single place in every post that would be:

Too heavy on performance
Difficult to scale
Messy to query

Even worse, each post contains multiple locations.

Should I compare all 7? Just the first one? The average?



The Insight: Use a Midpoint

I needed one clean coordinate per post, a single point to represent all 7 locations in that post.

So I took a step back and thought:

"What if I just calculate the midpoint?"

Like in high school geometry find the average of all the latitudes and longitudes.

That becomes the center point of the post.



Implementation Flow

Here's how I did it:

1.When a post is created, I extract the latitude and longitude from each Google Maps link.
2.I calculate the average of all lat/lng coordinates -> that becomes the midpoint.
3.I store that as center_lat and center_lng in the post table.
4.The Nearby feature uses this coordinate to calculate distance from the user's current location using the Haversine formula.


Why It Works

This small change made a big impact:

Nearby queries became lightweight
I only needed to compare one point per post
It's scalable even if I have thousands of posts
It still feels accurate enough for the user experience

It's not GPS precision but it's UX-accurate



Final Thoughts

Even in a side project, decisions like these matter.

Sometimes, solving a user-friendly feature requires thinking deeper about data structure and query design.

If you're building a "Nearby" feature and dealing with multi-location records, ask yourself:

Should I use a midpoint?
Or a bounding box?
Maybe even geohashing?

Every option has trade-offs. In my case, midpoint + Haversine worked great.



Built with:

Next.js, Express.js, and curiosity.



Let me know how you'd approach this, I'd love to hear other tricks!