New hook_device_geolocation_track_geo() | Custom logic for deciding whether or not to request users their location

Created on 20 June 2013, over 11 years ago
Updated 28 November 2024, 24 days ago

USE CASES

I need to get the user location as accurate as possible when they come from, say for instance, Brazil.

So, there is no need to bother people from other countries by asking them permission to access their browser geo location.

There are other case uses. Take FireFox for instance, depending on its user settings, it keeps on asking permission on every page load even when the user had previously responded it. So you can minimize this by doing a custom check.

Here is a sample of how to implement this new hook.

/**
 * Implements hook_device_geolocation_track_geo().
 */
function MYMODULE_device_geolocation_track_geo(&$track_geo) {
  $user_allowed = !is_null(smart_ip_session_get('device_geolocation'));

  $smart_ip_session = smart_ip_session_get('smart_ip');
  $country_code = $smart_ip_session['location']['country_code'];

  switch($country_code) {
    case 'BR':
    case '':
      $track_geo = TRUE;
      // This is needed because FireFox, depending on User settings, keeps on
      // asking permission on every single page load, and that is annoying.
      if ($user_allowed) {
        // No need to ask again.
        $track_geo = FALSE;
      }
    break;
    default:
      // Everybody else is spared from being asked permission.
      $track_geo = FALSE;
    break;
  }
}
Feature request
Status

Closed: outdated

Version

2.0

Component

Code

Created by

🇧🇷Brazil FranciscoLuz

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • heddn Nicaragua

    Drupal 7 is no longer supported in a month. Closing old issues. If this is still something you want addressed in the current 5.x version of the module, please re-open and provide some next steps.

Production build 0.71.5 2024