Calculate distance using Geolocation module
I've found a solution.

Getting the user location

I'm using the Browser API to get the user location in the frontend and then pass those parameters as query strings to my view, like ?latitude=xxxx&amp;longitude=xxxx.

if (navigator.geolocation) {
    var options = {
        enableHighAccuracy: true,
        maximumAge: 0
    };

    function success(pos) {
        var crd = pos.coords;
        // Send ajax request with crd.latitude and crd.longitude in query strings
    };

    function error(err) {
        console.warn(`ERROR(${err.code}): ${err.message}`);
    };

    navigator.geolocation.getCurrentPosition(success, error, options);
} else {
    console.warn('Geolocation not available.')
}


Using the location to calculate distances with Drupal hook

Then, using the THEME_views_post_render(\Drupal\views\ViewExecutable $view, &amp;$output) hook I check that $view-&gt;id() is the view in which I want to make the calculations and I get the data from the URL using the Drupal function \Drupal::request()-&gt;query-&gt;get('latitude'); (and longitude).

From that point, I decode the JSON variable $output['#markup'] and change the JSON objects the way I need to fulfill my requirements.

if ($view-&gt;id() == 'farmacias_perto') {
        $user_latitude = \Drupal::request()-&gt;query-&gt;get('latitude');
        $user_longitude = \Drupal::request()-&gt;query-&gt;get('longitude');
        $json_result = json_decode($output['#markup']-&gt;jsonSerialize());

        foreach ($json_result as $pharmacy) {
            $latlong = explode(',', $pharmacy-&gt;field_geolocation);
            $pharmacy_latitude = $latlong[0];
            $pharmacy_longitude = $latlong[1];
            $distance = distance($pharmacy_latitude, $pharmacy_longitude, $user_latitude, $user_longitude);
            $pharmacy-&gt;distance = round($distance);
        }

        $output['#markup'] = json_encode($json_result);
}


I used the distance function explained here.