Google Map Helper

This article is also available in the following languages:
By gigapromoters
A handy tool to generate maps using Google Map API.
I found CakePHP to be helpful with a lot of things and I loved the fact that anyone can reuse any classes made in PHP using 'vendors' in CakePHP. We recently wrote a CakePHP helper for generating map in CakePHP application.

The helper uses Google Maps API V2 class (http://www.phpinsider.com/php/code/GoogleMapAPI/) from phpinsider.com.

To begin with, just download the class and place them inside 'vendors' folder.

Here's the code for the helper: map.php

<?php
class MapHelper extends Helper
{

    var 
$helpers = array('Html');

    function 
displaymap($locations=false,$width=500,$height=500)
    {
        
vendor('GoogleMapAPI.class');
        
$map = new GoogleMapAPI('map');
        if(
$locations)
        foreach(
$locations as $location)
        {
            
$map->addMarkerByAddress$location['address'],strip_tags($location['title']), $location['title']);  //adds address to showup in Map
        
}
        else
        {
            
$map->setCenterCoords(-96.67,40.8279);   // if no locations are passed in function, then focus on US
            
$map->setZoomLevel(3);
        }

        
$map->setWidth($width);
        
$map->setHeight($height);
        
$map_content=$map->getHeaderJS().$map->getMapJS().$map->getMap();
        return 
$this->output($map_content);
    }
}
?>

Here's the code for the view (index.thtml in my case): index.thtml

<? php
// initialization of $my_locations array to show in map - you can do this in your controller.
$my_locations=array();
$my_locations[1]['address']='621 N 48th St # 6 Lincoln NE 68502';
$my_locations[1]['title']='PJ Pizza';

$my_locations[2]['address']='826 P St Lincoln NE 68502';
$my_locations[2]['title']='<b>PJ Pizza</b>';

echo $map->displaymap($my_locations,500,500); ?>
<script type="text/javascript">onLoad();</script>

Author: Abhimanyu Grover
Giga Promoters

Comments

  • Posted 04/20/11 06:17:52 PM
    It works but how can i change the icon from the map? I want to substitute that red icon that apears. How can i do that? Thanks.
  • Posted 02/13/09 11:56:13 AM
    The way vendor stuff works changes in 1.2. In the helper change 'vendor('GoogleMapAPI.class'); ' to 'App::import('Vendor', 'GoogleMapAPI', array('file' => 'GoogleMapAPI.class.php'));'

    And it works fine with 1.2 :)

    Happy baking.
  • Posted 03/12/08 08:28:13 AM
    $my_locations[1]['address']='621 N 48th St # 6 Lincoln NE 68502';

    I have to change the address..
    How can i give in this...???
    Please explain about this...
  • Posted 10/03/07 04:59:53 AM
    You have to point out in your controller that you wil use the helper 'Map'...

    Then it should work just fine
  • Posted 03/18/07 12:02:52 AM
    I get this error when trying your code:
    Fatal error: Call to a member function on a non-object in /var/www/cake_1.1.13.4450/app/views/pages/home.thtml on line 10

    I copied the both the GoogleMapAPI and map.php code as instructed but still get the error. Any suggestions?
  • Posted 01/02/07 04:35:17 PM
    it's much more flexible. Allows you to create an html template to be embedded in the bubble infoWindow. enjoy
    http://bakery.cakephp.org/articles/view/191

Comments are closed for articles over a year old