View source
<?php
function location_province_list_ca() {
return array(
'AB' => 'Alberta',
'BC' => 'British Columbia',
'MB' => 'Manitoba',
'NB' => 'New Brunswick',
'NL' => 'Newfoundland and Labrador',
'NS' => 'Nova Scotia',
'ON' => 'Ontario',
'PE' => 'Prince Edward Island',
'QC' => 'Quebec',
'SK' => 'Saskatchewan',
'NT' => 'Northwest Territories',
'NU' => 'Nunavut',
'YT' => 'Yukon Territory',
);
}
function location_province_list_numeric_ca() {
return array(
'001' => 'Alberta',
'002' => 'British Columbia',
'003' => 'Manitoba',
'004' => 'New Brunswick',
'005' => 'Newfoundland and Labrador',
'006' => 'Nova Scotia',
'007' => 'Ontario',
'008' => 'Prince Edward Island',
'009' => 'Quebec',
'010' => 'Saskatchewan',
'011' => 'Northwest Territories',
'012' => 'Nunavut',
'013' => 'Yukon Territory',
);
}
function location_latlon_rough_ca($location = array()) {
if (!isset($location['postal_code'])) {
return NULL;
}
$result = db_query("SELECT latitude, longitude FROM {zipcodes} WHERE country = :country AND zip = :zip", array(
':country' => $location['country'],
':zip' => $location['postal_code'],
));
if (($row = $result
->fetchObject()) !== FALSE) {
return array(
'lat' => $row->latitude,
'lon' => $row->longitude,
);
}
else {
return NULL;
}
}
function location_get_postalcode_data_ca($location = array()) {
$result = db_query("SELECT * FROM {zipcodes} where country = :country AND zip = :zip", array(
':country' => $location['country'],
':zip' => $location['postal_code'],
));
if (($row = $result
->fetchObject()) !== FALSE) {
return array(
'lat' => $row->latitude,
'lon' => $row->longitude,
'city' => $row->city,
'province' => $row->state,
'country' => $row->country,
);
}
else {
return NULL;
}
}
function location_latlon_exact_ca($location = array()) {
}
function location_map_link_ca_yahoo($location = array()) {
$get_query = '?';
if (isset($location['street'])) {
$get_query .= 'addr=' . urlencode($location['street']) . '&';
}
if ($location['province'] != '' || $location['city'] != '' || $location['postal_code'] != '') {
$get_query .= 'csz=' . _location_ca_yahoo_csz_get_field($location) . '&';
}
$get_query .= 'country=' . urlencode($location['country']);
return 'http://ca.maps.yahoo.com/maps_result' . $get_query;
}
function location_map_link_ca_google($location = array()) {
$query_params = array();
foreach (array(
'street',
'city',
'province',
'postal_code',
'country',
) as $field) {
if (isset($location[$field])) {
$query_params[] = $location[$field];
}
}
if (count($query_params)) {
return 'http://maps.google.ca?q=' . urlencode(implode(", ", $query_params));
}
else {
return NULL;
}
}
function location_map_link_ca_mapquest($location = array()) {
if (isset($location['street'])) {
$get_query .= 'address=' . urlencode($location['street']) . '&';
}
if (isset($location['city'])) {
$get_query .= 'city=' . urlencode($location['city']) . '&';
}
if (isset($location['province'])) {
$get_query .= 'state=' . urlencode($location['province']) . '&';
}
if (isset($location['postal_code'])) {
$get_query .= 'zipcode=' . urlencode($location['postal_code']);
}
if (strlen($get_query)) {
return 'http://www.mapquest.com/maps/map.adp?searchtype=address&country=CA&' . $get_query;
}
else {
return NULL;
}
}
function location_map_link_ca_providers() {
return array(
'google' => array(
'name' => 'Google Maps',
'url' => 'http://maps.google.ca',
'tos' => 'http://www.google.ca/help/terms_local.html',
),
'yahoo' => array(
'name' => 'Yahoo! Maps',
'url' => 'http://ca.maps.yahoo.com',
'tos' => 'http://help.yahoo.com/help/ca/maps/maps-25.html',
),
'mapquest' => array(
'name' => 'MapQuest',
'url' => 'http://www.mapquest.com',
'tos' => 'http://www.mapquest.com/features/main.adp?page=legal',
),
);
}
function location_map_link_ca_default_providers() {
return array(
'google',
);
}
function location_driving_directions_link_ca($location_a, $location_b) {
return _location_driving_directions_link_ca_yahoo($location_a, $location_b);
}
function _location_driving_directions_link_ca_yahoo($location_a, $location_b) {
if (trim($location_b['country']) != 'ca' && trim($location_b['country']) != 'us') {
return '';
}
foreach ($location_a as $field => $value) {
$location_a[$field] = trim($value);
}
foreach ($location_b as $field => $value) {
$location_b[$field] = trim($value);
}
if (_location_ca_enough_fields_for_yahoo($location_a) && _location_ca_enough_fields_for_yahoo($location_b)) {
$yahoo_maps_path = 'dd_result';
}
else {
$yahoo_maps_path = 'dd';
}
$get_query = '?';
$get_query .= 'addr=' . urlencode($location_a['street']) . '&';
$get_query .= 'csz=' . _location_ca_yahoo_csz_get_field($location_a) . '&';
$get_query .= 'country=' . urlencode($location_a['country']) . '&';
$get_query .= 'taddr=' . urlencode($location_b['street']) . '&';
$get_query .= 'tcsz=' . _location_ca_yahoo_csz_get_field($location_b) . '&';
$get_query .= 'tcountry=' . urlencode($location_b['country']);
$get_query .= '&getrte=' . urlencode('Get Directions');
return 'http://ca.maps.yahoo.com/' . $yahoo_maps_path . $get_query;
}
function _location_ca_enough_fields_for_yahoo($location) {
if (strlen($location['street']) && strlen($location['city']) && strlen($location['province'])) {
return TRUE;
}
if (strlen($location['street']) && strlen($location['postal_code'])) {
return TRUE;
}
if (strlen($location['street']) && strlen($location['city']) && strlen($location['province'])) {
return TRUE;
}
return FALSE;
}
function _location_ca_yahoo_csz_get_field($location) {
if ($location['country'] = 'ca') {
if (strlen($location['city']) && strlen($location['province'])) {
return urlencode($location['city'] . ', ' . $location['province']);
}
if (strlen($location['postal_code'])) {
return urlencode($location['postal_code']);
}
}
else {
if (strlen($location['postal_code'])) {
return urlencode($location['postal_code']);
}
if (strlen($location['city']) && strlen($location['province'])) {
return urlencode($location['city'] . ', ' . $location['province']);
}
}
if (strlen($location['city']) || strlen($location['province'])) {
if (strlen($location['city'])) {
return urlencode($location['city']);
}
else {
return urlencode($location['province']);
}
}
return '';
}
function location_geocode_ca_providers() {
return array(
'geocoder' => array(
'name' => 'GeoCode.ca geocoding service',
'url' => 'http://geocoder.ca/?api=1',
'tos' => 'http://geocoder.ca/?terms=1http://geocoder.ca/?terms=1',
),
);
}
function location_geocode_ca_geocoder_settings() {
$form = array();
$form['location_geocode_ca_geocoder_apikey'] = array(
'#type' => 'textfield',
'#title' => t('Geocoder API Key'),
'#size' => 64,
'#maxlength' => 128,
'#default_value' => variable_get('location_geocode_ca_geocoder_apikey', ''),
'#description' => t('In order to use the Geocoder.ca API geocoding web-service, if you are a commercial entity or wish to use the service without attribution, you will need a Geocoder API Key. You can obtain one at the !sign_up_link for the !geocoder_api.', array(
'!sign_up_link' => '<a href="http://geocoder.ca/?register=1">sign-up page</a>',
'!geocoder_api' => '<a href="http://geocoder.ca/?premium_api=1">Geocoder API</a>',
)),
);
return $form;
}
function location_geocode_ca_geocoder($location = array()) {
$service_url = "http://geocoder.ca/?geoit=XML";
if (variable_get('location_geocode_ca_geocoder_apikey', NULL)) {
$service_url .= '&auth=' . variable_get('location_geocode_ca_geocoder_apikey', NULL) . '&locate=';
}
else {
$service_url .= '&locate=';
}
unset($location['postal_code'], $location['country']);
$address = strtr(location_address2singleline($location), array(
', ' => ' ',
));
$http_reply = drupal_http_request($service_url . urlencode($address));
if ($http_reply->code == 400) {
return NULL;
}
else {
$matches = array();
$lat_match = array();
$lon_match = array();
$latlon = array();
if (preg_match('/<error>(.*)<\\/error>/', $http_reply->data, $lat_match)) {
return NULL;
}
if (preg_match('/<latt>(.*)<\\/latt>/', $http_reply->data, $lat_match)) {
$latlon['lat'] = $lat_match[1];
}
else {
return NULL;
}
if (preg_match('/<longt>(.*)<\\/longt>/', $http_reply->data, $lon_match)) {
$latlon['lon'] = $lon_match[1];
return $latlon;
}
else {
return NULL;
}
}
}
function location_bounds_ca() {
return array(
'minlng' => -141.00554,
'minlat' => 41.6690855,
'maxlng' => -52.61593,
'maxlat' => 83.1161164,
);
}