Support

Account

Home Forums ACF PRO ACF Pro Address Field Replacement

Solved

ACF Pro Address Field Replacement

  • I have an old theme that uses the address field for about 200 posts. The theme uses the “address field” add-on to do the following:

    1. Display each piece — the full city, state, post code, country as a block. The previous developer used if(!empty()) to make it work no matter how much or little detail an address contained.

    2. Display just the “[city], [state]OR[country]” in another place.

    3. Map the location.

    So, now with ACF 5, I believe I lose this field. Old posts still display previously entered content, of course. But if I edit or add new posts, I lose functionality. I went ahead and fixed the Google Map part already, but I’m wondering if anyone has a suggestion of what might be the best, least manual option for replacing the address field for items 1 & 2 across 200 posts. Thanks!

  • Sorry, would be great if I could somehow extract city and country from the Google Maps field, but … I can’t do that, right?

  • Hi @rktlwm

    I believe you are talking about a 3rd party address field add-on.
    If you are, the easiest thing to do is add v5 compatibility to the add-on.

    Checkout this guide here to find the field type template, and you can then edit the address field type add-on to work with ACF PRO
    http://www.advancedcustomfields.com/resources/getting-started/including-acf-in-a-plugin-theme/

    Cheers
    E

  • Thank you so much for the response, Elliot.

    Yes, I am writing about this plugin:
    https://github.com/strickdj/acf-field-address

    I think you meant to give me this link:

    Creating a new field type
    http://www.advancedcustomfields.com/resources/tutorials/creating-a-new-field-type/
    https://github.com/elliotcondon/acf-field-type-template

    I will give it a shot! (Unless there is a better way to create a group of fields similar to the functionality of this plugin.)

    Also, others’ reference, similar to what I want to do — extract address field types from lat/lng Google Maps settings — you mentioned a good stackoverflow thread here (another here) about “reverse geocoding.”

    I’ll try the field type template first. Thanks!

  • Sorry to reply to an old topic, but didn’t want to start a new thread since I was thinking the same thing after I switched to ACF5 Pro.

    City, State/Locality, Zip and Country would be extremely valuable to pull from these fields. Is there a reason that extra data isn’t stored with the address, lat & lng? That data should be coming in from Google Maps when the request is made.

    I tried digging through the ACF code myself to see, but can’t make much sense of it.

  • Hi @shshaw

    Thanks for the post. Perhaps a good solution is to add a new filter which could be used to define which data is saved for the google map field!

    I’ll add this to the list and let you know how it goes

  • Bringing up an old topic again, but I’m interested in exactly the same thing. The google map field stores the full address as a single field and the coords. Although I can look this up and finally display the city/country, it takes an additional call to Google and it doesn’t show the editor what they’re actually selecting.

    A filter hook doesn’t work for the same reason: another call to google, and the JS display isn’t affected. I’ve edited acf-input.js to achieve this, but of course core edits aren’t a great idea.

    Could we, along with address and lat/long, also store the rest of the components google provides so we can grab any of them?

    Additionally, could we restrict the Place Types, add a location bias or restriction? Eg: Only show results for cities. Only show results within a country.

  • I’m also looking for a way to filter CPT’s based on location settings compared to dynamic address matches based on city, state, country.

    In other words, end users conduct a search for CPT’s by address and expect post matches to show in order of closest proximity and then filter those results using city, state, zip, country.

    The only logical way would be if ACF google maps field stored those address values separately instead of a single value.

    How ACF stores google maps address values:

    Array
    (
    [address] => 3 W 18th St, New York, NY 10011, United States
    [lat] => 40.738917
    [lng] => -73.99243810000002
    )

    Ideal example of how ACF should store address values (IMHO):

    Array
    (
    [street_address] => 3 W 18th St
    [city] => New York
    [state] => NY
    [zip] => 10011
    [country] => United States
    [lat] => 40.738917
    [lng] => -73.99243810000002
    )
  • Hello,

    These ideas are exactly what I am working on currently. The dealerpower solution is great but in my case it doesn’t include all the information I need (for example I would like to save in the db the administrative_area_level_2 short & long)

    So maybe it would be better to let the choice to the user when we create the field.

    Thanks a lot for this plug-in, I hope it will never end its improvement 😉

  • So is there not an update to ACF PRO 5 to allow us to output individually the following:

    • [street_address]
    • [city]
    • [state]
    • [zip]
    • [country]

    I figured since it’s been a year and ACF PRO was released that this would be taken care of. The geo lat and long are recorded but the items need to be mapped and saved separately to be able to call it. The numbering doesn’t work properly as sometimes a ZIP isn’t saved or queried.

    Any update from the ACF team? Thanks!

  • Hi

    Using your add-on @shawnp_thinkodin, good work. Question is; how can I map a new user to those custom fields in User Profile?

    And, will there be a way to have a dropdown for the Country field?

  • I could also use this functionality.

    I’ve been consdiering a hack using jQuery: create separate address fields (address, zip, city, etc), concatenate and copy them into the google map field before publishing.

    It’s important to have the address fields separated out so they can be queried upon.

    DC

  • Hi guys

    I’ve just been running some ideas over the google map field on how to allow extra data to be saved with no luck.

    The data that Google provides about a ‘place’ is not consistent and not formatted in a way that can be used very well.

    Please note there is a JS action available called ‘google_map_change’ which sends through the map and map marker objects each time the map is changed.
    Info here: https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/

    This could be used to lookup map marker data and then create extra hidden inputs to save the data quite easily. The issue is I can’t see how to easily obtain place data from google in all scenarios:
    1. Search for an address
    2. Search for lat/lng
    3. Drag drop / click to place marker

    Please try out the JS action and let me know what you find.

    Thanks
    E

  • @huwrowlands I’m not sure what addon you’re referring to as I’ve not created anything. I was only laying out what I thought should be the setup/functionality for ACF Pro.

  • I believe it should be possible to get the below api response from google places api for an autocomplete address or from a lat/lng position pretty consistently (same principle is implemented in the Drupal Get Locations plugin). If some or all of the response is not present (e.g. a lat/lng in the ocean) the city/state/country field could be left empty. Shouldn’t this cover most use cases?

    I will see if I can make this work with Elliot’s suggestion about the google_map_change event and hidden fields. But a native solution would be much more preferable.

    “result” : {
    “address_components” : [
    {
    “long_name” : “Amsterdam”,
    “short_name” : “Amsterdam”,
    “types” : [ “locality”, “political” ]
    },
    {
    “long_name” : “Government of Amsterdam”,
    “short_name” : “Government of Amsterdam”,
    “types” : [ “administrative_area_level_2”, “political” ]
    },
    {
    “long_name” : “North Holland”,
    “short_name” : “NH”,
    “types” : [ “administrative_area_level_1”, “political” ]
    },
    {
    “long_name” : “Netherlands”,
    “short_name” : “NL”,
    “types” : [ “country”, “political” ]
    }
    ],

  • Thanks to Elliot’s excellent advice I managed to get this issue resolved.
    I created the address fields required like street, street_number, postcal code, city, country etc. I hide these fields with css, but you might want to show them to your clients.

    I hook into the ready action and have to do a little waiting game before creating the geocoder, this is my first attempt and could be improved upon.
    Listening to the google_map_change event I do a geocode request on the marker and inject the parsed values into the field (id’s). Hope this works as a starter for people trying to get more out of the google maps field.

    Remember, this is a draft at best but should help to get you started.

    
    acf.add_action('ready', function( $el ){
    
        setTimeout(function() {
                doStuf();
            }, 2000)
    
    });
    
    function doStuf() {
        acf.geocoder = new google.maps.Geocoder;
        acf.add_action('google_map_change', function( $el , $para2, $para3) {
    
            var latlng = {lat: $para2.marker.getPosition().lat(), lng: $para2.marker.getPosition().lng()};
            acf.geocoder.geocode({'location': latlng}, function(results, status) {
                if (status === google.maps.GeocoderStatus.OK) {
                    console.log(results);
                    if (results[0]) {
                        var location = results[0];
                        if(location && location["address_components"] && location["address_components"].length) {
                            location["address_components"].forEach(function (loc_comp) {
    
                                if (loc_comp.types.indexOf("street_number") == 0) {
                                    // number acf-acf-field_5768465c33a83
                                    jQuery("#acf-field_5768465c33a83").val(loc_comp.long_name);
                                }
                                if (loc_comp.types.indexOf("route") == 0) {
                                    // street
                                    jQuery("#acf-field_576824d541746").val(loc_comp.long_name);
                                }
                                if (loc_comp.types.indexOf("locality") == 0) {
                                    // city acf-field_576824ebf4a32
                                    jQuery("#acf-field_576824ebf4a32").val(loc_comp.long_name);
                                }
                                if (loc_comp.types.indexOf("administrative_area_level_1") == 0) {
                                    // province / state acf-field_576824fe14f6c (strip from ,
                                    if (loc_comp.long_name.indexOf(",") != -1) {
                                        loc_comp.long_name = loc_comp.long_name.substr(loc_comp.long_name.indexOf(",") + 1);
                                    }
                                    jQuery("#acf-field_576824fe14f6c").val(loc_comp.long_name);
                                }
                                if (loc_comp.types.indexOf("country") == 0) {
                                    // country  	acf-field_5768250c14f6d
                                    // country_code_iso2 acf-field_5768469733a85
                                    jQuery("#acf-field_5768250c14f6d").val(loc_comp.long_name);
                                    // iso2 country_code_iso2 acf-field_5768469733a85
                                    jQuery("#acf-field_5768469733a85").val(loc_comp.short_name);
                                }
                                if (loc_comp.types.indexOf("postal_code") == 0) {
                                    // postal_code acf-field_5768468633a84
                                    jQuery("#acf-field_5768468633a84").val(loc_comp.long_name);
                                }
    
                            })
                        }
                    } else {
                        window.alert('No results found');
                    }
                } else {
                    window.alert('Geocoder failed due to: ' + status);
                }
            });
        });
    }
    
    
Viewing 16 posts - 1 through 16 (of 16 total)

The topic ‘ACF Pro Address Field Replacement’ is closed to new replies.