Use kirby-locator in a Structure field

Hello

I’m trying to use the kirby-locator Plugin in a Structure field. I want to retrieve the Longitude/Latitude from multiple location so I can build a Map with location markers.

I tried something like this:

  <?php
  $items = $page->locations()->toStructure();
  foreach ($items as $item): ?>
    <?= $item->coordinates()->lat() ?>
  <?php endforeach ?>

But now I get a «ErrorException: Array to string conversion».
It’s probably easy to resolve but Im just not so good in PHP yet :slight_smile:

Thanks for helping

You have to convert $item->coordinates() to yaml:

Thanks for your help. I’m still a bit lost
I tried this now:

  <?php foreach ($page->locations()->yaml() as $location): ?>
    <?= $location['lat'] ?>
  <?php endforeach ?>

and I get an «Undefined index: lat»

this is the dump of $location:

Array
(
    [coordinates] => Array
        (
            [lat] => 47.054267
            [lon] => 8.289384
            [number] => 12
            [city] => Luzern
            [country] => Switzerland
            [postcode] => 6003
            [address] => Bernstrasse
        )

)
Array
(
    [coordinates] => Array
        (
            [lat] => 47.054267
            [lon] => 8.289384
            [number] => 12
            [city] => Luzern
            [country] => Switzerland
            [postcode] => 6003
            [address] => Bernstrasse
        )

)
Array
(
    [coordinates] => Array
        (
            [lat] => 47.044094
            [lon] => 8.304822
            [number] => 7
            [city] => Luzern
            [country] => Switzerland
            [postcode] => 6003
            [address] => Bundesstrasse
        )

)
Array
(
    [coordinates] => Array
        (
            [lat] => 47.044094
            [lon] => 8.304822
            [number] => 7
            [city] => Luzern
            [country] => Switzerland
            [postcode] => 6003
            [address] => Bundesstrasse
        )

)

No, each of your structure items has a coordinates field, so you don’t use yaml() on the locations field (which is your structure field), but on the coordinates field inside the item, as I wrote above:

<?php
$items = $page->locations()->toStructure();
foreach ($items as $item): ?>
    <?php $coordinates = $item->coordinates()->yaml(); ?>
    <?php echo $coordinates['lat']; ?>
    <?php echo $coordinates['lon']; ?>
<?php endforeach ?>

ahh I see :slight_smile: thank you so much