Recently (actually, an hour ago) I have been creating a Drupal module which should create a form and process it. The purpose of this form was to replace the node add form so the only process action has to be the node_save call. This was quite simple, however, this form should contain gmap widget as a coordinate chooser. It took me a while to get it work so I would like to share my knowledge.

Firstly, how to we display the coordinate chooser as a form element. After long googling I have found a function gmap_set_location. It accepts three parameters. The first one, $map, is a gmap macro which you want to use to display the map coordinate chooser. The second one, &$form, is a reference to the particular form, you want to add this widget to. The third parameter is an array containing names of fields storing information to process. latitude and longitude keys are required, the address key is optional. So the form (or the location piece) itself will look like this:

  1. $map_macro = '[gmap|id=usermap|center=0,30|zoom=13|width=400px|height=400px]';
  2. $form = array();
  3.  
  4. $form['title'] = array(
  5. '#type' => 'textfield',
  6. '#title' => t('Location title'),
  7. '#required' => TRUE,
  8. );
  9.  
  10. $form['latitude'] = array(
  11. '#type' => 'hidden',
  12. '#default_value' => '48.15509285476016',
  13. );
  14.  
  15. $form['longitude'] = array(
  16. '#type' => 'hidden',
  17. '#default_value' => '17.105712890625',
  18. );
  19.  
  20. $form['gmap']['#value'] = gmap_set_location($map_macro, $form, array('latitude'=>'latitude','longitude'=>'longitude'));

So this is the first bit - the gmap coordinate chooser display. Of course, you do not have to make the latitude and longitude fields hidden - if you think visitors are able to enter these manually, make them as textfields. Also, I have added the title field, so the form makes at least some sense.

Now lets go to process the form. We can break down the processing to two steps - save the location and save the node. And that's very easy, the only thing we need to know is, how to save the location. Fortunately, there is a location_save function, which does all the dirty job for us. It accepts an array containing all the location information. As we want to store only the coordinates, it would like somehow like this:

  1. $location = array();
  2. $location['latitude'] = $form_state['values']['latitude'];
  3. $location['longitude'] = $form_state['values']['longitude'];
  4. $location['inhibit_geocode'] = TRUE;
  5. $lid = location_save($location);

Please, notice the very important key inhibit_encode. The default processing of storing location means to get the coordinates from address geocoding and storing them. So when you only send coordinates and no address, location module will ignore coordinates and will try to get them from (none) address. The result will be latitude and longitude set to 0. Not anything we would like to see. So what we need to do is to inhibit geocoding and this is achieveable by setting the inhibit_geocode key to TRUE value.

When the location_save function finishes successfully, it will return the location id ($lid). With the new node you will save it like this:

  1. $node->locations[0]['lid'] = $lid;
  2.  
  3. // other stuff
  4.  
  5. node_save($node);

Easy, uh?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <css>, <drupal5>, <drupal6>, <html4strict>, <java>, <javascript>, <php>, <python>, <ruby>.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.