How to fetch nearest node based on geofield?
Something like this should do the trick to implement haversine :

/**
 * Implements hook_views_pre_execute().
 */
function YOURMODULE_views_pre_execute(\Drupal\views\ViewExecutable $view) {
//...
    $view-&gt;build_info['query']-&gt;join('YOUR_FIELD_LATLNG', '...', '...');
    $view-&gt;build_info['query']-&gt;addExpression(sprintf('3956 * 2 * 
                                            ASIN(
                                                SQRT( 
                                                    POWER(
                                                        SIN((%s - TABLE.YOUR_FIELD_LATLNG_LAT)*pi()/180/2)
                                                    ,2)
                                                    +COS(
                                                        %s*pi()/180 
                                                    )
                                                    *COS(
                                                        TABLE.YOUR_FIELD_LATLNG_LAT*pi()/180
                                                    )
                                                    *POWER(
                                                        SIN((%s-TABLE.YOUR_FIELD_LATLNG_LNG)*pi()/180/2)
                                                    ,2)
                                                )
                                            ) * 1.60934', $lat, $lat, $lon), 'kilometers');
}




edit nearest :

$view-&gt;build_info['query']-&gt;orderBy('kilometers', 'ASC');
$view-&gt;build_info['query']-&gt;range(0, 1);




Note there is a contribution with views on geofield : https://www.drupal.org/project/geofield/issues/3012750