views:

346

answers:

1

Hi.

I am new to the googlemaps API. I have written a small app for my mobile phone that periodically updates its location to an SQL databse.

I would like to display this information on a googlemap in my browser. Ideally i'd like to then poll the database periodically and if any new co-ords have arrived, add them to the line.

Best way of describing it is this;

http://tiny.cc/HEIa0

In a quest to get to there, i've started on the documents on google and been modifying them to try and acheive what I want. It doesn't work - and i don't know enough to know why. I would love some advice as to why, and any pointers towards my ultimate goal would be very much welcomed. Google Maps AJAX + MySQL/PHP Example

   <script type="text/javascript">
  //<![CDATA[

function load() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(47.614495, -122.341861), 13);

    GDownloadUrl("phpsqlajax_genxml.php", function(data) {
      var xml = GXml.parse(data);
      var line = [];
      var markers = xml.documentElement.getElementsByTagName("points");
        for (var i = 0; i < points.length; i++) {
            var point = points.item(i);
            var lat  = point.getAttribute("lat");
            var lng  = point.getAttribute("lng");

            var latlng = new GLatLng(lat, lng);

       line.push(latlng);
       if (point.firstChild) {
         var station = point.firstChild.nodeValue;
         var marker = createMarker(latlng, station);
         map.addOverlay(marker);
       }
     }

     var polyline = new GPolyline(line, "#ff0000", 3, 1);
     map.addOverlay(polyline);
});
}
//]]>

My php file is generating the following XML;

<?xml version="1.0" encoding="UTF-8" ?>
<points>
<point lng="-122.340141" lat="47.608940"/>
<point lng="-122.344391" lat="47.613590"/>
<point lng="-122.356445" lat="47.624561"/>
<point lng="-122.337654" lat="47.606365"/>
<point lng="-122.345673" lat="47.612823"/>
<point lng="-122.340363" lat="47.605961"/>
<point lng="-122.345467" lat="47.613976"/>
<point lng="-122.326584" lat="47.617214"/>
<point lng="-122.342834" lat="47.610126"/>
</points>

I have successfully worked through this; http://code.google.com/apis/maps/articles/phpsqlajax.html before attempting to customise the code.

Any pointers? Where am I go wrong?

A: 

You seem to be on the right track.

Your php script should accept a timestamp parameter, and should check if new points have been inserted in the database after that timestamp. If yes, it should return a response with the latest entry (or a list of entries after that timestamp, if you want to show a live trail as the vehicle moves).

On the client-side, you may want to initiate an AJAX request to the server-side script, either using normal or long polling, with the timestamp parameter of the last update.

When your AJAX request receives new information from the server, you would simply move your markers on the map. Then initiate a new AJAX request with the updated timestamp paramater.

Pseudocode-ish example using jQuery:

var lastUpdate = '2000/01/01 00:00:00';

function autoUpdate () {
    $.ajax({
       type: "GET",
       url: "phpsqlajax_genxml.php?last_update=" + lastUpdate,
       dataType: 'xml',
       success: function(xmlData) {

          // 1. Check if the xmlData is empty. If not we received 
          //    some fresh data.
          // 2. Update lastUpdate from the xmlData with the timestamp from 
          //    the server. Don't use JavaScript to update the timestamp, 
          //    because the time on the client and on the server will 
          //    never be exactly in sync.
          // 3. Move the markers on Google Map.

          // Relaunch the autoUpdate() function in 5 seconds.
          setTimeout(autoUpdate, 5000);
       }
    });
}
Daniel Vassallo
thanks for your reply. I have got the above bit of code working now, i had made some careless mistakes with it that i just couldn't see for looking. AJAX is another new to me (not that up on web tbh) but will look into it and try and apply what you've said.
arc