4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
13 namespace Civi\Api4\Action\Address
;
15 use Civi\Api4\Generic\Result
;
18 * Converts an address string to lat/long coordinates.
20 * @method $this setAddress(string $address)
21 * @method string getAddress()
23 class GetCoordinates
extends \Civi\Api4\Generic\AbstractAction
{
26 * Address string to convert to lat/long
33 public function _run(Result
$result) {
34 $geocodingClassName = \CRM_Utils_GeocodeProvider
::getUsableClassName();
35 $geocodingProvider = \CRM_Utils_GeocodeProvider
::getConfiguredProvider();
36 if (!is_callable([$geocodingProvider, 'getCoordinates'])) {
37 throw new \
API_Exception('Geocoding provider does not support getCoordinates');
39 $coord = $geocodingClassName::getCoordinates($this->address
);
40 if (isset($coord['geo_code_1'], $coord['geo_code_2'])) {
43 elseif (!empty($coord['geo_code_error'])) {
44 throw new \
API_Exception('Geocoding failed. ' . $coord['geo_code_error']);