Aprium Software

Drupal Location searching by UK postcode

I have been asked to configure the location 6.x-3.1-rc1 module to allows a postcode proximity search. This allows you to search for nodes within 5 miles of a postcode which is very powerful. Out of the box location 3.1 looks like it supports it, however when activated for the uk the search will just return nothing.

In the location module each country has its own include file in the ironically named supported folder. It seems that the UK file location.uk.inc has a couple of missing functions to handle finding the longitude and latitude of postcodes which I found on this post http://drupal.org/node/158101.

When setting up the location module for UK search, its important to remember to run through the steps in the install.txt. This shows you in step 6 how to import the UK postcodes with there longitude and latitude which are not installed by default.

Im reposting the 2 extra functions needed in location.uk.inc that nicholas Thompson wrote.

/**
* Returns a lat/lon pair of the approximate center of the given postal code in the given country
*
* @param $location
*   An associative array $location where
*     'street'       => the street portion of the location
*     'supplemental' => additional street portion of the location
*     'province'     => the province, state, or territory
*     'country'      => lower-cased two-letter ISO code (REQUIRED)
*     'postal_code'  => the international postal code for this location (REQUIRED)
*
* @return
*   An associative array where
*      'lat' => approximate latitude of the center of the postal code's area
*      'lon' => approximate longitude of the center of the postal code's area
*
*/
function location_latlon_rough_uk($location = array()) {
  if (!isset($location['postal_code'])) {
    return NULL;
  }

  $result = db_query("SELECT latitude, longitude FROM {zipcodes} WHERE country = '%s' AND UPPER(zip) = UPPER('%s')", $location['country'], $location['postal_code']);

  if ($row = db_fetch_object($result)) {
    return array('lat' => $row->latitude, 'lon' => $row->longitude);
  }
  else {
    return NULL;
  }
}


/**
* Returns a lat/lon pair of the approximate center of the given postal code in the given country
*
* @param $location
*   An associative array $location where only postal code and country are necessary, but can have the keys:
*     'street'       => the street portion of the location
*     'supplemental' => additional street portion of the location
*     'province'     => the province, state, or territory
*     'country'      => lower-cased two-letter ISO code (REQUIRED)
*     'postal_code'  => the international postal code for this location (REQUIRED)
*
* @return
*   An associative array where
*      'lat' => approximate latitude of the center of the postal code's area
*      'lon' => approximate longitude of the center of the postal code's area
*
*/
function location_get_postalcode_data_uk($location = array()) {

  // Now we query.
  $res = db_query("SELECT latitude, longitude FROM {zipcodes} WHERE country = '%s' AND UPPER(zip) = UPPER('%s')", $location['country'], $location['postal_code']);
  if ($row = db_fetch_object($res)) {
    return array('lat' => $row->latitude, 'lon' => $row->longitude, 'city' => $row->city, 'province' => $row->state, 'country' => $row->country);
  }
  else {
    return NULL;
  }
}

Comments

Hi I tried for ages awhile

Hi
I tried for ages awhile back to get UK proximity search working with Location, and as a designer rather than PHP expert found it very hard going.
So have you got this working on a production site, with those 2 functions added to the location.uk.inc?
Would be great to see a working example in Drupal if you have a URL you can share.
Cheers,
Luke

Hire

If you need a London based Drupal developer you can hire me. Check the availability page and contact me on info@aprium.net.

 

Powered by Drupal, an open source content management system

Copyright Aprium 2010