--- ./sites/all/modules/geocode/modules/geocode_widget/geocode_widget.module	Tue Dec 22 20:50:30 2009
+++ ./sites/all/modules/geocode/modules/geocode_widget/geocode_widget.module	Wed Nov 03 13:22:26 2010
@@ -89,6 +89,43 @@
     
         if (is_array($form_state['values'][$field])) {
           foreach ($form_state['values'][$field] as $delta => $value) {
+            
+            $field_type = $form[$field][$delta]['#type'];            
+            
+            if ($field_type == 'addresses_elements') {
+              
+              // Filter the input from an addresses field
+              // In an addresses field, each country and province value is a key, not an actual readable country or province              
+              // The key format is such that google can usually decipher the address, but not always.
+              // For example:
+              //  - "Toronto,on,top_ca"       => Hilltop Rd, Toronto, ON M6C, Canada
+              //  - "Toronto,on,ca"           => Toronto, ON, Canada
+              //  - "TypoCity,on,ca"          => California, USA
+              //  - "TypoCity,Ontario,ca"     => Ontario, California, USA
+              //  - "TypoCity,Ontario,Canada" => Ontario, Canada
+              //  - "TypoCity,ON,Canada"      => error
+              // Hence, better to give the full names.
+              // Also, the $value array supplies the fields in reverse
+              
+              module_load_include('inc', 'addresses');
+              if (substr($value['country'], 0, 4) === 'top_') {
+	              $value['country'] = substr($value['country'], 4);
+	            }	                          
+              $clean = array();           
+	            $clean['street'] = $value['street'];
+	            $clean['city'] = $value['city'];
+	            $clean['province'] = theme_addresses_province($value);
+	            $clean['postal_code'] = $value['postal_code'];      
+	            $clean['country'] = theme_addresses_country($value);
+	            
+	            $value = array();
+	            foreach ($clean as $key_clean => $clean_value) {
+                if (! empty($clean_value)) {
+                  $value[$key_clean] = $clean_value;                  
+                }                
+              }
+            }
+            
             if ($geo = geocode($info['handler'], $value, $type, $info['options'])) {
               $values[$delta]['geo'] = $geo;
             }
