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?
Hmm I think we are not talking about the same thing. While I create a new retailer, I have now two fields : Google maps and a text field. Is the text field supposed to be filled automatically by itself with the zipcode when I validate an address in google maps field?
I didn’t implement the text field in my template yet so I didn’t try the end-user part.
This is the code I put in my function.php file :
// 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
$(".zipcode_to_sort input").val(zipcode);
});
});
})(jQuery);
</script>
<?php
}
This is my setting in ACF :
And this is a screenshot of 1 retailer post (admin side) :
I also have to tell you I’ve made this a few years ago when the google maps field was an paying add-on called “Advanced Custom Fields: Location Field” as I entered more than 300 retailers I never tried to adapt the code to match with the new google maps field.
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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.