You are here

function device_geolocation_get_coordinates in Smart IP 6

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

Get Visitor's coordinates.

2 calls to device_geolocation_get_coordinates()
device_geolocation_init in modules/device_geolocation/device_geolocation.module
Implements hook_init().
smart_ip_admin_settings_submit in includes/smart_ip.admin.inc
Process Forms submitted by IP to Country administration page

File

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

Code

function device_geolocation_get_coordinates() {
  global $user;
  $latitude = 0;
  $longitude = 0;
  $user_data = unserialize($user->data);
  $smart_ip_session = smart_ip_session_get('smart_ip');

  // Smart IP user $_SESSION
  if (!is_null($smart_ip_session) && !empty($smart_ip_session['location']['latitude'])) {
    $latitude = $smart_ip_session['location']['latitude'];
    $longitude = $smart_ip_session['location']['longitude'];
  }
  elseif (isset($user_data['geoip_location']) && !empty($user_data['geoip_location']['latitude'])) {
    $smart_ip_session['location'] = $user_data['geoip_location'];
    $latitude = $smart_ip_session['location']['latitude'];
    $longitude = $smart_ip_session['location']['longitude'];
    smart_ip_session_set('smart_ip', $smart_ip_session);
  }
  else {
    $smart_ip_session['location'] = smart_ip_get_current_visitor_location_data();
    smart_ip_session_set('smart_ip', $smart_ip_session);
    if ($user->uid) {
      $user_obj = user_load($user->uid);
      user_save($user_obj, array(
        'geoip_location' => $smart_ip_session['location'],
      ));
    }
    if (!empty($smart_ip_session['location']['latitude'])) {
      $latitude = $smart_ip_session['location']['latitude'];
      $longitude = $smart_ip_session['location']['longitude'];
    }
    else {
      smart_ip_session_set('smart_ip', NULL);
    }
  }
  $coordinates = array(
    'latitude' => $latitude,
    'longitude' => $longitude,
    'debug_mode' => isset($smart_ip_session['debug_mode']) ? TRUE : FALSE,
  );

  // Send also to javascript
  drupal_add_js(array(
    'device_geolocation' => $coordinates,
  ), 'setting');
  return $coordinates;
}