You are here

function ip_geoloc_build_google_api_url in IP Geolocation Views & Maps 8

Same name and namespace in other branches
  1. 7 ip_geoloc.module \ip_geoloc_build_google_api_url()

Builds the javascript maps api url based on authentication method.

Patch from https://www.drupal.org/node/2776209

1 call to ip_geoloc_build_google_api_url()
IpGeoLocAPI.php in src/Services/IpGeoLocAPI.php

File

./ip_geoloc.module, line 910
IPGV&M is a mapping engine for Views that contain locations of entities and/or visitors. Google Maps, Leaflet and OpenLayers2 maps are all supported and available through this module. Using a number of optional sources IPGV&M also retrieves…

Code

function ip_geoloc_build_google_api_url() {
  $google_api_url = 'https://maps.googleapis.com/maps/api/js';

  // Append query parameters for the Google Maps url.
  // See https://developers.google.com/maps/documentation/javascript/versions
  $query = [];
  $config = \Drupal::config('ip_geoloc.settings');
  $auth_method = $config
    ->get('ip_geoloc_auth_method') ? $config
    ->get('ip_geoloc_auth_method') : 0;
  switch ($auth_method) {
    case 1:
      $key = $config
        ->get('ip_geoloc_apikey') ? $config
        ->get('ip_geoloc_apikey') : '';
      $key = trim($key);
      if (!empty($key)) {
        $query['key'] = $key;
      }
      break;
    case 2:
      $client_id = $config
        ->get('ip_geoloc_client_id') ? $config
        ->get('ip_geoloc_client_id') : '';
      $client_id = trim($client_id);
      if (!empty($client_id)) {
        $query['client'] = $client_id;
        $client_id = $config
          ->get('ip_geoloc_signature') ? $config
          ->get('ip_geoloc_signature') : '';
        $signature = trim($client_id);
        if (!empty($signature)) {
          $query['signature'] = $signature;
        }
      }
      break;
    default:
  }

  // Add query params to API url.
  if (!empty($query)) {

    // Include version number as it's required for Premium plans.
    $query['v'] = '3.26';
    $google_api_url .= '?' . UrlHelper::buildQuery($query);
  }

  // print_r($google_api_url);die();
  return $google_api_url;
}