View source
<?php
$plugin = array(
'title' => t('Latitude / Longitude'),
'description' => t('Parse location from freeform latitude and longitude string'),
'callback' => 'geocoder_latlon',
'field_types' => array(
'text',
'text_long',
'text_with_summary',
'computed',
),
'field_callback' => 'geocoder_latlon_field',
);
function geocoder_latlon($latlon_string, $options = array()) {
geophp_load();
$delims = array(
',',
'/',
' ',
"\t",
);
foreach ($delims as $delim) {
$parts = explode($delim, $latlon_string);
if (count($parts) === 2) {
$lat = geocoder_latlon_dms_to_dec(trim($parts[0]));
$lon = geocoder_latlon_dms_to_dec(trim($parts[1]));
return new Point($lon, $lat);
}
}
return FALSE;
}
function geocoder_latlon_dms_to_dec($dms) {
if (is_numeric($dms)) {
return $dms;
}
if (stripos($dms, 'H') !== FALSE && stripos($dms, 'M') !== FALSE) {
$dms = strtr($dms, "'\"HOURSMINTECNDAhoursmintecnda", ' ');
$dms = preg_replace('/\\s\\s+/', ' ', $dms);
$dms = explode(' ', $dms);
$deg = $dms[0];
$min = $dms[1];
$sec = $dms[2];
$dec = floatval($deg * 15 + $min / 4 + $sec / 240);
return $dec;
}
if (stripos($dms, 'S') !== FALSE || stripos($dms, 'W') !== FALSE) {
$direction = -1;
}
else {
$direction = 1;
}
$dms = strtr($dms, "�'\"NORTHSEAWnorthseaw'", ' ');
$dms = preg_replace('/\\s\\s+/', ' ', $dms);
$dms = explode(' ', $dms);
$deg = $dms[0];
$min = $dms[1];
$sec = $dms[2];
$dec = floatval($deg + ($min * 60 + $sec) / 3600);
if ($dec > 0) {
$dec = $direction * $dec;
}
return $dec;
}
function geocoder_latlon_field($field, $field_item) {
return geocoder_latlon($field_item['value']);
}