There was a post regarding this topic here: http://www.gravityhelp.com/forums/topic/saving-latlong-data-from-a-form, but it has been closed.
Any idea what's wrong with this code? The values are being pulled from two custom fields, not a Gravity Forms address field. The fields are ' input_10' and 'input_11'. I've tried using just the numbers as well as the preceding 'input_' to no avail.
The custom fields are 'latitude' and 'longitude'
Any help is appreciated.
// Geocode New Development Additions
add_action("gform_after_submission_2", "pre_submission_handler");
function pre_submission_handler($entry){
// update the "1.x" to the ID of your address field
$query_string = $entry['10'] . "+" . $entry['11'];
$coords = getLatLong($query_string);
update_post_meta($entry['post_id'], 'lat', $coords['latitude']);
update_post_meta($entry['post_id'], 'long', $coords['longitude']);
}
// function to take address and turn it into coordinates lat and long
function getLatLong($code){
$mapsApiKey = '<removed>';
$query = "http://maps.google.com/maps/geo?q=".urlencode($code)."&output=json&key=".$mapsApiKey;
// This takes your full street address to create the lat/long
$data = file_get_contents($query);
// if data returned
if($data){
// convert into readable format
$data = json_decode($data);
$long = $data->Placemark[0]->Point->coordinates[0];
$lat = $data->Placemark[0]->Point->coordinates[1];
return array('Latitude'=>$lat,'Longitude'=>$long);
} else {
return false;
}
}