On this page
How to create a nearest node view / Sorting by proximity
Last updated on
5 April 2021
This documentation needs work. See "Help improve this page" in the sidebar.
Drupal 8
There are at least a couple of solutions out there.
If the field that holds your location (latitude, longitude) is a Geofield, then see example 2 in this article.
Alternatively, if you have the Geolocation Field module installed and you want to create a view of entities, sorted by their proximity to a center entity, e.g. as a block on a node page showing the closest nodes:
- Under Advanced > Contextual Filters, add the following filters:
- Proximity Entity Argument for the relevant geolocation field
- ID (Content)
- Important: Under the "More" tab, check the 'Exclude' option to exclude this entity.
- For both contextual filters:
- select the "Provide default value" radio button
- for Type, select "Content ID from URL".
- Under FIELDS, add a new proximity field for the geolocation field of the center entity.
- Under Centre options, select the Entity argument field that you added above.
- You will probably want to check the Round option, and set the number of decimal places to show
- Under SORT CRITERIA, search for "Proximity" and add a proximity sort.
- Optionally: Add a filter/two filters to check for the lat/lng values not being empty, as those would result in proximity 0 and listed first.
For more examples please check Views from Integrated Demo module packaged with Geolocation Field
Help improve this page
Page status: Needs work
You can:
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion