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.

Saving lat/long data from a form

  1. hardseatsleeper
    Member

    This is not a GF issue really, but maybe some of you brainiacs can help me.

    I have a form where people are entering addresses & locations. I could use Google's reverse geocoding APIs to get the latitude and longitude of the adresses, but what I really want to do is save that data to the db when they submit.

    Any idea how I could hack a gravity form to run the geocoder, then save the lat/long to the db?

    Posted 8 years ago on Saturday January 22, 2011 | Permalink
  2. This is possible, but it would be fairy complicated. You would first need to know how to use the Google geocoding API.

    You would first need to add one or two hidden fields to your form depending on if you want to store the lat/long in one field or as a lat and a long.

    On form submission you would use the gform_pre_submission hook and pass the field data for the Address field to Google geocoding API and receive back a lat/long.

    You would then also use the gform_pre_submission hook to then store the value that Google returns in the hidden fields.

    You would need to know hot to use WordPress hooks, and the Google geooding API to pull it off. It is certainly doable.

    Posted 8 years ago on Saturday January 22, 2011 | Permalink
  3. hardseatsleeper
    Member

    Hi Carl,

    The Google geocoding I know already, that's not a problem. It's the gform_pre_submission that I don't know.

    http://www.gravityhelp.com/?s=gform_pre_submission

    Thanks for the help, let me look into this. If I can jimmy something I'll post back here.

    Posted 8 years ago on Saturday January 22, 2011 | Permalink
  4. We are finishing up all new documentation that will launch with Gravity Forms v1.5 at the beginning of February. Until then I have created a PDF of the gform_pre_submission hook example and specifications.

    You can download the PDF here:

    http://bit.ly/i1xedx

    Posted 8 years ago on Saturday January 22, 2011 | Permalink
  5. I was wondering if I could get a little direction on this. I would like to geocode lat and lng from the address the user provides in the form and insert lat and lng into the database as post meta. I know how to set up the form, but not sure if I'm on the right track for defining a pre submission function calling the data back from google api. Here's what I pieced together so far, not working.

    [php]
    // gforms hook for form_#
    add_action("gform_pre_submission_6", "pre_submission");
    function pre_submission_handler($form){
    
    	// function to take address and turn it into coordinates lat and lng
    	function getLatLong($code){
    	 $mapsApiKey = '(REMOVED FROM POST ONLY)';
    	 $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;
    	 }
    	}
    
    	//was called process.php
    	$query_string = $_POST['address'] . "+" . $_POST['city'] . "+" . $_POST['state'] . "+" . $_POST['zipcode'];
    	$coords = getLatLong($query_string);
    	$lat = $coords['_wp_geo_latitude'];
    	$lng = $coords['_wp_geo_longitude'];
    }
    Posted 7 years ago on Friday October 7, 2011 | Permalink
  6. Hi Clay,

    There were several issues with the code. I've cleaned it up a bit but you will still need to replace some variables to integrate with your specific form.

    http://pastie.org/2672031

    Also, don't forget to add your API key back in.

    Posted 7 years ago on Monday October 10, 2011 | Permalink
  7. Thanks for the code help David! That's really kind of you. I updated my address city, state, zip fields and api, but no luck. See anything I did wrong? Note, while I removed it here, I included my gmaps api as well.

    http://pastie.org/2679206

    Posted 7 years ago on Tuesday October 11, 2011 | Permalink
  8. By the way the fields for latitude and longitude are _wp_geo_latitude and _wp_geo_longitude

    Posted 7 years ago on Tuesday October 11, 2011 | Permalink
  9. Hi Clay,

    The 6.x IDs are probably not correct. The address field has an ID (ie 2) and then it has a decimal number for each input in the field, so:

    Address = 2.1
    Address Line #2 = 2.2
    City = 2.3
    State = 2.4
    Zip = 2.5
    Country = 2.6

    Make sense?

    Posted 7 years ago on Tuesday October 11, 2011 | Permalink
  10. Ohhh I see! I'm not using the advanced address field because I'm creating posts from the form using POST CUSTOM FIELD s.. Got it so I just use the number from the input field! Thank yoU!!!

    Posted 7 years ago on Tuesday October 11, 2011 | Permalink

This topic has been resolved and has been closed to new replies.