I have working a site that needs mileage given between user coordinate and node coordinate. using location module. I thought it was returning wrong results, when I discovered it returns actual miles "as the crow flies" so to speak. I determined that when looking at what it plotted on a map. I need driving distance.

I am trying the store locator module (but it really doesn't let me do coordinates yet. But the mileage doesn't display there, and views doesn't let me do that, that I can tell.

Everything I have tried has failed. Anyone doing this?

2 Answers 2


You can use The Google Distance Matrix API

If you want it to display in views there are a few ways you can implement it Either as a custom module, theme the view, or use the views php module.


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.= $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
        // Closing

        // 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;



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.