Working with Google Maps

There used to be a time when there was this huge maps craze, it has since passed, but Google Maps remains the most recognized map applications seen on the internet.  Recently, I worked on Google Maps API for a client.  This post is a retrospect look at how it went.  I’ve not worked with other map systems, so I cannot compare my experience.

My task at hand was to create a store locator that would take an address as input and plot all the points on a map that was within 100 miles of the given location.  A fairly simple map application, except I decided to innovate.  My first stop was the articles page on the Google Maps API Reference page.  I found a very handy tutorial which was exactly about creating a store, wow that made my work much easier.  What I found very helpful from that tutorial was the formula.  this formula.

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) – radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

That is the heart of the entire module.  That forumla returns coordinates that are within 25 miles of a point with coordinates (37, -122).  The complexity (if at all) of the application is to pass data from a database using PHP or other server-side language and passing into a javascript function.  The tutorial that I was looking at used xml to pass data to the javascript function.  This of course is nice, but I was a bit lazy and a bit innovative.

In my quest for something better, I discovered JSON.  Now, this seemed simple enough since is 2010 and most languages have JSON support including PHP.  So, I put all the the results into a hidden textbox as JSON and wrote a javascript function that would execute on window load.  Using that information, I could then loop through it and mark points on the map from that.

jQuery being an awesome library provided a means for me to do exactly that.  Icould loop through each of them and plot points on the map quite painlessly.

function markOnMap(x, y) {
    geocoder = new google.maps.Geocoder();
    latlng = new google.maps.LatLng(x, y);  //center the map to the coordinates of the searched address
    var myOptions = {
        center: latlng,
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        mapTypeControl: false
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var markers = document.getElementById('marker').value;
    var mapPoints = $.parseJSON(markers);
    var marker = new Array();
    i = 0;
    $.each(mapPoints, function() {
        var latlng = new google.maps.LatLng(this.lat, this.lng);
        marker[i] = new google.maps.Marker({
                    map: map,
                    draggable: true,
                    position: latlng,
                    content: '<b>Name : </b>' + this.name, //very important
              });
    google.maps.event.addListener(marker[i], 'click', function() {
        infowind = new google.maps.InfoWindow({
            content: this.content,
            });
        infowind.open(map, this);
        });
    i++;
    });
}

(WordPress seems to gobble up the indenting, so if you want the code, its on pastebin.com)

When using infowindows, its very important that the content is stored inside the marker and then used to pop out the infowindow, that’s the only way that works.  I spend about 5 hours trying to figure that one out.

Comunity rocks

Hold my hand by Adam Foster | Codefor on Flickr. Licensed under Attribution-NonCommercial-NoDerivs 2.0 Generic
Though I’ve been involved with the Ubuntu community for close to a year now, support wasn’t something I was good at (mainly because #ubuntu was too fast for comfort). I’ve been working on cakephp for about 3 weeks now and I’ve been in the channel the whole time. The one thing that always strikes me in the help channel of any open source software is the community feeling around there. I’ve often seen person A coming in asking a question and a person B coming in to ask another question and they end up answering each other and both leave happy. That feeling just plain rocks!

Baking on Ubuntu while using xampp

xampp logoSo, I’m a web developer working on Ubuntu.  Since I don’t want to get into complications like pinning to have earlier versions of PHP (useful when developing on Drupal), I use XAMPP.  Its a bit of a work to get php ready to bake while using XAMPP on Ubuntu.  These instructions are probably generic to any bash shell, but tested and working on Ubuntu.  Also note, I assume you install XAMPP in the expected directory at /opt/lampp

Step 1:  Add an alias for php in .bashrc.  Open the ~/.bashrc file and add the following line
alias php='/opt/lampp/bin/php'

Step 2: When running the bake script, run it as follows
php cake.php bake

pageTitle gotcha

When cake moved from 1.2 to 1.3, there were a few changes.  While the manual is up-to-date, the default manual that’s shown tends to be 1.2 and there is a section in the 1.2 manual that talks about the migration.  I got stuck with one of the gotchas today.  I spent 1 hour trying to find out why the title I set in $pageTitle would not print with $title_for_layout until Phally pointed it out on IRC for me.  To set the title from the controller, I’m supposed to $this->set('title_for_layout', 'My title'); doh!  Anyway, now I’m changed my bookmark to the 1.3 manual instead of 1.2

For a full list of changes, take a look at the transition guide.

Cake is yummy!

Its a few weeks since I formally started my career as a web developer and I feel I’ve got lots to say.  I started working on a project writing PHP code that mixed with the view, basically a bunch of php files with lots of PHP code and html.  It was easy, but it wasn’t great fun.  There was indeed a lot of copy and paste and find and replace, but it was hard work to get things done though it was easier to figure out how to do things.
CakePHP Logo
When I started working on a new project, I convinced my boss that we should try moving to a framework and I chose CakePHP for some reason though there was CakePHP, Codeigniter, and Symfony to chose from.  Something about CakePHP pulled me towards it.  It took a fair bit of experimentation before I was able to convince myself that I could do it (yeah, this was after I convinced my boss).

After about a week into development, I can happily say that it is indeed awesome and yummy.  I like the inbuilt helpers and components that helps do a lot of common tasks like access control, pagination, data validation, and others quite easily.  But the best part, is baking code.  It takes a lot of the boring part out of coding for a web application especially when your entire application is a CMS like system that is somewhat custom made to client requirements.  Being the first project I’m doing in CakePHP, I think it might take around 2 weeks for this project, but I’m pretty sure, I can cut down this time to probably a week or less once I get a feel for it.

I’ve got stuck a fair number of times and I’m really glad that most of my google searches lead me back to the Manual and its mostly what I wanted at that time too.  In some cases I got stuck and lost for hours at a time, and that was when IRC came to my rescue.  There was always someone online who had a good idea about cake, particularly markstory and savant, always willing to help us newbies.  I look forward to more adventures and perhaps more frequent posts.