Click here to Skip to main content
14,031,365 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I am able to create and display map with location(lat,long) data from database.
"setInterval" used but the map and marker is not changing.
I need to track GPS device with updated lat,long from the database.
upon new data(lat,long) read from the database, the marker should animate to the new location.
objective of the code is to show the realtime position of GPS device on the map.

I am putting my present code below..

please help me,anybody.

Thanks.

What I have tried:

var map;
           var marker;

           function LoadMap()
           {
                <?php include 'latlongmap.php';?>
                SetMap(fetchedlatlong);
           }


           function SetMap(latlong)
           {
              map = L.map('map').setView(latlong, 12);

              mapLink = 'OpenStreetMap';
              L.tileLayer(
           'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
           attribution: 'Map data © ' + mapLink,
           maxZoom: 18,
           }).addTo(map);
               marker = L.marker(latlong).addTo(map);
               layer.bindPopup('LatLng: ' + layer.getLatLng()).openPopup();
           }



           setInterval(LoadMap,5000);
Posted
Updated 14-Feb-19 3:47am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Quote:
function LoadMap()
{
    <?php include 'latlongmap.php';?>
    SetMap(fetchedlatlong);
}

That code will render the output of the latlongmap.php file ONCE, when the page is first requested. That output will not change until you reload the page.

If you want to load data from the server without reloading the page, then you need to make an AJAX request.
Getting Started - Developer guides | MDN[^]
fetch documentation[^]
   
Comments
ramen79 24-Feb-19 15:33pm
   
Thanks Richard for your reply.
I have tried with ajax, it is polling latlon from mysql every 5 secs as i set(I have checked with alert), but no luck yet ,the map layer/marker position not getting updated..

Below is the code I have tried...

function fetchdata(){
$.ajax({
url: 'getLatLong.php',
type: 'post',
dataType: 'json',
success: function(response){
// Perform operation on the return value
// alert(response);
var latlon = response
//alert(latlon)
//setMap(latlon)
var myMap = setTimeout(setMap,3000)
function setMap(){
var map = new L.map('map' , {center: latlon,
zoom: 14})
// Creating a Layer object
var layer = new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png')
// Adding layer to the map
map.addLayer(layer)
var marker = L.marker(latlon).addTo(map)
layer.bindPopup('LatLng: ' + layer.getLatLng()).openPopup()
}

},
complete:function(data){
setTimeout(fetchdata,5000);
}
});
}


$(document).ready(function(){
setInterval(fetchdata,5000);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web02 | 2.8.190419.4 | Last Updated 14 Feb 2019
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100