Home › Forums › General Issues › Google Map field : Is there a way to extract zipcode? › Reply To: Google Map field : Is there a way to extract zipcode?
Hi @trouille2
In this case, you need to create a text field to store the zip code and use the geocoder to get it. Here’s an example how to do that:
// set zipcode to certain field
add_action('acf/input/admin_footer', 'my_acf_get_address_zipcode');
function my_acf_get_address_zipcode() {
?>
<script type="text/javascript">
(function($) {
// do this when the map is changed
acf.add_action('google_map_change', function( latlng, $map, $field ){
// get the address using the geocoder
acf.fields.google_map.geocoder.geocode({ 'latLng' : latlng }, function( results, status ){
// get the zip code from the returned address
var address = results[0].address_components;
var zipcode = address[address.length - 1].long_name;
// set it to a text field
$(".acf-field-1234567890abc input").val(zipcode);
});
});
})(jQuery);
</script>
<?php
}
Where “.acf-field-1234567890abc” is based on the text field key.
I hope this helps 🙂
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.