Done in 60 seconds: a location based service example using IP Geolocation

Apart from ready-to-use maps of your site's visitor locations, the IP Geolocation module also comes with an API to help you realise location based services on your site. Here we consider the use-case of a travelling writer who updates a blog during her journey across the globe. With IP Geolocation and a few lines of code we are able to automatically insert at the top of each new entry the location the writer is blogging from, even if she isn't quite sure herself where she is today!

All we need to do is to add a location field to the blog or article content type and implement a couple of hooks. Here's the scoop.

  • We assume you have configured IP Geolocation as per its README (up to point A3 is sufficient).
  • At Structure >> Content type >> Manage fields add a field of type text, with machine name field_location. The label can be anything you like. Accept the defaults for all remaining options and save. Then at Manage display drag the field down into the Hidden area and press Save.
  • To auto-insert the writer's location we implement a form hook. Copy the code below into any existing module on your system (the dirty way) or create a new module to hold this function (the clean way). Replace the prefix MYMODULE by the real name of the module you place this code in. The function will fill out the location field with the user's current address (as retrieved by IP Geolocation) when she creates a new piece of content. She can then edit it or leave it as is before saving.
    /**
     * Implements hook_form_BASEFORMID_alter()
     */
    function MYMODULE_form_node_form_alter(&$form, &$form_state, $form_id) {
      if (isset($form['field_location']) && isset($_SESSION['ip_geoloc'])) {
        $language = $form['#node']->language;
        if (empty($form['field_location'][$language][0]['value']['#default_value'])) {
          $location = $_SESSION['ip_geoloc']['location']['formatted_address'];
          $form['field_location'][$language][0]['value']['#default_value'] = $location;
        }
      }
    }
    
  • While we could simply display the location information via the field (normal behaviour), here we'll demonstrate how to show the address as part of the "Submitted by... on ..." text that appears above each blog entry (provided "Display author and date information" is ticked for the content type). For this we implement a suitable template preprocess hook, like shown below. You'd normally add this function to the template.php file in your sites/all/themes/MYTHEME theme or subtheme folder (see also Done in 60 seconds: creating a subtheme). Or you can replace MYTHEME by the name of the module that you put this function in.
    function MYTHEME_preprocess_node(&$vars) {
      if (!empty($vars['field_location']) && !empty($vars['submitted'])) {
        $location = $vars['field_location'][0]['value'];
        $vars['submitted'] .= ', ' . t('while in') . " $location";
      }
    }
    

Voila! Every time the writer creates a new blog entry, the location field is automatically fleshed out with her current address. When, after saving, the blog entry is viewed, the top of the page will display something like Submitted by TravellingLight on Tue, 17/01/2012 - 1:58pm, while in 329-330 Beaconsfield Parade, St Kilda West VIC 3206, Australia.

Naturally, this is only scratching the surface of what you can do when you have your visitor locations handy at all times, e.g. in the session as provided by IP Geolocation. Think of apps like foursquare. You can have users "check in" at wherever they happen to be, without having to enter their locations. More ideas on the IP Geolocation project page.

Note: a fully functional add-on module, ip_geoloc_node, based on the above ideas is attached for your convenience

File under: 

Planet Drupal

Attachment 

Comments