$values['geo_code_error'] = $coord['geo_code_error'];
}
+ CRM_Utils_Hook::geocoderFormat('Google', $values, $coord['request_xml']);
+
return isset($coord['geo_code_1'], $coord['geo_code_2']);
}
/**
* @param string $add
* Url-encoded address
+ *
* @return array
+ * An array of values with the following possible keys:
+ * geo_code_error: String error message
+ * geo_code_1: Float latitude
+ * geo_code_2: Float longitude
+ * request_xml: SimpleXMLElement parsed xml from geocoding API
+ *
* @throws \GuzzleHttp\Exception\GuzzleException
*/
private static function makeRequest($add) {
-
+ $coords = [];
$config = CRM_Core_Config::singleton();
if (!empty($config->geoAPIKey)) {
$add .= '&key=' . urlencode($config->geoAPIKey);
libxml_use_internal_errors(TRUE);
$xml = @simplexml_load_string($string);
- CRM_Utils_Hook::geocoderFormat('Google', $values, $xml);
+ $coords['request_xml'] = $xml;
if ($xml === FALSE) {
// account blocked maybe?
CRM_Core_Error::debug_var('Geocoding failed. Message from Google:', $string);
- return ['geo_code_error' => $string];
+ $coords['geo_code_error'] = $string;
}
if (isset($xml->status)) {
) {
$ret = $xml->result->geometry->location->children();
if ($ret->lat && $ret->lng) {
- return [
- 'geo_code_1' => (float) $ret->lat,
- 'geo_code_2' => (float) $ret->lng,
- ];
+ $coords['geo_code_1'] = (float) $ret->lat;
+ $coords['geo_code_2'] = (float) $ret->lng;
}
}
- elseif ($xml->status == 'ZERO_RESULTS') {
- // reset the geo code values if we did not get any good values
- return [];
- }
- else {
+ elseif ($xml->status != 'ZERO_RESULTS') {
+ // 'ZERO_RESULTS' is a valid status, in which case we'll change nothing in $ret;
+ // but if the status is anything else, we need to note the error.
CRM_Core_Error::debug_var("Geocoding failed. Message from Google: ({$xml->status})", (string ) $xml->error_message);
- return ['geo_code_error' => $xml->status];
+ $coords['geo_code_error'] = $xml->status;
}
}
- return [];
+ return $coords;
}
}