Search code examples
drupaldrupal-7coordinatesdrupal-viewsproximity

Drupal 7 - Views / Geofield - display nodes nearby current node lat / lng


I thought this would be a pretty simple thing to do given I have the lat / lng stored in a Geofield. However, I'm really struggling.

Basically I just want to show the 20 nearest nodes to the current node in a block.

No address fields or anything complicated, just straightforward coordinates...

Any ideas before I start hacking?!

BTW, I'm a relative Drupal newbie, but lots of PHP experience.

Thanks, Chris


Solution

  • My solution:

    • In the contextual filter field add a Geofield proximity filter - something like "Content: Location (field_location) - proximity". Set the radius distance at the bottom.
    • Under "When the filter value is NOT available" select "PHP Code"
    • Enter the following PHP. The return value is the argument that is passed to the filter.
    
        $node = menu_get_object();
        $value = field_get_items('node', $node, 'field_location');
        $lat = $value[0]['lat'];
        $lon= $value[0]['lon'];
        return ($lat . ', ' . $lon);
    
    

    Figured this out from this page https://drupal.org/node/2014345