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?

  • 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 🙂