The Community forums are being phased out in favor of a new Slack group.
Add your email address below to get an invitation to join the community slack group
Address Fields to ACF Google Map Field
As a non-developer I've been struggling with getting certain things working with my forms and as I've just managed to achieve something I've been trying to do so I thought I'd post it here in case it helps anyone in the future as I couldn't find the answers I needed on this support forum.
The Problem
Pass address data from an FP form to an ACF Google Map custom field so that the address appears in the field and the map is rendered with the correct location marker.
The Solution
I found out that the ACF field holds the address data as a serialized array. I had no idea what that meant so with the help of Google and numerous posts I finally managed to work it out and as is often the case, it's pretty simple once you get your head round what's going on and how to do basic things.
First you need to have a way of getting the address data into your Formidable form in the first place. I've been using a postcode API (https://ideal-postcodes.co.uk) to get the address from an entered postcode but you can also use a plugin like Geo My WP which has a Formidable addon to insert a Google Maps API search field into your form.
The ACF Google Map field takes an array in the form:
array( 'address' => 'xxx', 'lat' => 'xxx', 'lng' => 'xxx' )
If you use a plugin like Geo My WP then one of the fields you can insert is the formatted address from the API search along with Lat/Long fields. If you use a postcode/zipcode API then you will have to concatenate the address fields to get a complete address for the ACF field. This is what I do in the code below. Either way, you need to create a hidden field in your form to hold the final serialized array string.
Here is the code I ended up using in my functions.php:
// Create location array and serialize for ACF Google Maps field
add_filter('frm_validate_field_entry', 'serialize_location', 10, 3);
function serialize_location($errors, $posted_field, $posted_value){
if($posted_field->id == 25){ //Change this to the ID of your hidden field to hold the serialized array
$group_fields = array(74, 75, 76, 77, 78, 79); // List of fields containing address lines, e.g., Line1, Line2, Town, City etc.
// Loop through address fields and add non-empty fields to an array
foreach ( $group_fields as $field_id ) {
if ( $_POST['item_meta'][$field_id] != '' ) {
$address_lines[] = $_POST['item_meta'][$field_id];
}
}
$full_address = implode(", ",$address_lines); // Expand the array to a string with values separated by commas for formatted address
$_POST['item_meta'][$posted_field->id] = serialize(array( 'address' => $full_address, 'lat' => $_POST['item_meta'][81], 'lng' => $_POST['item_meta'][80])); // This lines adds the Lat/Long fields to an array with the full address, serialises it and puts in the hidden field. Change 80/81 to the IDs of your Lat/Long fields
}
return $errors;
}
If you don't need to concatenate the address due to using a maps plugin, you can just use this:
// Create location array and serialize for ACF Google Maps field
add_filter('frm_validate_field_entry', 'serialize_location', 8, 3);
function serialize_location( $errors, $posted_field, $posted_value ) {
if($posted_field->id == 25){ //Change this to the ID of your hidden field to hold the serialized array
$_POST['item_meta'][$posted_field->id] = serialize(array( 'address' => $_POST['item_meta'][113], 'lat' => $_POST['item_meta'][115], 'lng' => $_POST['item_meta'][116])); // Change 113,115.116 for the IDs of your full address, latitude and longitude fields
}
return $errors;
}
Finally, in Form Actions / Create Post, map the ACF Google Map custom field to your hidden field containing the serialized array. End result is address info from an FP form entered into an ACF field in the correct format.
Hope this helps someone else. I also had a thread with the code I use for the postcode lookup API here: https://formidableforms.com/help-desk/how-to-use-an-api-for-uk-address-auto-complete/
June 10, 2016 at 4:48 am
Hello,
Thank you for your post. I tried but it does not work. because I have no latitude and longitude field !!
in form Formidable Pro
I created three fields:
addr, postal and city
I have a field that appends these 3 fields
concat = addr + postal + city => OK :-)
I have a field acfGM = Google maps ACF
acfGM maps = concat
I have no storage is done
We can not let latitude and longitude empty?
How have latitude and longitude? it is mandatory to use the plugin Geo My WP?
Regards
June 10, 2016 at 4:54 am
You don't have to use Geo My WP but you do have to use some method to retrieve the lat/long from somewhere, either using the Google Maps API or some other API that allows you to retrieve lat/long data from postcodes/zipcodes. I linked to a UK one and there is Ziptastic and lots of services out there that you could use and several other WP plugin choices to get geolocation data. I just selected Geo My WP because it has a nice FP integration.
This particular post it not about how to get that data into your form but more how to get the data from the form into ACF.
June 10, 2016 at 5:53 am
Hi,
Ok thank you for the quick response :-)
I may be waiting for the add-on of FormidablePro because I'm not a developer (a bit complicated for me to use the api)
Regards
June 10, 2016 at 5:55 am
Geo My WP is a plugin that will do what you need for $34
https://geomywp.com/add-ons/formidable-geolocation/
Discussion closed.