http://drupal.org/node/255995

A few changes for Drupal 7 to accommodate the database provided in the above link. The changes support lat / lon for users and proximity search.

location.ca.inc

<?php 
function location_latlon_rough_ca($location = array()) {
  if (!isset($location['postal_code'])) {
    return NULL;
  }

	$postal_code = $location['postal_code'];
  while (strlen($postal_code) >= 3) {
    $row = db_query("SELECT latitude, longitude FROM {zipcodes} WHERE country = :country AND zip LIKE :zip", array(':country' => $location['country'], ':zip' => db_like($postal_code) . '%'))->fetchObject();

    if ($row) {
      return array('lat' => $row->latitude, 'lon' => $row->longitude);
    }
    $postal_code = trim(substr($postal_code, 0, -1));
  }
  return NULL;
}

function location_get_postalcode_data_ca($location = array()) {
	$postal_code = $location['postal_code'];
  // Now we pad the thing and query.
  while (strlen($postal_code) >= 3) {
    $row = db_query("SELECT * FROM {zipcodes} where country = :country AND zip LIKE :zip", array(':country' => $location['country'], ':zip' => db_like($postal_code) . '%'))->fetchObject();
    
    if ($row) {
      return array('lat' => $row->latitude, 'lon' => $row->longitude, 'city' => $row->city, 'province' => $row->state, 'country' => $row->country);
    }
    $postal_code = trim(substr($postal_code, 0, -1));
  }
  return NULL;
}
?>
CommentFileSizeAuthor
location.ca_.inc_.patch2.05 KBnathaniel

Comments

shiraz dindar’s picture

Nathaniel,

D7 Canadian postal code proximity searches are working for me already, after having added the DB from #20 as in your link, without patching. That is to say, if a user provides only their postal code during registration, location will correctly geocode their lat/lon, and then proximity searches work fine.

Is this because you are addressing something different in your patch, or is it because your patch has already been rolled in dev?

Thanks,
Shiraz

nathaniel’s picture

I believe the patch addresses an issue if someone enters a partial postal code or a postal code that is not in the database provided in the link. So it will select the most accurate postal code if not found.

Quoted from this comment: http://drupal.org/node/255995#comment-2620136

What it does:
Searches first for the entire zip code - if nothing is returned, it will continue to lop off the last character and search for any zip code beginning with that sequence. If it gets down to the first three and STILL doesn't find a matching zip code, it will return with the standard failure mode.

This is great for those of us who do only need to do approximate proximity searches or those who don't know their exact postal code (which is a valid concern we have gotten from customers)

colan’s picture

Title: Canada Zipcodes DB » Drupal 7 fixes for the Canada zipcodes DB
podarok’s picture

Status: Needs review » Active

#1931088: [META] Fixing tests tests were broken, so triggering to active

podarok’s picture

Status: Active » Needs review

bot

Status: Needs review » Needs work

The last submitted patch, location.ca_.inc_.patch, failed testing.