Files
thetool/lib/Gmaps/Geocoding.php
Frank Schubert 33d7a4e5f1 WIP RTR Reporting
2024-12-06 13:45:20 +01:00

58 lines
1.4 KiB
PHP

<?php
class Gmaps_Geocoding {
public static function getCoords($search) {
$log = mfLoghandler::singleton();
$components = [];
if(is_array($search)) {
if(!$search['country'] || !$search['street'] || !$search['zip'] || !$search['city']) {
return false;
}
$components = [
'country:' . urlencode($search['country']),
'postal_code:' . urlencode($search['zip']),
'locality:' . urlencode($search['city']),
];
$address = $search['street'];
} else {
$address = $search;
}
$address = urlencode($address);
$url = TT_GEOCODING_API_URL."?address=$address&key=".TT_GEOCODING_API_SECRET;
$cstr = "";
if(count($components)) {
$component_string = implode("|", $components);
$url .= "&components=$component_string";
}
//$log->debug(__FILE__.": $url");
$resp = file_get_contents($url);
//print_r($resp);
if($resp) {
$json = json_decode($resp);
if($json->status != "OK") {
$log->warn(__FILE__.": No Results ($url)");
return false;
}
$results = $json->results;
/*if(count($results) > 1) {
$log->warn(__FILE__.": Got more then 1 result. Aborting. ($url)");
return false;
}*/
$lat = $results[0]->geometry->location->lat;
$long = $results[0]->geometry->location->lng;
return [$lat,$long];
} else {
return false;
}
}
}