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: http://www.phpinsider.com/php/code/GoogleMapAPI/GoogleMapAPI-2.3.tar.gz from this site http://www.phpinsider.com/php/code/GoogleMapAPI/

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.

        CREATE TABLE GEOCODES (
          address varchar(255) NOT NULL default '',
          lon float default NULL,
          lat float default NULL,
          PRIMARY KEY  (address)
        );

The actual CI library


<?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;
    }
}

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

More functions to use in the library are available here http://www.phpinsider.com/php/code/GoogleMapAPI/


<?PHP
$this->load->library('Cigooglemapapi');
$this->cigooglemapapi->setAPIKey('yourapikey'); 

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

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.