Getting latitude and longitude from a geolocation field

By: Rob LeVine | Asked: 02/19/2023
ForumsCategory: General questionsGetting latitude and longitude from a geolocation field
Rob LeVineRob LeVine asked 2 years ago
If you're using a geolocation field and you want to get the latitude and longitude from the element, here's how to do it by taking advantage of the hidden properties in the field. Put the following script into the After Fields section of your form's Custom HTML tab.
Start your code here<script>
jQuery(document).ready(function($) {
"use strict";

$("#frm_field_563_container").change(function () { /* 563 is the ID of the geolocation address field */
var lat = $("#geo-lat-563").val();
var lng = $("#geo-lng-563").val();
$("#field_cigab").val(lat); /* your latitude field */
$("#field_o0e96").val(lng); /* your longitude field */
});

});
</script>
2 Answers
Jan Theofel answered 2 years ago
If I understand this correct you suggest to add two additional fields and use your JS code to fill these with the values for latitude and longitude when the form is sent?
Rob LeVineRob LeVine replied 2 years ago

Yes. You add fields, hidden or otherwise so that the lat/long is stored somewhere when the form is submitted/updated.

Jan Theofel replied 2 years ago

Thanks! I contacted support and they made a feature request for this. In the meantime I'll use your workaround.

Hans BaumhardtHans Baumhardt answered 2 years ago
Is this still working ? Just bought the the Formidable Pro version specifically to get lat/long from a google maps fill, unfortunately cant get this to work. <script> added to Settings | customise HTML | After fields section and fields updated to the hidden lat and long field keys but the fileds are not filled :(
Rob LeVineRob LeVine replied 2 years ago

I can say with absolute certainty that it's still working. Send me a link to the page where you're having the issue.

Hans BaumhardtHans Baumhardt replied 2 years ago

<p><p>This just in from the wonderful Formidable support folks which works perfectly in child theme</p><p> </p><p> </p></p>

Hans BaumhardtHans Baumhardt replied 2 years ago

<pre>Start your code here <br /><?php
add_filter('frm_validate_field_entry', 'store_address_cordinates', 10, 3);
function store_address_cordinates($errors, $posted_field, $posted_value){
$fields = array( 31014, 31015); //Change 31014 and 31015 to IDs of the hidden fields where the long and lat cordinates should be stored.
if ( in_array( $posted_field->id, $fields ) ) {
$address_field_id=31016; //change 31016 to the ID of the Address field
if ( empty( $_POST['geo_lat'] ) || empty( $_POST['geo_lng'] ) || ! is_array( $_POST['geo_lat'] ) || ! is_array( $_POST['geo_lng'] ) ) {
return;
}

if ( ! array_key_exists( $address_field_id, $_POST['geo_lat'] ) || ! array_key_exists( $address_field_id, $_POST['geo_lng'] ) ) {
return;
}

$lat = floatval( $_POST['geo_lat'][ $address_field_id ] );
$long = floatval( $_POST['geo_lng'][ $address_field_id ] );

$_POST['item_meta'][31014] = $lat; //Change 31014 to the ID of the latitude hidden field
$_POST['item_meta'][31015] = $long; //Change 31015 to the ID of the longtitude hidden field
}
return $errors;
}
</pre><p> </p>

Hans BaumhardtHans Baumhardt replied 2 years ago

<p>Except this post editor has some amusing html/code formatting issues.</p>

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right