You are here

function ip_geoloc_get_current_location in IP Geolocation Views & Maps 8

Same name and namespace in other branches
  1. 7 ip_geoloc_api.inc \ip_geoloc_get_current_location()

Uses AJAX to return in $_POST info about the visitor's current location.

Note: this function will result in the browser prompting the visitor to share their location, which they may or may not accept.

This works via an asynchronous javascript call, so the result is not immediately available on return from this function, hence the $menu_callback. Upon page load the included javascript will, when ready, instigate an AJAX call to the $menu_callback, which should invoke a function to pull the lat/long and address values out of the $_POST variable. See ip_geoloc_current_location_ajax_recipient() for an example.

Note: will result in a HTTP error 503 when the site is in maintenance mode, as in maintenance mode menu items are not available.

File

./ip_geoloc_api.inc, line 363
API functions of IP geolocation module

Code

function ip_geoloc_get_current_location($menu_callback = NULL, $reverse_geocode = NULL, $refresh_page = NULL) {
  $throbber_text = \Drupal::state()
    ->get('ip_geoloc_throbber_text2');
  if (empty($throbber_text)) {
    $throbber_text = IP_GEOLOC_THROBBER_DEFAULT_TEXT;
  }
  if ($throbber_text != '<none>') {
    drupal_set_message(filter_xss_admin($throbber_text));
  }
  _ip_geoloc_set_session_value('last_position_check', time());
  _ip_geoloc_set_session_value('position_pending_since', microtime(TRUE));
  drupal_add_js(IP_GEOLOC_GOOGLE_MAPS, array(
    'type' => 'external',
    'group' => JS_LIBRARY,
  ));
  drupal_add_js(drupal_get_path('module', 'ip_geoloc') . '/js/ip_geoloc_current_location.js');
  if (!isset($menu_callback)) {
    global $base_url;
    $menu_callback = "{$base_url}/" . (\Drupal::state()
      ->get('clean_url', 0) ? 'js/ip_geoloc/current_location' : '?q=js/ip_geoloc/current_location');
  }
  if (!isset($reverse_geocode)) {
    $reverse_geocode = \Drupal::state()
      ->get('ip_geoloc_visitor_reverse_geocode', TRUE);
  }
  if (!isset($refresh_page)) {
    $refresh_page = \Drupal::state()
      ->get('ip_geoloc_page_refresh', TRUE) && !path_is_admin($_GET['q']);
  }
  $settings = array(
    'ip_geoloc_menu_callback' => $menu_callback,
    'ip_geoloc_refresh_page' => $refresh_page,
    'ip_geoloc_reverse_geocode' => $reverse_geocode,
  );
  drupal_add_js($settings, 'setting');
}