PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Geocode Address on Form Submit

  1. 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;
        }
    }
    Posted 12 years ago on Saturday February 4, 2012 | Permalink
  2. Hi Nufantech,

    I didn't see anything obviously wrong with your code. You might try doing some value-checking to make sure the different values are being pulled correctly. Here is an example of how to output the values to the screen:

    [php]
    print_r($entry);

    And would probably be a good idea to print_r() the $coords after they have been retrieved as well:

    [php]
    print_r($coords);
    Posted 12 years ago on Saturday February 11, 2012 | Permalink
  3. For whatever the reason, the filter/action is being ignored by GF. I ended up doing the geocoding directly to the database after the records are added.

    Posted 12 years ago on Saturday February 11, 2012 | Permalink
  4. Hi nufantech,

    Hm, there may be an issue with where you are adding the hook/filter. Also, are you using an older version of Gravity Forms? The gform_after_submission hook is a relatively newer hook.

    Glad you were able to find an alternate solution. :)

    Posted 12 years ago on Saturday February 11, 2012 | Permalink