Dynamic Maps: Questions and Featured requests

Hi!

Since we now have the Post layout builder, it makes sense to allow Dynamic Maps, for our post types that require a Map on the listings.

Thanks to Dynamic data, we can easily create Custom fields for the Latitude and the Longitude, and feed the map with that dynamically. So far so good.

However, if we are using a Map field in ACF Pro, it creates a single custom key google_map that is not useful.

I have tried some custom functions found on various forums to separate the lat and lng into separate keys so I can feed the Map element, but with no success.

My question:

Do you know any way (maybe a functions.php code) to separate the ACF latitude and longitude, so we could feed the Map element with the custom keys?

My Feature requests:

1. Map element to read ACF PRO Map data

I suggest that you update the Map element so it can communicate with ACF Pro Map Custom field, and read the latitude and longitude from it, for the individual pins and the map centering. On the single post type, both map centering and the pin position should be fed from the ACF data.

2. Map Looper for the Archive page?

The Archive page (and basically anywhere else where needed), there should be an option to have a Map that is displaying all the pins from all the posts in the selected Post type.

If we are building a listing website, having this solved would basically allow us to get rid of the listing plugins and have map listings natively with Pro.

Thanks!

Allow me to tag @alexander here. I believe Alex would find this interesting. :slight_smile:

PS: to have the ACF Map field working properly, it requires a custom functions.php code that adds the API key to it:

function my_acf_init() {
	acf_update_setting('google_api_key', 'xxx');
}
add_action('acf/init', 'my_acf_init');

Hi @Misho,

As you know our current ACF integration is pretty basic and limited to scalar values (not arrays or nested data) so it’s a bit out or reach out the moment natively, but we do plan on adding ACF Repeater Field support to Loopers and also revisit ACF integration in general.

Meanwhile, I can give you some ideas that might help. I was looking at this article: https://www.advancedcustomfields.com/resources/google-map/ It seems like we need to access array keys of the fields returned by ACF.

It’s possible to add custom handlers for dynamic content, so here’s some custom code that should get you close. I’ve not tested it with your use case, and this kind of thing is a bit outside the scope of support so I can’t spend much more time on it, but I think this example should be a really good starting point.

/*
{{dc:acfpro:field name="location" key="lat"}}
{{dc:acfpro:field name="location" key="lng"}}
*/

// add a filter to supply data for {{dc:acfpro:*}} tags
add_filter( 'cs_dynamic_content_acfpro', function($result, $field, $args = array()) {

  // supply results to {{dc:acfpro:field name="..."}} tags
  if ( $field === 'field' && isset( $args['name'] ) ) {
    
    // get the data for the named field
    $data = get_field( $args['name'] );

    if ( is_scalar( $data ) ) { 
        $result = $data;   // If it is a string or numeric, pass it through directly
    } else if ( is_array( $data ) && isset( $args['key'] ) && isset( $data[$args['key']] )) {
        // If it is an array and a key argument is present, pass through the requested key
        $result = $data[$args['key']];
    }

  }

  return $result;

}, 10, 3 );

I went with acfpro for the example to prevent conflicts with the existing ACF integration. When we revisit all this it will probably look something like the above but with some adjustments to be properly compatible inside of repeater fields and nested repeated fields.

2 Likes

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.