Google Maps

captainkuro edited this page Jan 10, 2013 · 11 revisions
Clone this wiki locally

To implement a google maps api library into CI I did the following. Firstly, download this file: from this site

Extract the file and put GoogleMapAPI.class.php into your libraries folder. Next step is to make it compatible with CI. Make the following file and put it into your libraries folder: Cigooglemapapi.php This file extends the original class and turns the original DB Pear layer into something that makes sense to CI. I made this in an afternoon so it's not perfect. Apart from the database functions I also changed the fetchURL function since it didn't work on my host (I emailed him and it's fine now but I left the function intact). Instead of file_get_contents() it now uses Curl to get the data.

ps database functions are not necessary for the functioning of this class, they do speed it up. You need to make this table. The name of the table might be adjusted within the class. See the original website.

          address varchar(255) NOT NULL default '',
          lon float default NULL,
          lat float default NULL,
          PRIMARY KEY  (address)

The actual CI library


class CiGoogleMapAPI extends GoogleMapAPI {

    function CiGoogleMapAPI(){

    function fetchURL($url) {
        if(ini_get('allow_url_fopen')==true) {
            return file_get_contents($url);
            return $this->curlFetchURL($url);                


    function curlFetchURL($url){
        // create a new curl resource
        $ch = curl_init();

        // set URL and other appropriate options
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        // grab URL and pass it to the browser
        $content= curl_exec($ch);

        // close curl resource, and free up system resources
        return $content;

    function getCache($address) {

        $_ret = array();
        $this->ci =& get_instance();
        $sql='SELECT lon,lat FROM ' . $this->_db_cache_table. ' WHERE address ="'.$address.'"';


        if($_row = $_res->row()) {  
            $_ret['lon'] = $_row->lon;
            $_ret['lat'] = $_row->lat;

        return !empty($_ret) ? $_ret : false;

    function putCache($address, $lon, $lat) {
        if(strlen($address) == 0 || strlen($lon) == 0 || strlen($lat) == 0)
            return false;

        $this->ci =& get_instance();

        $data = array(
           'address' => $address,
           'lon' => $lon,
           'lat' => $lat,   

        $this->ci->db->insert($this->_db_cache_table, $data); 
        return true;

When that's done we can begin loading the library and the code to show something. The example is taken from the original website and adapted to CI. It should work, I haven't tested it. Hopefully it shows

More functions to use in the library are available here


$this->cigooglemapapi->addMarkerByAddress('621 N 48th St # 6 Lincoln NE 68502','PJ Pizza','<b>PJ Pizza</b>');
$this->cigooglemapapi->addMarkerByAddress('826 P St Lincoln NE 68502','Old Chicago','<b>Old Chicago</b>');
$this->cigooglemapapi->addMarkerByAddress('3457 Holdrege St Lincoln NE 68502',"Valentino's","<b>Valentino's</b>");
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="" xmlns:v="urn:schemas-microsoft-com:vml">
        <?php $this->cigooglemapapi->printHeaderJS(); ?>
        <?php $this->cigooglemapapi->printMapJS(); ?>
        <!-- necessary for google maps polyline drawing in IE -->
        <style type="text/css">
          v\:* {
    <body onload="onLoad()">
        <table border=1>
            <?php $this->cigooglemapapi->printMap(); ?>
            <?php $this->cigooglemapapi->printSidebar(); ?>

I hope it's clear. The database class could be abstracted more with ActiveRecord, feel free to do so if you can.

Ya its working.

It's a good thing I came across this library, I had plans to make this EXACT Google Maps class into a CI Library.