Distance-Based Results

What is a distance-based result?

A distance-based result is a data point calculated based on a user's current address, zip code, or latitude and longitude. The result can be the closest or farthest point from the user input.

Use Cases

Distance-based sorting is a handy feature as it allows users to quickly and visually see items closest to them. Visualizing proximity enables users to make more informed decisions when selecting service providers, restaurants, or delivery services.

Visualizing distance-based results (UI/UX)

Typically, distance-based results are presented as a list or as pointers on a map. Modern UIs display a list of distance-based elements within a container and pointers on an interactive map. In terms of the experience, users can sort the results in ascending (closest) or descending (farthest) order, which updates both the order of elements within the results container and the labels over the pointers (according to element list number) on the map.

UI/UX

Calculating Distance

The simplest method of calculating distance uses the Haversine formula, which relies on spherical trigonometry to determine the great-circle distance, more commonly known as the straight-line approximation between two points.

Haversine Formula

The Haversine formula is computationally efficient, but it comes with an inherent degree of error because the Earth is not a perfect sphere (it has hills and valleys). The Vincenty formula, which uses an ellipsoidal model of the Earth, utilizes an iterative method for calculating distance. While it results in a better approximation of distance, it is more computationally intensive as it carries a log-linear spacetime complexity.

Implementations

💲
Please subscribe to see the remaining sections of this post.

Below I cover popular implementations of distance calculations at various points of a tech stack (code and demos included), such as JavaScript on the front end, Python on the back end, Postgres-SQL on the database end, and Geo-functionalities in popular CloudSearch providers.

You can get access to all my premium content, for $1 a month or $9 a year.

You don't have full access to this post on Amitk.io at the moment.

Subscribe now
You've successfully subscribed to Amitk.io
Great! Next, complete checkout for full access to Amitk.io
Welcome back! You've successfully signed in.
Unable to sign you in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.