If you intend to show the actual driving distance between two points then you can use below code :-
function MY_MODULE_views_post_execute(&$view) {
if ($view->name == 'VIEW_NAME' ) {
//sort by driving road distance
$results = $view->result;
$nids = array();
foreach($results as $node){
$nids[] = $node->nid;
}
$nodes = _get_geo_position_value($nids); //gets geofield value as view result doesnt have the value
if(count($nodes) > 0){
$http_url = 'https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial';
$origin_text = "&origins=".$_COOKIE['currentLat'].",".$_COOKIE['currentLong']; //current location of user
$destination_origin = '&destinations=';
foreach($nodes as $node){
$lat = $node->field_gmap_position['und'][0]['lat'];
$lon = $node->field_gmap_position['und'][0]['lon'];
$str = $lat.','.$lon.'|';
$destination_origin.=$str;
// $destination_origin.= $destination_origin.$node->field_gmap_position['und'][0]['lat'].','.$node->field_gmap_position['und'][0]['lon'].'|';
}
$destination_origin = rtrim($destination_origin, "|"); //remove trailing |
$URL = $http_url.$origin_text.$destination_origin;
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$URL);
// Execute
$json_output=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
$calc_dist = json_decode($json_output, true);
for($i = 0; $i < count($results); $i++){
$results[$i]->field_google_calc_dist = $calc_dist['rows'][0]['elements'][$i]['distance']['value'];
}
//sort by google calculated distance
usort($results, function($a, $b)
{
if ($a->field_google_calc_dist == $b->field_google_calc_dist) {
return 0;
}
return ($a->field_google_calc_dist > $b->field_google_calc_dist) ? 1 : -1;
});
}
$view->result = $results;
}
}