20 Apr
Archivado en: PHP, Programación
Para implementar una librería específica para la API de Google Maps en Code Igniter he seguido las instrucciones que puedes encontrar en el wiki de CI. Vayamos por pasos:
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;
}
}
CREATE TABLE GEOCODES (
address varchar(255) NOT NULL default '',
lon float default NULL,
lat float default NULL,
PRIMARY KEY (address)
);
Ya está todo preparado, para comprobar que funciona, vamos a crear un controlador y una vista que muestre un mapa con 3 coordenadas, para ello:
<?php
class Gmap extends Controller
{
function Gmap()
{
parent::Controller();
$this->load->library('Cigooglemapapi');
$this->cigooglemapapi->setAPIKey('PON_AQUI_LA_APIKEY');
}
function index()
{
$this->cigooglemapapi->setWidth('100%');
$this->cigooglemapapi->disableDirections();
$this->cigooglemapapi->addMarkerByAddress('Obispo Cobos 12 Ubeda Spain','Mi casa','<b>Mi casa</b>');
$this->cigooglemapapi->addMarkerByAddress('Trinidad 6 Ubeda Spain','La casa de Juán','<b>La casa de Juan</b>');
$this->cigooglemapapi->addMarkerByAddress('Rastro 2 Ubeda Spain',"La casa de Marta","<b>La casa de Marta</b>");
$this->load->view('gmap');
}
}
?>
<!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()">
<?php
$this->cigooglemapapi->printMap();
?>
</body>
</html>
Ahora ponemos en nuestro navegador: http://localhost/index.php/gmap y debería aparecer el mapa.
RSS de los comentarios de esta anotacion · TrackBack URI
Deja tu comentario