Codeigniter & Google Maps

I am currently working on a website that is based on bike routes. What basically happens is our client goes out and rides the route tracking the route with his GPS devise. He then downloads this GPS info into MemoryMaps and adjusts the route points as necessary. This information is then exported from MemoryMaps into a GPX file. The GPX file is then uploaded into our custom built backend along with some route information and a description and this route is then listed as a route. The first latitude and longitude imported from the GPX file is saved as the starting point and so an overview map can be shown of all the routes. When you click on one of the route starting points you get a little overview of the route and you can then click to view the full route page.

I am using Codeigniter for this project. I am obsessed with Codeigniter since I was introduced to it by @boxedfish. Its taken me a long time to move away from my own framework, but the move has been well worth it. There is an amazing PHP Google Maps API class which I have used in the past and I was about to get started on creating a Codeigniter library using this class when it crossed my mind to do a quick search in Google for one already built. Wise move! Google Maps API V3 Library already exists thanks to the good people at in-the-attic.co.uk.

Adding an overview map with a number of markers was one of the easiest implementations of Google maps I have ever done… however I got a little stuck when it came to the route information pages where I wanted to show the route points using polylines. A simple enough task!

I fired up the documentation (which is part of the original php class not the library by in-the-attic.co.uk)

$this->gmap->addPolylineByCoordsArray($poly_array,false,'#FF0000', '6', '0.5');

This should have done the trick. I simply get all the coordinates of my route, put them in an array and bing, bang bosh! I tried everything under the sun to get this to work but I could not get the polylines to display on the map, every time I tried to load the page the map would not load. I knew I was getting the information in the array but something was not right. I made a cry of help to twitter but nothing came back… I did what every good developer should do, I went away and worked on something else! Then it hit me, why not just open up the class file and look at the function that processes the $poly_array and see what format it should be in. It was then I realised that I was assuming that everyone called latitude ‘lat’ and longitude ‘lng’. In fact it was expecting ‘long’!

So for anyone else out there was is having problems, the correct array format for addPolylineByCoordsArray is:

$poly_array[] = array('lat' => $my_lat, 'long' => $my_lng);

8 thoughts on “Codeigniter & Google Maps

  1. You sir ARE A LIFE SAVER!!!! I was nearly in tears trying to sort this out! Assumption in programming is the mother of all fkucups and assuming lng over long has got to be my year’s biggest gotcha

    • James Mills says:

      I am so pleased that my post has help a fellow programmer! I was banging my head against a brick wall when I was trying to figure it out. Only felt right to share my experience in the hope I could save someone else the pain. Thanks for your comment.

  2. blessy says:

    Hi

    I am using codeigniter 2.0.3 version and I use vehicle tracking using GPS and android device.
    I need to display the exact location in the google map and draw the diagram(route)(I need to link all the location based on the lat and long) Can any one help me out on these ? Any idea how to implement this on Codeigniter ?

  3. tai-nui says:

    hi thanks for the trick, but actually when reading the doc, we can read :
    “array $polyline_array: array of lat/long coords”
    and NOT
    “array $polyline_array: array of lat/lng coords”

    i should have been more concentrated but i guess lots of people will read “lat/lng” since “long” is used only in this function … 😉

    Regards

  4. Matt Shirey says:

    I was wondering if you ran into the issue of the map in the background refreshing each time you wanted to load points onto the map. I have my points broken into categories and would like to load the points on the map based on the category selected without having to have the map image reload itself. How did you handle the clearing of the map points.

Leave a Reply

Your email address will not be published. Required fields are marked *