Home Forums ACF PRO Populate Google Map lng/lat


Populate Google Map lng/lat

  • Hi,

    Quick questions: i’ve got an “old-ish” theme with a map function that’s no longer working: ACF to the resue!

    I’ve got an address and generated lng and lat: how can i dynamicly pre-populate a new ACF field based on this data?

    I’ve tried:

    function my_acf_load_field2( $field ) {
         $field['address'] = get_field( '_job_location' );
         $field['lat'] = get_field( 'geolocation_lat' );
         $field['lng'] = get_field( 'geolocation_long' );
         return $field;
    add_filter('acf/load_field/name=google_map', 'my_acf_load_field2', 10, 3);

    But that’s not working!

    Any tips? 🙂

  • Hey Derk…

    You’re pretty close here.. but the object you’ll use to prepopulate the ACF field is the value of the field (for each post), rather than the field itself..

    Perhaps something like below:

    function my_acf_import_address( $value, $post_id, $field )
        // Only do this if there is not a google_map
        // value already in this post's fields
        if ( empty( $value ) )
            // Here I'm assuming the old map function stored
            // the address, lat & lng as post meta & not as custom fields
            $address = get_post_meta( $post_id, '_job_location', true );
            $lat = get_post_meta( $post_id, 'geolocation_lat', true );
            $lng = get_post_meta( $post_id, 'geolocation_long', true );
            // Now we assign the returned meta values to the
            // value array to be returned
            $value['address'] = $address;
            $value['lat'] = $lat;
            $value['lng'] = $lng;
        return $value;
    add_filter('acf/load_value/name=google_map', 'my_acf_import_address', 10, 3);

    Hope this helps!

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Populate Google Map lng/lat’ is closed to new replies.