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.

Passing Geo Location to Gravity Form

  1. jrothca
    Member

    I trying to figure out the easiest way to pass the latitude and longitude information from the web browser using HTML5 to a gravity form. I plan on using (2) custom fields with the visibility set to admin only, one for latitude and one for longitude. I have checked the 'Allow field to be populated dynamically' and will set a 'Parameter Name'. I know I have to use hook, but I don't have very much php experience. It seems like it would be simple to accomplish.

    How do I modify my functions.php file to pass the lat long information from the web browser to dynamically populate my 2 custom fields ?

    Posted 12 years ago on Sunday January 29, 2012 | Permalink
  2. jrothca
    Member

    I figured it out. It's most likely hack. If a GF employee could explain how to use hooks to do this, it would be helpful.

    This is what I did to get it to work.
    I put this in my header file to have the script loaded:
    '<script src="http://www.google.com/jsapi"></script>
    <script>
    google.load("jquery", "1.4.1");
    </script>'

    I put this in my page template that has the gravity form on it.
    '<script>
    navigator.geolocation.getCurrentPosition(
    function(pos) {
    $("#input_1_12").val(pos.coords.latitude);
    $("#input_1_13").val(pos.coords.longitude);
    }
    );
    </script>'

    The input_1_12 & input_1_13 variables correspond to the 'input ID' the gravity form creates. When the page loads the browser dynamically populates the form with input ID input_1_12 for latitude & input_1_13 for longitude.

    The input_1_12 number will very depending on how many GF's you've created and how many input fields you have on your form.

    Posted 12 years ago on Sunday February 5, 2012 | Permalink
  3. Thanks for sharing! This worked for me and happened to be the same approach I'd been going down. I think geolocation should be supported more easily with gravity forms.

    Posted 12 years ago on Friday March 9, 2012 | Permalink
  4. Ok.. Some questions. My intent was to capture longitude and latitude (as above) and then use the CustomFields plug-in for gravity forms to add a geolocation element to the post. CustomFields recognizes the Wordpress Geolocation plug-in. As the form is submitted the blog post is created.

    However, the location data isn't posted with the blog? Help appreciated. Thanks

    Posted 12 years ago on Friday March 9, 2012 | Permalink
  5. jrothca
    Member

    Here's how I did it.
    Save lat long data to wordpress
    1. Set gravity form to save the lat and long data to the wordpress custom fields 'geo_latitude' and 'geo_longitude' (without the quotes, these (2) field names are supported by many geo location plugins for wordpress)
    Display location data in the post
    1. I used the plugin Geo Mashup. You can use others as long as they support Wordpress Geodata Meta Fields . In the general setting you need to turn on 'Copy Geodata Meta Fields' (turning this on enables the plugin to use the lat and long data that is saved in the custom fields you created above)
    2. Use one of Geo Mashup's many shortcodes or template tags to display the location data in your post. Use the template tags in your php files to have the location data automatically displayed to posts or index page.

    Posted 12 years ago on Saturday March 10, 2012 | Permalink