// include gmaps2 lib
require_once JPATH_SITE . '/plugins/fabrik_cron/geocode/libs/gmaps2.php';
// set the form prefix (leave blank for tableless form)
$formPrefix = '';
// get the API key and verify peer workaround from Fabrik config
$config = JComponentHelper::getParams('com_fabrik');
$apiKey = $config->get('google_api_key', '');
$verifyPeer = (bool) $config->get('verify_peer', '1');
// get the GeoCode instance
$gmap = new GeoCode($verifyPeer);
// get the address
$fullAddr = array();
$fullAddr[] = $formModel->formData[$formPrefix . '___street_address'];
$fullAddr[] = $formModel->formData[$formPrefix . '___city'];
$fullAddr[] = $formModel->formData[$formPrefix . '___state'];
$fullAddr[] = $$formModel->formData[$formPrefix . '___zip'];
// create the flat address string, and fire off the request
$fullAddr = implode(',', array_filter($fullAddr));
$fullAddr = urlencode(html_entity_decode($fullAddr, ENT_QUOTES));
$res = $gmap->getLatLng($fullAddr, 'array', $apiKey);
// check the response
if ($res['status'] == 'OK')
{
// we got a response, so update the lat and lon element data
$formModel->updateFormData($formPrefix . '___lat', $res['lat'], true, true);
$formModel->updateFormData($formPrefix . '___lon', $res['lng'], true, true);
return true;
}
else
{
// meh, no response, so fail the submission (fake out a validation error)
$formModel->errors['foobar'] = array(FText::_('Unable to geocode address!'));
$formModel->formErrorMsg = FText::_('We were unable to find your address, please check and try again.');
return false;
}