When I built the Nearby feature in my side project
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?
If I calculated the distance from the user's location to every single place in every post that would be:
Even worse, each post contains multiple locations.
Should I compare all 7? Just the first one? The average?
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.
Here's how I did it:
center_lat
and center_lng
in the post table.This small change made a big impact:
It's not GPS precision but it's UX-accurate
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:
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!