Skip to content

Commit

Permalink
Optional Integration of Google API for Geo Coding. (#2)
Browse files Browse the repository at this point in the history
* [TASK] You can now use the Google API to get Geo Locations from an address.

* [FIX] Readded getResultData method.
  • Loading branch information
samsauter authored and bweinzierl committed Sep 6, 2019
1 parent 0237b0a commit d28194a
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 15 deletions.
90 changes: 77 additions & 13 deletions Classes/Service/GeoIndexService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Neos\Flow\Annotations as Flow;
use Neos\Neos\Exception;

/**
* Service for indexing geo-data
Expand Down Expand Up @@ -40,6 +41,16 @@ class GeoIndexService {
*/
protected $geonamesUsername;

/**
* @var string
*/
protected $googleBaseUri;

/**
* @var string
*/
protected $googleApiKey;

/**
* @Flow\Inject
* @var \Neos\Flow\Http\Client\Browser
Expand Down Expand Up @@ -71,12 +82,35 @@ public function initializeObject() {
$this->geonamesEnable = $conf['geonamesEnable'];
$this->geonamesBaseUri = $conf['geonamesBaseUri'];
$this->geonamesUsername = $conf['geonamesUsername'];
$this->googleBaseUri = $conf['googleBaseUri'];
$this->googleApiKey = $conf['googleApiKey'];
}

/**
* @param string $address
* @param $address
* @param string $serviceToUse
* @return bool
* @throws Exception
* @throws \Neos\Flow\Http\Client\InfiniteRedirectionException
*/
public function indexAddress($address, $serviceToUse = 'nominatim') {
switch ($serviceToUse) {
case 'google':
$indexedAddress = $this->indexWithGoogle($address);
break;
default:
$indexedAddress = $this->indexWithNomination($address);
}
return $indexedAddress;
}

/**
* @param $address
* @return bool
* @throws \Neos\Flow\Http\Client\InfiniteRedirectionException
*/
public function indexAddress($address) {
public function indexWithNomination($address)
{
$uri = $this->nominatimBaseUri . 'search?format=json&addressdetails=1&accept-language=en&q=' . urlencode($address);
$geoData = json_decode($this->sendRequest($uri));
if ($geoData && array_key_exists(0, $geoData)) {
Expand All @@ -91,6 +125,28 @@ public function indexAddress($address) {
return FALSE;
}

/**
* @param $address
* @return bool
* @throws Exception
* @throws \Neos\Flow\Http\Client\InfiniteRedirectionException
*/
public function indexWithGoogle($address)
{
if(!$this->googleApiKey){
throw new Exception('Please specify your Google Api Key!', 1567771297);
}
$formattedAddr = str_replace(' ','+', $address);
$uri = $this->googleBaseUri . 'geocode/json?address='.$formattedAddr.'&key='.$this->googleApiKey;
$geoData = json_decode($this->browser->request($uri)->getContent());

if ($geoData && array_key_exists(0, $geoData->results)) {
$this->resultData = $geoData->results[0]->geometry;
return TRUE;
}
return FALSE;
}

/**
* @param $uri
* @return string
Expand All @@ -105,6 +161,12 @@ protected function sendRequest($uri) {
* @param $object
*/
public function setLocationDataOnObject($object) {
if (isset($this->resultData->location->lng)) {
$object->setLocationLatitude($this->resultData->location->lat);
$object->setLocationLongitude($this->resultData->location->lng);
}

// Nominatim
if (isset($this->resultData->lon)) {
$object->setLocationLatitude($this->resultData->lat);
$object->setLocationLongitude($this->resultData->lon);
Expand All @@ -128,7 +190,9 @@ public function setLocationDataOnObject($object) {
* @return float
*/
public function getLongitude() {
if ($this->resultData->lon) {
if (isset($this->resultData->location->lng)) {
return $this->resultData->location->lng;
} elseif (isset($this->resultData->lon)) {
return $this->resultData->lon;
}
return NULL;
Expand All @@ -138,18 +202,18 @@ public function getLongitude() {
* @return float
*/
public function getLatitude() {
if ($this->resultData->lat) {
if (isset($this->resultData->location->lat)) {
return $this->resultData->location->lat;
} elseif (isset($this->resultData->lat)) {
return $this->resultData->lat;
}
return NULL;
}

/**
* @return array
*/
public function getResultData(){
return $this->resultData;
}

}
?>
/**
* @return array
*/
public function getResultData(){
return $this->resultData;
}
}
4 changes: 3 additions & 1 deletion Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ FormatD:
nominatimBaseUri: 'http://nominatim.openstreetmap.org/'
geonamesEnable: true
geonamesBaseUri: 'http://api.geonames.org/'
geonamesUsername: ''
geonamesUsername: ''
googleBaseUri: 'https://maps.googleapis.com/maps/api/'
googleApiKey: ''
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

# FormatD.GeoIndexable

Service for fetching geo-information for addresses in Neos Flow projects (using nominatim/openstreatmap and geonames api)
Service for fetching geo-information for addresses in Neos Flow projects. By default, it uses the nominatim/openstreatmap and geonames API,
but you can use Google API, too.


## What does it do?
Expand All @@ -20,4 +21,12 @@ FormatD:
geoIndexService:
geonamesEnable: true
geonamesUsername: ''
```

In case you want to use the Google API, you have to provide your API Key
```
FormatD:
GeoIndexable:
geoIndexService:
googleApiKey: ''
```

0 comments on commit d28194a

Please sign in to comment.