-
Notifications
You must be signed in to change notification settings - Fork 0
Google Maps
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. [code]
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;
}
[/code]
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] [code] <?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"); ?> <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()">
<?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.