CakePHP and GoogleMapsHelper: How do I load markers from my DB?

552 Views Asked by At

I'm using GoogleMapsHelper with CakePHP to include a Google Map in my app.

In order to add a marker to my map, the documentation says I should use the following syntax, where the three variables are the map ID, the marker ID, and the marker location.

<?= $this->GoogleMap->addMarker("map_canvas", 1, array('latitude' => 40.69847, 'longitude' => -73.9514)); ?>

With this in mind, I've been trying to iterate through my DB and display all my points, but it doesn't do anything: Here's the code, where each post has (lat) and (lng).

<?php 
    foreach ($posts as $post): ?>

        <?php $this->GoogleMap->addMarker("map_canvas", $post['Post']['id'], array('latitude' => $post['Post']['lat'], 'latitude' => $post['Post']['lng'])); ?>

<?php endforeach; ?>

How do I load these entries into my map as markers? Thanks in advance!

1

There are 1 best solutions below

0
On BEST ANSWER

You are simply forgetting to echo the marker. Notice the short tag <?= in the example. It's the short (and somewhat ugly) way for doing <?php echo. Also, there's no need to open/close the PHP tags so much as you're doing. It can all be put in one block. Finally, you're declaring a duplicate key latitude in your options array, you probably mean longitude by the look of your Post model's field name. So all put together, this should do the trick:

<?php
foreach ($posts as $post):
    echo $this->GoogleMap->addMarker(
        'map_canvas',
        $post['Post']['id'],
        array(
            'latitude' => $post['Post']['lat'],
            'longitude' => $post['Post']['lng']
        )
    );
endforeach;