3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2016
35 * Class that uses google geocoder
37 class CRM_Utils_Geocode_Google
{
40 * Server to retrieve the lat/long
44 static protected $_server = 'maps.googleapis.com';
51 static protected $_uri = '/maps/api/geocode/xml?sensor=false&address=';
54 * Function that takes an address object and gets the latitude / longitude for this
55 * address. Note that at a later stage, we could make this function also clean up
56 * the address into a more valid format
58 * @param array $values
59 * @param bool $stateName
62 * true if we modified the address, false otherwise
64 public static function format(&$values, $stateName = FALSE) {
65 // we need a valid country, else we ignore
66 if (empty($values['country'])) {
70 $config = CRM_Core_Config
::singleton();
74 if (!empty($values['street_address'])) {
75 $add = urlencode(str_replace('', '+', $values['street_address']));
79 $city = CRM_Utils_Array
::value('city', $values);
81 $add .= '+' . urlencode(str_replace('', '+', $city));
85 if (!empty($values['state_province'])) {
86 if (!empty($values['state_province_id'])) {
87 $stateProvince = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_StateProvince', $values['state_province_id']);
91 $stateProvince = CRM_Core_DAO
::getFieldValue('CRM_Core_DAO_StateProvince',
92 $values['state_province'],
98 $stateProvince = $values['state_province'];
102 // dont add state twice if replicated in city (happens in NZ and other countries, CRM-2632)
103 if ($stateProvince != $city) {
104 $add .= '+' . urlencode(str_replace('', '+', $stateProvince));
109 if (!empty($values['postal_code'])) {
110 $add .= '+' . urlencode(str_replace('', '+', $values['postal_code']));
114 if (!empty($values['country'])) {
115 $add .= '+' . urlencode(str_replace('', '+', $values['country']));
118 $query = 'http://' . self
::$_server . self
::$_uri . $add;
120 require_once 'HTTP/Request.php';
121 $request = new HTTP_Request($query);
122 $request->sendRequest();
123 $string = $request->getResponseBody();
125 libxml_use_internal_errors(TRUE);
126 $xml = @simplexml_load_string
($string);
127 CRM_Utils_Hook
::geocoderFormat('Google', $values, $xml);
128 if ($xml === FALSE) {
129 // account blocked maybe?
130 CRM_Core_Error
::debug_var('Geocoding failed. Message from Google:', $string);
134 if (isset($xml->status
)) {
135 if ($xml->status
== 'OK' &&
136 is_a($xml->result
->geometry
->location
,
140 $ret = $xml->result
->geometry
->location
->children();
141 if ($ret->lat
&& $ret->lng
) {
142 $values['geo_code_1'] = (float) $ret->lat
;
143 $values['geo_code_2'] = (float) $ret->lng
;
147 elseif ($xml->status
== 'OVER_QUERY_LIMIT') {
148 CRM_Core_Error
::debug_var('Geocoding failed. Message from Google: ', (string ) $xml->status
);
149 $values['geo_code_1'] = $values['geo_code_2'] = 'null';
150 $values['geo_code_error'] = $xml->status
;
155 // reset the geo code values if we did not get any good values
156 $values['geo_code_1'] = $values['geo_code_2'] = 'null';