Bdragon wrote some code for me on Coulee Region Online that I find incredibly useful. I have an address field that's just a simple textarea CCK field. The user types in the address or even a close approximation, hits tab, and it magically transforms into co-ordinates and plots on the map. I can't imagine I'm the only one that would find this useful. Not everyone needs an address broken up into pieces and this works wonderfully if you don't.

I would really love to see this become an official part of the module rather than having to maintain a hacked up address.js file and custom code. What do you think? Anyone else interested in this?

Michelle

Comments

michelle’s picture

Here's the code involved:

In function gmap_gmap()

      if (isset($map['behavior']['locpick']) && $map['behavior']['locpick']) {
        drupal_add_js($path . 'locpick.js');
        // @CROCUSTOM  Add this line to make the one textbox address work.
        drupal_add_js($path .'address.js');
      }

Then in address.js, comment out every line that sets the address field back to "Enter an address". We want to keep the data in that field so never clear it out, even if they move the marker on the map to correct a bad geocoding. Maybe this could be done with a setting or perhaps just use a different .js file?

Finally, this bit of custom code goes in hook_form_alter() for the form with the address field. It attaches the map to the address field and makes it all work together. It would need to either be generalized to know what field names you're using or require using specific names for the fields. One thing to watch out for is that if you put your field in a fieldset, the fieldset needs to be added to the code as well.

  $map = gmap_parse_macro('[gmap ]');
  $map['id'] = 'loc0';
  $map['zoom'] = 15;
  $map['behavior']['locpick'] = TRUE;

  $form['field_address'][0]['_map'] = array(
    '#type' => 'gmap',
    '#map' => $map['id'],
    '#settings' => $map,
  );
  
  $remap = array(
    'edit-field-latitude-0-value' => array('classes' => array('gmap-locpick_latitude'), 'id' => 'gmap-loc0-locpick_latitude0'),
    'edit-field-longitude-0-value' => array('classes' => array('gmap-locpick_longitude'), 'id' => 'gmap-loc0-locpick_longitude0'),
    'edit-field-address-0-value' => array('classes' => array('gmap-locpick_address'), 'id' => 'gmap-loc0-locpick_address0'),
  );
  
  drupal_add_js(array('gmap_remap_widgets' => $remap), 'setting');
  
  $form['field_latitude'][0]['value']['#map'] = $id;
  gmap_widget_setup($form['field_latitude'][0]['value'], 'locpick_latitude');

  $form['field_longitude'][0]['value']['#map'] = $id;
  gmap_widget_setup($form['field_longitude'][0]['value'], 'locpick_longitude');

  $form['field_address'][0]['value']['#map'] = $id;
  gmap_widget_setup($form['field_address'][0]['value'], 'locpick_address');
drupalfan81’s picture

Which file is this function in?

function gmap_gmap()

Can you please specify so the rest of us can test this out?

Thanks!

hutch’s picture

grep -rn 'gmap_gmap(' *
finds it in gmap.module, line 96

ccheu’s picture

Michelle,

Thanks for the information. I am using Node Locations from the Locations module. I've created a content type activated Node Locations for this type. But I still can't seem to make the GMap do the geocoding locpick from the Street address.

Where do I enter the codes for the mod concerning the hook_form_alter() ? Are there any changes required to the code or do I just slot it somewhere in a file? For my case, is it the location_node.module file? But where do I slot it?

By the way, I have successfully done first two parts of your suggestion (add address.js and comment out the "Enter an Address." Works well when I go to "/search/location" under the "Advance Search/Proximity" section. Thanks for that!

Regards,

Chang

michelle’s picture

@ccheu: It's meant to go in your own module and, no, it can't be just dropped in as is. If you don't know how to use form alters, you should read up on FAPI first.

Michelle

Madis’s picture

Thanks Bdragon for writing and Michelle for sharing the code! Helped me out immensely in both adding the mentioned gmap functionality to a content type with CCK textfields and creating a custom form with the same features. I managed to avoid hacking GMap module files by creating a copy of address.js (customized and loaded it on the specific pages where I needed). It seemed like the 3 last blocks of code (setting map id-s and widgets) weren't necessary when adding the functionality to my content type CCK fields.

I needed the map to zoom in after the search as well, which I managed to do with the following code snippet (took me a while to find something that worked):

obj.vars.zoom = 12;
obj.map.setZoom(obj.vars.zoom);

Also supporting the idea of making this an official part of GMap.

avpaderno’s picture

Issue summary: View changes
Status: Active » Closed (outdated)

I am closing this issue, as it has been created for a release that is now not supported.