Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 10 revisions

To implement a google maps api library into CI I did the following. Firstly, download this file: [url]http://www.phpinsider.com/php/code/GoogleMapAPI/GoogleMapAPI-2.3.tar.gz[/url] from this site [url]http://www.phpinsider.com/php/code/GoogleMapAPI/[/url]

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. [php]

require('GoogleMapAPI.class.php');

class CiGoogleMapAPI extends GoogleMapAPI{ function CiGoogleMapAPI(){

    parent::GoogleMapAPI();
    }
    
function fetchURL($url) {

if(ini_get('allow_url_fopen')==true)
            {
        return file_get_contents($url);
            }else{
                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
        curl_close($ch);
        return $content;
    }
 function getCache($address) {

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

             
             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();
            $this->ci->load->database();

         
         $data = array(
           'address' => $address,
           'lon' => $lon,
           'lat' => $lat,
           
        );
            $this->ci->db->insert($this->_db_cache_table, $data); 
            return true;
 }

[/php]

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 [url]http://www.phpinsider.com/php/code/GoogleMapAPI/demo/[/url]

More functions to use in the library are available here[url]http://www.phpinsider.com/php/code/GoogleMapAPI/[/url] [php] $this->load->library('Cigooglemapapi'); $this->cigooglemapapi->setAPIKey('yourapikey');

$this->cigooglemapapi->addMarkerByAddress('621 N 48th St # 6 Lincoln NE 68502','PJ Pizza','PJ Pizza'); $this->cigooglemapapi->addMarkerByAddress('826 P St Lincoln NE 68502','Old Chicago','Old Chicago'); $this->cigooglemapapi->addMarkerByAddress('3457 Holdrege St Lincoln NE 68502',"Valentino's","Valentino's");

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml"&gt;
&lt;head&gt;
&lt;?php $this->cigooglemapapi->printHeaderJS(); ?&gt;
&lt;?php $this->cigooglemapapi->printMapJS(); ?&gt;
&lt;!-- necessary for google maps polyline drawing in IE --&gt;
&lt;style type="text/css"&gt;
  v\:* {
    behavior:url(#default#VML);
  }
&lt;/style&gt;
&lt;/head&gt;
&lt;body onload="onLoad()"&gt;
<table border=1>
<tr><td>
&lt;?php $this->cigooglemapapi->printMap(); ?&gt;
</td><td>
&lt;?php $this->cigooglemapapi->printSidebar(); ?&gt;
</td></tr>
</table>
&lt;/body&gt;
&lt;/html&gt;

[/php]

Clone this wiki locally