2

I'm using the Smart Map plugin, and I can't seem to figure out how to output all the locations of my entries from a single section onto one single map.

I think the issue I'm having is trying to get the information out from a Matrix block within each entry or knowing where to start!

The section is 'buildings' which contains all the building entries. For each entry, there is a matrix block called 'buildingContent' which has 5 matrix blocks, and the one I want to pull information from is 'howToGetThere' (last block out of the matrix array). In this block, I have a field 'smartMap' which contains the location of the building.

How would I extract the location from the 'smartMap' field for all my entries and output them onto one single map?

I know the code below outputs the location of one entry (St Bartholomew The Great), but thought it might be useful to include what I've tried so far. I adapted it from the documentation:

{% set entry = craft.entries.slug('st-bartholomew-the-great').first %}

{% set locations = entry.buildingContent %}

{% set options = {
     height: 600,
     zoom: 13,
     draggable: true,
     markerInfo: '_includes/mapInfoBubble',
     markerOptions: {
        icon: 'images/map-marker2.svg',
     }
 } %}

{{ craft.smartMap.map(locations[4], options) }}

Any help much appreciated, thanks!

2

Great question! While seemingly tricky, the solution to this should be relatively simple...

Per the documentation, the locations parameter can come in a variety of different forms. But the one we're looking for here is this one...

As an array of elements

Collecting Matrix blocks from across different entries can be complicated, but I think you have (fortunately) described a fairly straightforward use-case. We'll need to do the following:

  1. Loop through relevant entries.
  2. Build an array of Matrix blocks.
  3. Use that array to render a map.

{# Initialize locations array #}
{% set locationsArray = [] %}

{# Loop through all relevant entries #}
{% for entry in craft.entries.section('buildings').find() %}

    {# Get all relevant Matrix blocks #}
    {% set matrixBlocks = entry.buildingContent.type('howToGetThere').find() %}

    {# Append blocks to locations array #}
    {% set locationsArray = locationsArray|merge(matrixBlocks) %}

{% endfor %}

{# Render map #}
{{ craft.smartMap.map(locationsArray, options) }}
  • 1
    Thanks so much Lindsey, this has worked a treat and all the locations are now showing on the single map! – H. Ng Jun 25 '17 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.