Skip to content
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 13 additions & 26 deletions Classes/Service/GeoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ class GeoService

/**
* base URL to fetch the Coordinates (Latitude, Longitutde of a Address String.
* TODO: Language shall be configurable
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still not configurable, so I'd leave this todo in there.

*/
protected $geocodingUrl = 'https://maps.googleapis.com/maps/api/geocode/json?language=de&sensor=false';

/**
* constructor method.
*
* sets the google code API key
* @param string $apikey (optional) the API key from google, if empty, the default from the configuration is taken
*
* @param string $apiKey (optional) the API key from google, if empty, the default from the configuration is taken
*/
public function __construct($apiKey = null)
{
Expand All @@ -66,8 +67,10 @@ public function __construct($apiKey = null)
if ($apiKey === null) {
$apiKey = $geoCodingConfig['googleApiKey'] ?: '';
}
$this->apikey = $apikey;
$this->geocodingUrl .= '&key=' . $apikey;
if (!empty($apiKey)) {
$this->geocodingUrl .= '&key=' . $apiKey;
}
$this->maxRetries = (int)$geoCodingConfig['maxRetries'];
}

/**
Expand All @@ -79,31 +82,14 @@ public function __construct($apiKey = null)
* @param $city
* @param $country
*
* @return array|null an array with long name, latitude and longitude or null if nothing was found
* @return array an array with latitude and longitude
*/
public function getCoordinatesForAddress($street = null, $zip = null, $city = null, $country = 'Germany'): array
{
$results = null;

$address = $street . ', ' . $zip . ' ' . $city . ', ' . $country;
$address = trim($address, ', '); // remove trailing commas and whitespaces

if ($address) {
$cacheObject = $this->initializeCache();

// create the cache key
$cacheKey = 'geocode-' . strtolower(str_replace(' ', '-', preg_replace('/[^0-9a-zA-Z ]/m', '', $address)));

// not in cache yet
if (false === $cacheObject->has($cacheKey)) {
$geocodingUrl = $this->geocodingUrl . '&address=' . urlencode($address);
$results = $this->getCoordinatesFromApi($geocodingUrl);
if ($results !== null) {
// Now store the $result in cache and return
$cacheObject->set($cacheKey, $results, [], $this->cacheTime);
}
} else {
$results = $cacheObject->get($cacheKey);
$addressParts = [];
foreach ([$street, $zip . ' ' . $city, $country] as $addressPart) {
if (empty($addressPart)) {
continue;
}
$addressParts[] = trim($addressPart);
}
Expand Down Expand Up @@ -178,6 +164,7 @@ protected function getCoordinatesFromApi($fullGeocodingUri) {
$apiResult = json_decode(GeneralUtility::getUrl($fullGeocodingUri), true);
if (count($apiResult['results']) > 0) {
$record = reset($apiResult['results']);
//\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($record);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove this line :)

$geometrics = $record['geometry'];
if(false === empty($geometrics['location']['lat'])) {
$results = [
Expand Down