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:

  1. Under Advanced > Contextual Filters, add the following filters:
    1. Proximity Entity Argument for the relevant geolocation field
    2. ID (Content)
      1. Important: Under the "More" tab, check the 'Exclude' option to exclude this entity.
  2. For both contextual filters:
    1. select the "Provide default value" radio button
    2. for Type, select "Content ID from URL".
  3. Under FIELDS, add a new proximity field for the geolocation field of the center entity.
    1. Under Centre options, select the Entity argument field that you added above.
    2. You will probably want to check the Round option, and set the number of decimal places to show
  4. Under SORT CRITERIA, search for "Proximity" and add a proximity sort.
  5. 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: