47 lines
1.4 KiB
PHP
47 lines
1.4 KiB
PHP
<?php
|
|
|
|
class GeocodingController extends mfBaseController {
|
|
|
|
protected function init() {
|
|
$this->needlogin = true;
|
|
$me = new User();
|
|
$me->loadMe();
|
|
$this->me = $me;
|
|
}
|
|
|
|
protected function autocompleteAction() {
|
|
$search = urlencode($this->request->q);
|
|
$url = TT_GEOCODING_API_URL . "?address={$search}&key=" . TT_GEOCODING_API_SECRET . "®ion=at&language=de&components=country:AT";
|
|
|
|
if (!$response = @file_get_contents($url)) {
|
|
self::returnJson([]);
|
|
return;
|
|
}
|
|
|
|
$data = json_decode($response, true);
|
|
if ($data['status'] !== 'OK' || empty($data['results'])) {
|
|
self::returnJson([]);
|
|
return;
|
|
}
|
|
|
|
$out = [];
|
|
foreach ($data['results'] as $entry) {
|
|
$hasHouseNumber = false;
|
|
foreach ($entry['address_components'] as $component) {
|
|
if (in_array('street_number', $component['types'])) {
|
|
$hasHouseNumber = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
$text = $entry['formatted_address'];
|
|
$value = "{$entry['geometry']['location']['lat']},{$entry['geometry']['location']['lng']}" . ($hasHouseNumber ? '' : ', area');
|
|
|
|
$out[$text] = ['value' => $value, 'text' => $text];
|
|
}
|
|
|
|
self::returnJson(array_values($out));
|
|
}
|
|
|
|
|
|
} |