Data Management

Geo location: How to sort records based on the distance from a point

Summary

Welcome to this step-by-step guide on how to sort records by distance using geolocation in Xano! Xano's robust support for geofunctionality allows you to query and filter your data based on geographical locations, making it incredibly useful for various use cases.

In this article, we'll walk through a practical example of how to sort records in your database based on their distance from a specific point. Follow along, and you'll be able to leverage this powerful feature in no time!

Step 1: Set Up a Geography Field in Your Database

Before we dive into the geolocation sorting, let's ensure you have a geography field set up in your database. Here's how:

  1. Navigate to your database in Xano.
  2. Click the "+" button to add a new field.
  3. Scroll down and select the "Geography" field type.
  4. Choose the appropriate geography type for your use case (e.g., Point, Point Collection, Path, Polygon).
  5. Optionally, you can click on the map interface to pinpoint a specific location or enter an address.

For this example, we'll assume you already have a "location" field of type "Point" set up for your "Merchants" table.

Step 2: Create an API Endpoint

Next, we'll create an API endpoint that will fetch and sort our records based on distance:

  1. Head over to the API section in Xano.
  2. Create a new API group or use an existing one.
  3. Add a new API endpoint (e.g., "Sort Merchants by Distance").
  4. In the endpoint's configuration, add an input field to accept the target geolocation (latitude and longitude).

Step 3: Query Your Records

Now, let's query all the records from your "Merchants" table:

  1. In your API endpoint, locate the "Query" section.
  2. Click "Add Query" and select your "Merchants" table.
  3. Choose the appropriate operation (e.g., "Query All Records").

Step 4: Add an Eval Field for Distance Calculation

To sort our records by distance, we need to add an "eval" field that calculates the distance between each record's location and the target geolocation provided in the input:

  1. In the "Output" section of your query, scroll down to the "Evals" area.
  2. Click "Add Eval" and select your "Merchants.location" field from the dropdown.
  3. Rename this eval field to "distance".
  4. In the "Filter" input field, search for and select the "distance" filter.
  5. Set the first parameter of the "distance" filter to your input field (the target geolocation).
  6. Click "Update" to apply the changes.

You should now see the "distance" eval field added to your output.

Step 5: Sort Records by Distance

Finally, let's sort the records based on the calculated distance:

  1. In the "Output" section, locate the "Sorts" area.
  2. Click "Add Sort" and select the "distance" eval field from the dropdown.
  3. Choose the appropriate order (e.g., "Ascending" for closest records first).
  4. Click "Save" to apply the sort.

That's it! You've now configured your API endpoint to fetch and sort records from your "Merchants" table based on their distance from the provided geolocation.

Test Your API

To test your API, simply provide a valid latitude and longitude in the input field and execute the endpoint. The response should contain your "Merchants" records sorted by distance, with the closest record having a "distance" of 0 (or a very small value, depending on the precision of your input geolocation).

Congratulations! You've successfully learned how to leverage Xano's powerful geofunctionality to sort records by distance. This feature can be incredibly useful for various applications, such as finding nearby businesses, sorting real estate listings by proximity, or optimizing delivery routes.

Remember, this is just one example of what you can achieve with Xano's geolocation capabilities. Feel free to explore further and unleash the full potential of this powerful no-code platform!

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team's progress and create a backend that will scale for free.

Start building for free