dynamically drawing polylines on googlemaps using php/mysql

前端 未结 1 759
抹茶落季
抹茶落季 2021-01-07 05:32

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

1条回答
  •  伪装坚强ぢ
    2021-01-07 06:07

    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);
           }
        });
    }
    

    0 讨论(0)
提交回复
热议问题