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!
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 keylatitude
in your options array, you probably meanlongitude
by the look of your Post model's field name. So all put together, this should do the trick: