In my CakePHP 2.4 app using the GoogleMapsv3 helper, I have two views: A paginated list view, and a Google Maps view. I want to show all markers on the map view, but when I use a foreach loop to echo them into the Google map, the Pagination helper steps in and only echos 20 markers at a time.
How do I disable PaginatorHelper on my map view so I can display all results?
My foreach loop, which is being interrupted by the Pagination helper:
foreach ($posts as $post):
$marker_options = array(
'showWindow' => true,
'windowText' => '<b>' . $post['Post']['title'] . '</b>' . '<br>' . $post['Post']['body'],
'markerTitle' => '',
'markerIcon' => 'http://labs.google.com/ridefinder/images/mm_20_green.png',
'markerShadow' => 'http://labs.google.com/ridefinder/images/mm_20_greenshadow.png',
);
echo $this->GoogleMap->addMarker(
"map_canvas",
$post['Post']['id'], array(
'latitude' => $post['Post']['lat'],
'longitude' => $post['Post']['lng']
),
$marker_options);
endforeach; ?>
The solution was actually really easy. The paginator doesn't kick in unless specifically called: I originally copied my map method from my index method, which also copied the paginator over. Stupid mistake.
The method that worked:
public function map() {
$this->Post->recursive = 0;
$this->set('posts', $this->Post->find('all'));
}
this should happen in your controller.
let's say you have a variable which make the difference let's say your url is like:
http://your.server.com/posts/index (for paginated posts)
and
http://your.server.com/posts/index/map for your map view
in your controller you should do the following:
class PostsController extends AppController{
function index($isMap = null){
if($isMap == 'map'){
$this->set('posts', $this->Post->find('all', ...));
} else {
$this->set('posts', $this->paginate());
}
}
this way depending from the url you will return full or paginated results. Bear in mind that loading all results, especially if you add some kind of relation could slow down the page.