You are here

function geocoder_autocomplete_autocomplete in Geocoder autocomplete 7

Menu callback.

1 string reference to 'geocoder_autocomplete_autocomplete'
geocoder_autocomplete_menu in ./geocoder_autocomplete.module
Implements hook_menu().

File

./geocoder_autocomplete.module, line 34
@author Stefan Auditor <stefan.auditor@erdfisch.de>

Code

function geocoder_autocomplete_autocomplete($string = '') {
  $matches = array();
  if ($string !== '') {
    $query = array(
      'address' => $string,
      'language' => $GLOBALS['language']->language,
      'sensor' => 'false',
    );
    $url = 'http://maps.googleapis.com/maps/api/geocode/json?' . drupal_http_build_query($query);
    $response = drupal_http_request($url);
    if (empty($response->error)) {
      $data = json_decode($response->data);
      if ($data->status == 'OK') {
        foreach ($data->results as $result) {
          if (!empty($result->formatted_address)) {
            $matches[$result->formatted_address] = '<div class="reference-autocomplete">' . check_plain($result->formatted_address) . '</div>';
          }
        }
      }
    }
  }
  drupal_json_output($matches);
}