You are here

function device_geolocation_init in Smart IP 6.2

Same name and namespace in other branches
  1. 6 modules/device_geolocation/device_geolocation.module \device_geolocation_init()
  2. 7.2 modules/device_geolocation/device_geolocation.module \device_geolocation_init()
  3. 7 modules/device_geolocation/device_geolocation.module \device_geolocation_init()

Implements hook_init().

File

modules/device_geolocation/device_geolocation.module, line 13
Provides visitor's geographical location using client device location source that implements W3C Geolocation API and Google Geocoding service.

Code

function device_geolocation_init() {

  // Check to see if the page is one of those allowed for geolocation
  if (!smart_ip_check_allowed_page()) {

    // This page is not on the list to acquire/update user's geolocation
    return;
  }
  $ajax_check = variable_get('device_geolocation_ajax_check', FALSE);
  if ($ajax_check || device_geolocation_check_geolocation_attempt()) {
    device_geolocation_get_coordinates();
    if ($ajax_check) {

      // Add javascript app that checks if device geolocation needs refresh via ajax call
      drupal_add_js(array(
        'device_geolocation' => array(
          'ask_geolocate' => FALSE,
        ),
      ), 'setting');
      drupal_add_js(drupal_get_path('module', 'device_geolocation') . '/js/device_geolocation_check.js', 'module', 'footer');
    }
    else {
      drupal_add_js(array(
        'device_geolocation' => array(
          'ask_geolocate' => TRUE,
        ),
      ), 'setting');
    }
    $param = '';
    $google_map_api = variable_get('device_geolocation_google_map_api', NULL);
    $google_region = variable_get('device_geolocation_google_map_region', NULL);
    $google_map_language = variable_get('device_geolocation_google_map_language', NULL);
    if ($google_map_api) {
      $param .= "key={$google_map_api}";
    }
    if ($google_region) {
      $param .= "&region={$google_region}";
    }
    if ($google_map_language) {
      $param .= "&language={$google_map_language}";
    }
    drupal_set_html_head('<script type="text/javascript" src="//maps.google.com/maps/api/js?key=' . $param . '"></script>');
    drupal_add_js(drupal_get_path('module', 'device_geolocation') . '/js/device_geolocation.js', 'module', 'footer');
  }
}