Ask your WordPress questions! Pay money and get answers fast! (more info)

Geo Mashup and GravityForms

I have several gravityforms on our site with the following fields:

Address
City
State

I need to use these custom fields, geocode those submitted addresses and then out put a global map using geo mashup to demonstrate the submitted addresses.

need directions on how to proceed.

Answers (3)

2014-11-01

Kyle answers:

This should get you started, you probably will need to raise the pot for a more hands-on answer


function getLatLong($query_string){

$query = "http://maps.google.com/maps/api/geocode/json?address=".urlencode($query_string)."&sensor=false";

$data = url_get_contents($query);
$data = json_decode($data);

if( $data->status == 'ZERO_RESULTS' ){

return array('Error'=>True);

}else{

$long = $data->results[0]->geometry->location->lng;
$lat = $data->results[0]->geometry->location->lat;


return array('Latitude'=>$lat,'Longitude'=>$long,'Error'=>False);

}
}

add_action("gform_field_validation_1_1", "pre_submission_handler2",10,4);
function pre_submission_handler2($result, $value, $form, $field){


$CP1 = $_GET["gform_post_id"];
$query_string = $value["1.1"]. "+" . $value["1.3"] . "+" . $value["1.4"]. "+" . $value["1.5"];
$coords = getLatLong($query_string);
if( $coords['Error'] ){

$validation_result["is_valid"] = false;

$result["is_valid"] = false;
$result["message"] = "We did not recognize this address, please recheck and submit.";

}else{

$result["is_valid"] = true;
$result["message"] = "";

update_post_meta($CP1, 'custom-lat', $coords['Latitude']); //set these to your custom fields
update_post_meta($CP1, 'custom-long', $coords['Longitude']);

}

return $result;

}


This is for when the form is creating a post on submission, you'll have to modify how you get post ID if you are doing something else.

2014-11-01

Arnav Joy answers:

can you show me what you have ?
are you creating posts using these gravity forms ? and these fields working as custom fields?

2014-11-02

Dylan Kuhn answers:

Here's a [[LINK href="https://gist.github.com/cyberhobo/576aa79a9f1d246d7817"]]code snippet[[/LINK]] you can use in a plugin or your theme's functions.php:


/**
* Example WordPress hook to geocode Gravity Forms fields for Geo Mashup.
*
* This example uses a form ID of 2, replace _2 with your form ID,
* 'address' and 'zip' with your Gravity Forms field names, and prefix
* with your unique namespace prefix.
*/

add_action( 'gform_after_submission_2' 'prefix_gform_after_submission_2', 10, 2 );

function prefix_gform_after_submission_2( $lead, $form ) {
// Require a post id
if ( empty( $lead['post_id'] ) )
return;

// Require Geo Mashup
if ( !class_exists( 'GeoMashupDB' ) )
return;

// Geocode address fields
$postmeta_address_fields = array(
'address',
'zip',
);
$geocode_address_parts = array();
foreach( $postmeta_address_fields as $postmeta_address_field ) {
$field_value = get_post_meta( $lead['post_id'], $postmeta_address_field, true );
if ( !empty( $field_value ) ) {
// Remove commas to avoid mixing them up with ours
$geocode_address_parts[] = str_replace( ',', ' ', $field_value );
}
}

if ( empty( $geocode_address_parts ) )
return;

$location = GeoMashupDB::blank_location();
if ( GeoMashupDB::geocode( implode( ', ', $geocode_address_parts ), $location ) )
GeoMashupDB::set_object_location( 'post', $lead['post_id'], $location );
}