We started running into denials from the Google Geocoding JSON Service. It turns out that the google geocoder in the location module is not using an API key for requests even when one is provided. I will attach a patch that resolved the issue for us and got geocoding going again with an API key.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

onelittleant’s picture

onelittleant’s picture

Issue summary: View changes
Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 1: location-googlegeocoderapikey-2219543-1.patch, failed testing.

JKingsnorth’s picture

I can confirm this bug. Even though the module grabs our API key from the GMAP module, it doesn't actually USE it in the request!

JKingsnorth’s picture

I can confirm that the patch in #1 does work, but I'm not sure it is the right approach.

Surely the API key should be set into the variable location_geocode_google_apikey regardless of whether it comes from GMAP or is set in the location module. It looks like the admin settings form options is supposed to do this:

function google_geocode_settings() {
  $form = array();
  $key = '';
  if (function_exists('gmap_get_key')) {
    $key = gmap_get_key();
  }

  if (!empty($key)) {
    $form['location_geocode_google_apikey'] = array(
      '#type' => 'item',
      '#title' => t('Google Maps API Key'),
      '#value' => $key,
      '#description' => t('The key in use was automatically provided by GMap.'),
    );
  }
  else {
    $form['location_geocode_google_apikey'] = array(
      '#type' => 'textfield',
      '#title' => t('Google Maps API Key'),
      '#size' => 64,
      '#maxlength' => 128,
      '#default_value' => variable_get('location_geocode_google_apikey', ''),
      '#description' => t('In order to use the Google Maps API geocoding web-service, you will need a Google Maps API Key.  You can obtain one at the !sign_up_link for the !google_maps_api.  PLEASE NOTE: You will <em>not</em> have to re-enter your API key for each country for which you have selected Google Maps for geocoding.  This setting is global.', array(
        '!sign_up_link' => '<a href="http://www.google.com/apis/maps/signup.html">sign-up page</a>',
        '!google_maps_api' => '<a href="http://www.google.com/apis/maps/">Google Maps API</a>'
      ))
    );
  }
...

But it doesn't work. My API key is set via GMAP, and the location_geocode_google_apikey variable is empty. It is not empty if there is an API key set on the location module though, ie when the second form is active. But obviously the first part is not working properly. I think we should try to fix that and kill two birds with one stone.

JKingsnorth’s picture

OK, so that first bit of the form, that is suppoed to detect the GMAP API key and set it to the variable is doing nothing useful:

 $form = array();
  $key = '';
  if (function_exists('gmap_get_key')) {
    $key = gmap_get_key();
  }
  if (!empty($key)) {
    $form['location_geocode_google_apikey'] = array(
      '#type' => 'item',
      '#title' => t('Google Maps API Key'),
      '#value' => $key,
      '#description' => t('The key in use was automatically provided by GMap.'),
    );
  }

Issue 1 - The 'item' type can't have a value, so nothing is being set here.
Issue 2 - Even if we change this to a 'textfield' with the value from the GMAP module's API key, the value isn't set to the location_geocode_google_apikey variable until an admin goes to one of these configuration pages (Location > Geocoding options > Next to a country - Configure parameters) and hits 'Save configuration'.

So we need to somehow get this variable to be set by default if the GMAP module's API key is set? I guess this would have to hook in to the install process, and hook in to the form in the GMAP module where the API key is saved? Or we abandon trying to use the GMAP module's API key altogether, and just set it again in Location.

onelittleant’s picture

With Google Maps API 3.x an API key is not needed. Also, the API key needed for geocoding should now be different than any browser api key. It should be a server key generated through the Google developers console. This is tied to limits and billing.

I would think the best approach to preserve backward compatibility would be to always show the API key field on the Location settings and in the code use the gmap API key if nothing is set in the Location geocoder API key.

onelittleant’s picture

Status: Needs work » Needs review
FileSize
3.57 KB

This patch includes some updates based on suggestion from John https://drupal.org/comment/8611387#comment-8611387 and an API key for test bot.

podarok’s picture

Status: Needs review » Fixed

#8 commited
thanks!

  • Commit 7c636d2 on 7.x-3.x authored by onelittleant, committed by podarok:
    Issue #2219543 by onelittleant: API key not being used in geocoding...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

gpvdo’s picture

It sounds like location is getting the API key from 'gmap' module.

Since Location module will do geo-coding without the 'gmap' module, should it not have its own entry for the Google Maps API?