View source
<?php
function location_latlon_rough_au($location = array()) {
if (!isset($location['postal_code'])) {
return NULL;
}
$result = db_query("SELECT latitude, longitude FROM {zipcodes} WHERE country = '%s' AND zip = '%s'", $location['country'], substr(str_pad($location['postal_code'], 4, '0', STR_PAD_LEFT), 0, 4));
if ($row = db_fetch_object($result)) {
return array(
'lat' => $row->latitude,
'lon' => $row->longitude,
);
}
else {
return NULL;
}
}
function _location_latlon_postalcode_au($location = array()) {
$dash_index = strpos($location['postal_code'], '-');
if (!($dash_index === FALSE)) {
$location['postal_code'] = substr($location['postal_code'], 0, $dash_index);
}
$res = db_query("SELECT * FROM {zipcodes} where country = '%s' AND zip = '%s'", $location['country'], str_pad($location['postal_code'], 4, "0", STR_PAD_LEFT));
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;
}
}
function location_latlon_exact_au($location = array()) {
return NULL;
}
function _location_latlon_exact_au_geocoder($location = array()) {
$location_string = '';
if (isset($location['street']) && trim($location['street']) != '') {
if (isset($location['postal_code'])) {
$location_string = $location['street'] . ' ' . $location['postal_code'];
}
elseif (isset($location['city']) && isset($location['province']) && trim($location['city']) != '' && trim($location['province'])) {
$location_string = $location['street'] . ', ' . $location['city'] . ', ' . $location['province'];
}
else {
return NULL;
}
}
else {
return NULL;
}
$result = xmlrpc('http://rpc.geocoder.us/service/xmlrpc', 'geocode', $location_string);
if (is_array($result) && is_array($result[0]) && isset($result[0]['lat']) && is_numeric($result[0]['lat']) && isset($result[0]['long']) && is_numeric($result[0]['long'])) {
return array(
'lat' => $result[0]['lat'],
'lon' => $result[0]['long'],
);
}
return NULL;
}
function location_map_link_au_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_au_yahoo_csz_get_field($location) . '&';
}
$get_query .= 'country=' . urlencode($location['country']);
return 'http://maps.yahoo.com/maps_result' . $get_query;
}
function location_map_link_au_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.com?q=' . urlencode(implode(", ", $query_params));
}
else {
return NULL;
}
}
function location_map_link_au_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=AU&' . $get_query;
}
else {
return NULL;
}
}
function location_map_link_au_providers() {
return array(
'google' => array(
'name' => 'Google Maps',
'url' => 'http://maps.google.com',
'tos' => 'http://www.google.com/help/terms_local.html',
),
'yahoo' => array(
'name' => 'Yahoo! Maps',
'url' => 'http://maps.yahoo.com',
'tos' => 'http://help.yahoo.com/help/us/maps/maps-24.html',
),
'mapquest' => array(
'name' => 'MapQuest',
'url' => 'http://www.mapquest.com',
'tos' => 'http://www.mapquest.com/features/main.adp?page=legal',
),
);
}
function location_map_link_au_default_providers() {
return array(
'google',
);
}
function location_geocode_au_providers() {
return array(
'yahoo' => array(
'name' => 'Yahoo! Maps Web Services',
'url' => 'http://developer.yahoo.com/maps/rest/V1/geocode.html',
'tos' => 'http://developer.yahoo.com/maps/mapsTerms.html',
),
'google' => array(
'name' => 'Google Maps API',
'url' => 'http://www.google.com/apis/maps/',
'tos' => 'http://www.google.com/apis/maps/terms.html',
),
);
}
function location_geocode_au_yahoo_settings() {
$form = array();
$form['location_geocode_au_yahoo_appid'] = array(
'#type' => 'textfield',
'#title' => t('Yahoo! Web Services Application ID'),
'#size' => 64,
'#maxlength' => 128,
'#default_value' => variable_get('location_geocode_au_yahoo_appid', 'YahooDemo'),
'#description' => t('Unless you are using this site to test and develop, you will need to obtain a Yahoo! Web Services Application ID from the %network_link. If you are using for development and testing purposes, you can use \'YahooDemo\' as your AppID. When getting an Application ID from Yahoo!, please also be sure to review the %usage_policy.', array(
'%network_link' => '<a href="http://api.search.yahoo.com/webservices/register_application">Yahoo! Developer Network</a>',
'%usage_policy' => '<a href="http://developer.yahoo.com/usagePolicy/index.html">usage policy</a>',
)),
);
return $form;
}
function location_geocode_au_yahoo($location = array()) {
$service_url = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=" . variable_get('location_geocode_au_yahoo_appid', "YahooDemo") . "&location=";
$address = location_address2singleline($location);
$http_reply = drupal_http_request($service_url . urlencode($address));
if ($http_reply->code == 400) {
return NULL;
}
else {
$matches = array();
preg_match('/precision="([a-z]*)"/', $http_reply->data, $matches);
if ($matches[1] != 'address') {
return NULL;
}
else {
$lat_match = array();
$lon_match = array();
$latlon = array();
if (preg_match('/<Latitude>(.*)<\\/Latitude>/', $http_reply->data, $lat_match)) {
$latlon['lat'] = $lat_match[1];
}
else {
return NULL;
}
if (preg_match('/<Longitude>(.*)<\\/Longitude>/', $http_reply->data, $lon_match)) {
$latlon['lon'] = $lon_match[1];
return $latlon;
}
else {
return NULL;
}
}
}
}
function location_geocode_au_google($location = array()) {
$service_url = 'http://maps.google.com/maps/geo?output=xml&key=' . variable_get('location_geocode_au_google_apikey', '') . '&q=';
$address = location_address2singleline($location);
$http_reply = drupal_http_request($service_url . urlencode($address));
$status_code_match = array();
preg_match('/<code>(.*)<\\/code>/', $http_reply->data, $status_code_match);
$status_code = $status_code_match[1];
if ($status_code != 200) {
watchdog('location', t('Google geocoding returned status code: %status_code', array(
'%status_code' => $status_code,
)));
return NULL;
}
$accuracy_code_match = array();
preg_match('/Accuracy="([0-9])"/', $http_reply->data, $accuracy_code_match);
$accuracy_code = $accuracy_code_match[1];
$min_accuracy = variable_get('location_geocode_au_google_accuracy_code', variable_get('location_geocode_google_minimum_accuracy', '3'));
if ($accuracy_code < $min_accuracy) {
watchdog('location', t('Google geocoding result for %country did not meet the minimum accuracy level of %min_accuracy. Result accuracy: %accuracy_code', array(
'%country' => $location['country'],
'%min_accuracy' => $min_accuracy,
'%accuracy_code' => $accuracy_code,
)));
return NULL;
}
$latlon_match = array();
preg_match('/<coordinates>(.*)<\\/coordinates>/', $http_reply->data, $latlon_match);
$latlon_exploded = explode(',', $latlon_match[1]);
return array(
'lat' => $latlon_exploded[1],
'lon' => $latlon_exploded[0],
);
}
function location_geocode_au_google_settings() {
$form = array();
$form['location_geocode_au_google_apikey'] = array(
'#type' => 'textfield',
'#title' => t('Google Maps API Key'),
'#size' => 64,
'#maxlength' => 128,
'#default_value' => variable_get('location_geocode_au_google_apikey', ''),
'#description' => t('In order to use the Google Maps API geocoding web-service, you will need a Google Maps API Key. You can obtain one at the !sign_up_link for the !google_maps_api.', array(
'!sign_up_link' => '<a href="http://www.google.com/apis/maps/signup.html">sign-up page</a>',
'!google_maps_api' => '<a href="http://www.google.com/apis/maps/">Google Maps API</a>',
)),
);
$form['location_geocode_au_google_accuracy_code'] = array(
'#type' => 'select',
'#title' => t('Google Maps Geocoding Accuracy for Australia'),
'#default_value' => variable_get('location_geocode_au_google_accuracy_code', variable_get('location_geocode_google_minimum_accuracy', '3')),
'#options' => location_google_geocode_accuracy_codes(),
'#description' => t('The minimum required accuracy for the geolocation data to be saved.'),
);
return $form;
}
function location_driving_directions_link_au($location_a, $location_b) {
return _location_driving_directions_link_au_yahoo($location_a, $location_b);
}
function _location_driving_directions_link_au_yahoo($location_a, $location_b) {
if (trim($location_b['country']) != 'ca' && trim($location_b['country']) != 'au') {
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_au_enough_fields_for_yahoo($location_a) && _location_au_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_au_yahoo_csz_get_field($location_a) . '&';
$get_query .= 'country=' . urlencode($location_a['country']) . '&';
$get_query .= 'taddr=' . urlencode($location_b['street']) . '&';
$get_query .= 'tcsz=' . _location_au_yahoo_csz_get_field($location_b) . '&';
$get_query .= 'tcountry=' . urlencode($location_b['country']);
$get_query .= '&getrte=' . urlencode('Get Directions');
return 'http://maps.yahoo.com/' . $yahoo_maps_path . $get_query;
}
function _location_au_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_au_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['state'])) {
if (strlen($location['city'])) {
return urlencode($location['city']);
}
else {
return urlencode($location['state']);
}
}
return '';
}
function _location_au_geocoder_oneline($location = array()) {
$line = '';
$line .= $location['street'] . ', ';
if (strlen($location['city']) && strlen($location['province']) && strlen($location['postal_code'])) {
$line .= $location['city'] . ', ' . $location['province'] . ' ' . $location['postal_code'];
}
elseif (strlen($location['city']) && strlen($location['province'])) {
$line .= $location['city'] . ', ' . $location['state'];
}
elseif (strlen($location['postal_code'])) {
if (strlen($location['city']) || strlen($location['state'])) {
if (strlen($location['city'])) {
$line .= $location['city'] . ', ' . $location['postal_code'];
}
else {
$line .= $location['state'] . ', ' . $location['postal_code'];
}
}
else {
$line .= $location['postal_code'];
}
}
return $line;
}
function location_province_list_au() {
return array(
'ACT' => 'Australian Capital Territory',
'NSW' => 'New South Wales',
'NT' => 'Northern Territory',
'QLD' => 'Queensland',
'SA' => 'South Australia',
'TAS' => 'Tasmania',
'VIC' => 'Victoria',
'WA' => 'Western Australia',
);
}
function location_province_list_numeric_au() {
return array(
'001' => "New South Wales",
'002' => "Queensland",
'003' => "South Australia",
'004' => "Tasmania",
'005' => "Victoria",
'006' => "Western Australia",
'007' => "Australian Capital Territory",
'008' => "Northern Territory",
);
}
function location_bounds_au() {
return array(
'minlng' => 72.57625,
'minlat' => -53.259967,
'maxlng' => 159.4839,
'maxlat' => -10.078033,
);
}