Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I am working on a project and I think I am stuck on this. I have a form that I want to add points to a map as users edit it.

Here's a rundown on how the form works:

The user chooses a number (1-10) for how many stops they'd like to make from a select list. The select list defaults to 1 and below it there is a select list that lists all of the points of interest they can choose from. If they change the first select to 2 the ajax callback updates the form and adds an additional select box of POIs. That's working great.

What I'd like to be able to do is add the POI to a map on the page whenever they select it (with driving directions between them). I have an ajax callback on the POI select list in order to do this but I am having trouble figuring out where to go from here.

I have the following in my POI callback (for testing) and it works, but I am having trouble figuring out what ajax_command_ function to use to call a js function to add the point to the map.

$commands = array();
$commands[] = ajax_command_alert("Here!");
return array('#type' => 'ajax', '#commands' => $commands);

Basically I just need to pass the latitude and longitude of the POI (which I am gathering in the callback) and send it to a js function to then update the map. My first thought was using drupal_add_js but I couldn't get it working and discovered later that it was not the right way to go.

Any thoughts?

share|improve this question
    
custom ajax commands. let me search on google and i'll post a real answer –  Marius Ilie Aug 31 '12 at 21:29
add comment

1 Answer

PHP:

$commands[] = ajax_command_alert("Here!");
$commands[] = array(
  "command" => 'your_custom_command',
  "latitude" => "45.23",
  "longitude" => "23.25"
);

Javascript:

Drupal.ajax.prototype.commands.your_custom_command = function(ajax, response, status) {
  if( response.latitude != "" ) {
    // do something
  }
};
share|improve this answer
1  
How about some explanation? Context? References for further documentation? –  Caleb Sep 1 '12 at 8:46
    
This is actually the correct answer. The PHP above is what goes in the function referred to by #ajax['callback']. The javascript is extending Drupal's built in commands to add a new one. The response parameter holds the data passed in the php array above. –  greggory.hz Nov 17 '12 at 18:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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