Support

Account

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 :
    Screenshot of the text field in ACF
    And this is a screenshot of 1 retailer post (admin side) :
    Screenshot of google Maps field and Text field

    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.