diff --git a/geocoder.admin.inc b/geocoder.admin.inc index c4ca49b..68b2c0c 100644 --- a/geocoder.admin.inc +++ b/geocoder.admin.inc @@ -42,6 +42,14 @@ function geocoder_admin_settings($form, &$form_state) { '#size' => 10, ); + $form['geocoder_apikey_google'] = array( + '#type' => 'textfield', + '#title' => t('Google Maps API Key'), + '#description' => t('If your share an IP address with other developers (ie on Pantheon dev/staging boxes) then you\'ll need your own key, otherwise you will hit the 2500 limit quickly each day. If you are just running on a local box, or your own server, you can go ahead and leave this blank. You can obtain a Google Geocoding API Key at https://developers.google.com/maps/documentation/geocoding/#api_key'), + '#default_value' => empty($geocoder_settings['geocoder_apikey_google']) ? '' : $geocoder_settings['geocoder_apikey_google'], + '#required' => FALSE, + ); + $form['#submit'][] = 'geocoder_admin_settings_submit'; return system_settings_form($form); } @@ -57,5 +65,6 @@ function geocoder_admin_settings_submit($form, &$form_state) { $geocoder_settings['geocoder_apikey_yahoo'] = trim($form_state['values']['geocoder_apikey_yahoo']); $geocoder_settings['geocoder_apikey_yandex'] = trim($form_state['values']['geocoder_apikey_yandex']); $geocoder_settings['geocoder_apikey_bing'] = trim($form_state['values']['geocoder_apikey_bing']); + $geocoder_settings['geocoder_apikey_google'] = trim($form_state['values']['geocoder_apikey_google']); variable_set("geocoder_settings", $geocoder_settings); } diff --git a/plugins/geocoder_handler/google.inc b/plugins/geocoder_handler/google.inc index ea5a845..d3e4d74 100755 --- a/plugins/geocoder_handler/google.inc +++ b/plugins/geocoder_handler/google.inc @@ -32,12 +32,28 @@ function geocoder_google($address, $options = array()) { geophp_load(); - $query = array( - 'address' => $address, - 'sensor' => 'false', - ); + $geocoder_settings = variable_get("geocoder_settings", array()); + + if (!empty($geocoder_settings['geocoder_apikey_google'])) { + $query = array( + 'address' => $address, + 'sensor' => 'false', + 'key' => $geocoder_settings['geocoder_apikey_google'], + ); + + // With a key must be https + $url = url("https://maps.googleapis.com/maps/api/geocode/json", array('query' => $query)); + + } + else { + $query = array( + 'address' => $address, + 'sensor' => 'false', + ); + + $url = url("http://maps.googleapis.com/maps/api/geocode/json", array('query' => $query)); + } - $url = url("http://maps.googleapis.com/maps/api/geocode/json", array('query' => $query)); $result = drupal_http_request($url); $delay_trigger = TRUE;