Hi guys,

I have my get redirections configured to show up in a colorbox.

People can calculate distance, etc...i wanted to know how I may retrieve the calculated value for distance.

I would want this sent with a webform data if that's possible (so maybe if it can be stored in a global variable/url argument maybe??) I'm a newbie at coding so if i'd like to know the best implementation.

Thanks.

Comments

hutch’s picture

What Getdirections does is provide a map with a form, once that is loaded it does not communicate with the server anymore. All the interactions are managed by the javascript in one of the files
getdirections_v3a.js
getdirections_v3b.js
getdirections_v3.js

Which one depends on which settings you have selected.
The javascript function that calculates Distance and Duration is function computeTotals(), which puts the calculated result in a div with id "getdirections_show_distance"

You would need to modify that to send the data back to the server using jQuery's $.get() function and you would need a php function to receive that data (presumably in a bespoke module), store it somewhere so that it would be available to whatever you want to do with it thereafter. You would also need to find a way to identify each session so that the data is stored uniquely, cookies might be a way to do that.

Alternately it might be possible to write your own javascript which monitors #getdirections_show_distance and returns it's content to the server, that way you avoid hacking Getdirections so you don't have to worry about updates overwriting your changes.

Although this is probably doable it would require a good understanding of jQuery and enough php programming skills to write a bespoke module to act as 'glue' between the various bits you want to join together.

chiko.mukwenha’s picture

Hi,

Thanks! I have had to revisit this issue, it keeps haunting me.

In that file, I would love to simply store the calculated distance in a $_POST['variable'] ... I have added this just below it:

      $.post("getdirections", {"duration": duration});

Hoping the /getdirections page gets the value, from then on...I can have a block with php code to store it until session closes. But right now, devel execute block isn't showing that it's set...what gives?

hutch’s picture

In order to get a piece of information stored on the server, you need to provide it with a menu path which points to a function that stores the info in whatever way you want it stored. To do this you need to create a bespoke module to do this for you.
All Getdirections does is provide a form and a map, everything thereafter is between the browser and Google.
If you want to modify the form you can either do that with theming (see getdirections.theme.inc) or by using a hook, see hook_form_alter on api.drupal.org

chiko.mukwenha’s picture

Thanks Hutch, for the quick reply.

I see what you mean, here's the plan i'm working with. HEADS UP: i'm still a newbie learning to code in Drupal so feel free to suggest ways on how I can do it better, would appreciate that.

The steps I see to storing a calculated distance are as follows:

1. User calculates distance on the form
2. Using hook_form_alter we add a form element of type 'value' that keeps just the distance. I intend to bundle this with a webform submission form later. so maybe, store with either variable_get/set then retrieve when needed. a submit function can do more complex things if need be.
3. After user has calculated distance...would be nice to have them click Finish or something like that that redirects them else where.

I have written the following hook_form_alter implementation in a module called desty that I have created.

dusty.module

<?php
/**
 * Implementation of hook_form_alter
 */

function desty_getdirections_direction_form_alter(&$form, &$form_state, $form_id){
	
	//Let's add a button that let's people proceeed to the next page!!
	$form['proceed'] = array(
		'#title' => 'Proceed',
		'#value' => 'Proceed',
		'#type' => 'submit',
		'#description' => 'A buttom to let people proceed to the check out page!',
		'#weight' => 30,
		);

        //Adding a new form element to store calculated distance
        $form['distance'] = array(
               '#type' => 'value',
               '#value' => //Need help here
        );

        //submit function will contain code that stores the distance value per session or feeds it to webform then redirect the user
	$form['#submit'][] = 'desty_form_submit';	

	return $form;
	 
?>

Basically I'd need help with setting a value for the new distance form element. i'm not sure how I can reach the calculated distance using jquery or javascript used by that form?

Thanks a lot, I hope that's the correct approach. Any pointers much appreciated.

hutch’s picture

Version: 7.x-2.0 » 7.x-2.x-dev

OK, first thing is to use type 'hidden' not type 'value'. The reason is that type 'value' is not actually sent to the browser whereas 'hidden' is and you need that in order to get the distance in the form.
See function computeTotals() in the javascript, it obtains and calculates the distance and puts it in id "getdirections_show_distance"

$("#getdirections_show_distance").html(Drupal.settings.getdirections.show_distance + ': ' + distance);

If you use the current dev version you can state which javascript file to use so you can copy the one you are using to your own one and edit that, eg copy getdirections_v3b.js to getdirections_v3b-bespoke.js and set that in the admin/config as the file to use. Then you can edit computeTotals() to poke your value into the form. But you don't need to alter the form, see below.

However this does not get your value back to the server, the form never goes back to the server, it's managed by javascript, so you need to send your value back to the server using jQuery's $.get() function.
You will need a bespoke 'helper' module to provide a path for the http request to send to, using hook_menu() and that will send the data to a function that will save the data wherever you tell it to.

Let's say you have a module 'myhelper' in sites/all/module/myhelper. It will need a myhelper.info and myhelper.module

myhelper.info

name = Myhelper
description = Site specific stuff.
package = Other
core = 7.x

myhelper.module


function myhelper_menu() {
  $items = array();
  $items['myhelper_test'] = array(
    'page callback'    => 'myhelper_test',
    'access callback'  => TRUE,
    'type'             => MENU_CALLBACK,
  );
  return $items;
}

function myhelper_test() {
  $distance = $_GET('distance');
  # store $distance
  exit;
}

add something like this to computeTotals()

$.get(Drupal.settings.basePath + "myhelper_test", {'distance': distance});

Remember that variable_get() and variable_set() do not store per-user so as soon as you have more than one user you are in a mess. You could use a cookie or if you want it permanent a db table.

Remember to flush cache after enabling the module....

chiko.mukwenha’s picture

Thanks Hutch!! It worked! You rock!

zeezhao’s picture

Hi. Assuming you had a content type called "Journey", and use location module to indicate journey points. Using computed_field module, please what is the correct way to calculate distance? Thanks

hutch’s picture

This question relates to the Location module and has nothing to do with Getdirections and should have been posted on the Location module issue queue, but nevermind, too late now, here is an answer.

In the Location module there is a file "earth.inc" which has a function earth_distance(), use that to find the distance between two lat/lon pairs.

zeezhao’s picture

Thanks for your reply. I assumed that to get distance of actual routes one would have to use getdirections. But the location will just give a kind of "straight-line" distance.

edit:
To be more precise, please how does one get distance after using functions like:

getdirections_locations_bylatlon()