Thursday, November 20, 2008
You are here:  Programming
 
Hiroshige

The following articles have to do with various programming topics.

Google Map GPS Cell Phone Tracker

By Nick Fox on Wednesday, November 28, 2007 10:23 AM : 42397 Views
 

Upload Webpage

Things get a lot easier now. In both the .Net and PHP versions, we get our queryString and send it to the database. Notice that there is a method called getDateFromJavaDate(). All this does it convert it from a java date to something that is a little more human readable. In the .NET version, we have a nice helper class that let us call a stored procedure with our sql parameters. In both the .NET version and the PHP version, we send a 1 back to the phone to let it know that we had a successful update of the DB. This is how the phone knows to remove the queryString from the queue. In the PHP version, the INSERT query is on the PHP page, this allows us to use versions of the MySQL database older than version 5 (version 5 supports stored procedures).

Database

Both databases have just one table called gpslocations. Most of the fields are pretty straight forward except for that last one. The locationMethod field. Cell phones have different ways of getting it's location. The first one is obviously from satellites. Sometimes a cell phone may not be able to get a fix on satellites but certain phones can get a location using the location of the nearest cell phone tower. As you move around with your cell phone, you'll see that a value of 327681 is a satellite fix and a value of 8 is a cell tower location. You can actually look on your Google map and see where the cell tower that you are using is.

The MySQL database just has the table in it. The Microsoft SQL Server has 2 stored procedures. The first prcSaveGPSLocation just inserts a row into the gpslocations table and returns a value greater than 0 on success. The prcGetGPSLocations is a little more interesting. There we are using the FOR XML PATH statement to generate XML. That FOR XML PATH really is the greatest thing since sliced bread. Unfortunately, MySQL does not have any XML support for queries. As great as MySQL is, that really is it's shortcoming. The XML that we are getting looks like this:

<gps>
<locations latitude="47.508464" longitude="-122.023040" speed="40" direction="356" distance="2" locationMethod="327681" gpsTime="9:02pm Tue, Jun 19 07"/>
<locations latitude="47.518773" longitude="-122.030123" speed="35" direction="335" distance="3" locationMethod="327681" gpsTime="9:03pm Tue, Jun 19 07"/>
<locations latitude="47.526416" longitude="-122.036533" speed="24" direction="330" distance="3" locationMethod="8" gpsTime="9:04pm Tue, Jun 19 07"/>
</gps>

Data Retriever and XML Converter

Here is where the PHP page catches up. We simply concatenate the XML together with the data to form an XML string. I could have used the DOM or any number of PHP classes to form the XML but I will leave that as an exercise for the reader. The PHP code does work well as it is. Please note that on the PHP page as well as the .aspx page, the header "Content-Type" is being set to "text/xml". This is important. When we make the ajax call later from the Google map webpage, it's wants to see that data coming in as text/xml in order for it to parse properly. ResponseText will work and it looks like XML but we want to use ResponseXML which is expecting text/xml. There is another important thing to notice on the .NET page. I have removed all the html except for .NET page header from the GetGpsLocations.aspx page. This allows us to just send XML to the Google maps page. The .NET page uses a nice helper class to retrieve the XML from the DB.

Google Map Display Webpage

Ok, we have finally arrived at the webpage that will display the GPS data on Google maps. At this point the .NET version and the PHP version are the same. The PHP version has a .html extension and the .NET version has an .aspx extension but it doesn't need it, it could have been .html as well. The nice thing about packaging our data as XML is that we have used two different methods of delivery (PHP and .NET) and we are now able to use the same webpage and javascript to process the data.

The Google Maps API is pretty nice. There are number of helper functions built in to help us display the data. You'll notice that the DisplayMap webpage is pretty simple. It has one line of javascript code and and a div to hold the map. You'll need to register with Google to get your own maps API key. Replace the key that is there with your new one. You have to put this code on a webserver with a registered domain name. This will not work on localhost. On the webpage, you'll also notice that there are 2 images with a style of "display:none". These are hidden but they need to be there for Internet Explorer. It preloads the images so that IE doesn't make a separate call back to the webserver for every single marker that you display on the map.

Previous Page | Next Page

SourceForge.net Logo

Our Sponsors
Privacy Statement | Terms Of Use