Showing distance and sorting by distance in radius search results
In a View with a radius search, the Distance field shows how far each entry is from the searched location. A View setting lists the nearest entries first.
Before you start #
- Maps is built into GravityView Pro. To turn it on or off, go to Plugins and click Features in the GravityView Pro row.
- On a GravityView + Extensions (Legacy) license, Maps is a separate plugin, and the Distance field and distance sorting need version 3.9.0 or later of it. About the “GravityView + Extensions (Legacy)” license shows how to install it.
- Your View needs a radius search. How to enable radius search sets one up.
How distance is measured #
Distance is measured in a straight line between the searched location and the entry’s position on the map, as the crow flies. A route by road is usually longer.
The unit follows the search:
- A search in miles shows distances in miles, such as
2.8 mi. - A search in kilometers shows them in kilometers, such as
4.5 km.
Step 1: Show the distance for each entry #
- Edit your View. In the Multiple Entries Layout tab, click Add Field in the area where the distance should show. In a Table View, the button is Add Table Column.
- Choose Distance.

- Click the gear icon on the new field to open its settings. Set Decimal places to the number of digits after the decimal point, from 0 to 5. The default is 1.

- Click Update.
During a radius search, each entry now shows its distance, such as 0.4 mi. Without a radius search, the field stays empty.
Note: Only the Multiple Entries Layout offers the Distance field. The Single Entry Layout doesn’t list it.
Step 2: Sort results by distance #
- Edit your View. In the Settings box, click the Maps tab.
- Under Radius Search Settings, turn on Sort radius search results by distance in the Radius Distance Sorting row.

- Click Update.
The order now depends on the search:
- During a radius search, the nearest entries come first, and the order continues across all pages of results.
- Without a radius search, the View keeps its own sort order.
Entries at exactly the same distance follow the View’s own sort order. Distances are compared before rounding, so entries that show the same rounded value still appear nearest first.

The Distance field or the sorting setting is missing #
- Look for the Distance field in the Multiple Entries Layout tab. The Single Entry Layout doesn’t offer it.
- Look for the sorting setting in the Maps tab of the Settings box, under Radius Search Settings.
Changing how the distance looks #
The gk/gravitymaps/fields/distance/output filter changes the text the Distance field shows. This example adds the word “away” after every Distance field value on your site:
/**
* Adds the word "away" after the Distance field value.
*
* @param string $output The distance and its unit, for example "2.4 mi".
* @param float $distance The distance in the unit of the search.
* @param string $unit The unit of the search: "mi" or "km".
*
* @return string The text the Distance field shows.
*/
function gk_distance_away( $output, $distance, $unit ) {
return $output . ' away';
}
add_filter( 'gk/gravitymaps/fields/distance/output', 'gk_distance_away', 10, 3 );Note: Adding custom PHP code snippets to your website shows where to add this code.
The filter also passes a fourth parameter. Its developer documentation page describes what it holds.