Tuesday 18 October 2016

Installation of Google Maps using Codeigniter

I modified to fit with Codeigniter were the data access and XML generation.  And in both of these cases it was more about reworking the code structure to fit into the MVC format that Codeigniter utilizes.  First we will look at the data access.
I created a model called MapData to hold my database query.
1<?php
2 
3class MapData extends CI_Model {
4   function __construct() {
5      parent::__construct();
6      $this->load->database();
7   }
8 
9   function getmappoints() {
10      $sql "SELECT * FROM markers WHERE 1";
11      $query $this->db->query($sql);
12      if ($query->num_rows() > 0) {
13         return $query;
14      }
15   }
16}
Yes I know that I do not thoroughly test if there was a failure with the database connection, left it out for brevity.  But as you can see it is just a simple database look up that returns the results object.  Now on to the controller.
The controller is all the other XML creation occurs.  To generate the XML I created a function calledsendXML that will generate the XML and echo it to the page when requested.
1public function sendXML() {
2   $dom new DOMDocument("1.0");
3   $node $dom->createElement("markers");
4   $parnode $dom->appendChild($node);
5   $this->load->model('MapData');
6   $mapdata new MapData();
7 
8   foreach $mapdata->getmappoints()->result() as $row ) {
9      $node $dom->createElement("marker");
10      $newnode $parnode->appendChild($node);
11      $newnode->setAttribute("name"$row->name);
12      $newnode->setAttribute("address"$row->address);
13      $newnode->setAttribute("lat"$row->lat);
14      $newnode->setAttribute("lng"$row->lng);
15      $newnode->setAttribute("type"$row->type);
16   }
17   header("Content-type: text/xml");
18   echo $dom->saveXML();
19}
Once again this code is very straight forward and it almost exactly as you will find in the tutorial code.  Now to render the map I simply created a view and pasted all the tutorial code went into the view verbatim, with the exception of the of the URL for the XML.  Then I called the view form the index function in the controller like so.
1public function index() {
2   $this->load->view('googlemap_mapview.php');
3}
The XML is passed via the Codeigniter controller instead directly from a php file.  And so my entry for URL in the javascript was  a Codeigniter styled URL as such.
1// Change this depending on the name of your PHP file
2downloadUrl("/codeigniter_test/index.php/googlemaptest/sendXML/",function(data) {
And that wraps it up.  I hope this was useful and interesting and I will post more about the project that I am working on later.

1 comment:

Installation of Drop Box API on Codigniter

As with the YouTube API the first step in getting this sucker setup is getting a developer key by visiting  https://www.dropbox.com/develop...