From e66fe2847b013960be0510df07886729b279cc3d Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 29 Jan 2018 12:31:40 +0100 Subject: [PATCH 1/3] Fixes fatal error "unkown column distance in order_by" --- Classes/Service/RadiusService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Classes/Service/RadiusService.php b/Classes/Service/RadiusService.php index b3ab577..e830e3c 100644 --- a/Classes/Service/RadiusService.php +++ b/Classes/Service/RadiusService.php @@ -89,7 +89,7 @@ public function findAllDatabaseRecordsInRadius($coordinates, $maxDistance = 250, return $queryBuilder ->select(...$fields) ->addSelectLiteral( - $distanceSqlCalc + $distanceSqlCalc . ' AS `distance`' ) ->from($tableName) ->where( From b5ded9e917b3062ee35a84ab2a333056c7f430cc Mon Sep 17 00:00:00 2001 From: Paul Date: Mon, 29 Jan 2018 12:49:16 +0100 Subject: [PATCH 2/3] [FEATURE] Add backwards search by Google PlaceID Fixes 5,4 --- Classes/Service/GeoService.php | 83 ++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/Classes/Service/GeoService.php b/Classes/Service/GeoService.php index cf6a15f..a37e4ff 100644 --- a/Classes/Service/GeoService.php +++ b/Classes/Service/GeoService.php @@ -43,14 +43,13 @@ class GeoService /** * base URL to fetch the Coordinates (Latitude, Longitutde of a Address String. + * TODO: Language shall be configurable */ - protected $geocodingUrl = 'http://maps.googleapis.com/maps/api/geocode/json?language=de&sensor=false'; + 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 */ public function __construct($apikey = null) @@ -61,7 +60,7 @@ public function __construct($apikey = null) $apikey = $geoCodingConfig['googleApiKey']; } $this->apikey = $apikey; - //$this->geocodingUrl .= '&key=' . $apikey; + $this->geocodingUrl .= '&key=' . $apikey; } /** @@ -73,7 +72,7 @@ public function __construct($apikey = null) * @param $city * @param $country * - * @return array an array with accuracy, latitude and longitude + * @return array|null an array with long name, latitude and longitude or null if nothing was found */ public function getCoordinatesForAddress($street = null, $zip = null, $city = null, $country = 'Germany') { @@ -89,26 +88,11 @@ public function getCoordinatesForAddress($street = null, $zip = null, $city = nu $cacheKey = 'geocode-' . strtolower(str_replace(' ', '-', preg_replace('/[^0-9a-zA-Z ]/m', '', $address))); // not in cache yet - if (!$cacheObject->has($cacheKey)) { + if (false === $cacheObject->has($cacheKey)) { $geocodingUrl = $this->geocodingUrl . '&address=' . urlencode($address); - $results = GeneralUtility::getUrl($geocodingUrl); - $results = json_decode($results, true); - - $latitude = 0; - if (count($results['results']) > 0) { - $record = reset($results['results']); - $geometrics = $record['geometry']; - - $latitude = $geometrics['location']['lat']; - $longitude = $geometrics['location']['lng']; - } - - if ($latitude != 0) { - $results = [ - 'latitude' => $latitude, - 'longitude' => $longitude, - ]; - // Now store the $result in cache and return + $results = $this->getCoordinatesFromApi($geocodingUrl); + if ($results !== null) { + // Now store the $result in cache and return $cacheObject->set($cacheKey, $results, [], $this->cacheTime); } } else { @@ -119,6 +103,57 @@ public function getCoordinatesForAddress($street = null, $zip = null, $city = nu return $results; } + /** + * This function will get coordinates by a given place_id of google + * @param string $placeId The Google place_id of the location + * @return array|null An array holding long name, lat and long for the given placeId or null if nothing was found + */ + public function getCoordinatesFromPlaceId($placeId) { + $results = null; + + $cacheObject = $this->initializeCache(); + + // create the cache key + $cacheKey = 'geocodeplace-' . $placeId; + + // not in cache yet + if (false === $cacheObject->has($cacheKey)) { + $geocodingUrl = $this->geocodingUrl . '&place_id=' . urlencode($placeId); + $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); + } + + return $results; + + } + + /** + * This function will try to fetch coordinates from Google and return null or an array with the coordinates + * @param string $fullGeocodingUri The complete Geocoding URL for the API request + * @return array|null long name, latitute and longitude or null if nothing was found + */ + protected function getCoordinatesFromApi($fullGeocodingUri) { + $results = null; + $apiResult = json_decode(GeneralUtility::getUrl($fullGeocodingUri), true); + if (count($apiResult['results']) > 0) { + $record = reset($apiResult['results']); + $geometrics = $record['geometry']; + if(false === empty($geometrics['location']['lat'])) { + $results = [ + 'long_name' => $record['address_components'][0]['long_name'], + 'latitude' => $geometrics['location']['lat'], + 'longitude' => $geometrics['location']['lng'], + ]; + } + } + return $results; + } + /** * geocodes all missing records in a DB table and then stores the values * in the DB record. From d5aff5bc959d60ca41c0db4aef4923bb44ec2a44 Mon Sep 17 00:00:00 2001 From: Paul Beck Date: Sat, 12 Jan 2019 13:20:43 +0100 Subject: [PATCH 3/3] Removed Debug --- Classes/Service/GeoService.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Classes/Service/GeoService.php b/Classes/Service/GeoService.php index a37e4ff..16d55de 100644 --- a/Classes/Service/GeoService.php +++ b/Classes/Service/GeoService.php @@ -142,6 +142,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); $geometrics = $record['geometry']; if(false === empty($geometrics['location']['lat'])) { $results = [