I am trying to find a solution that will allow me to plot the GPS coordinates of many moving objects "live" on a Google Map.
The GPS coordinates of each object will constantly be updating into a MySQL database, and I want to read each updated coordinate, once every three of seconds, and re-plot the marker coordinates on the Google Map, without the user having to refresh the page.
I have a code that receives data from the database,and Database information is read every 3 seconds with setInterval.
My problem is here: With each setinterval load
New database data is not read.that's mean
Database information is read once and does not receive any new information.
How can I fix this problem?
<?php
include "db_connect.php";
$sql="SELECT * FROM temperature_details ORDER BY id DESC LIMIT 1 ";
$result=mysql_query($sql);
$firstrow = mysql_fetch_assoc($result);
function getNewLat(){
$sql="SELECT * FROM temperature_details ORDER BY id DESC LIMIT 1 ";
$result=mysql_query($sql);
$lat = mysql_fetch_assoc($result);
return $lat['latitude'];
}
function getNewLang(){
$sql="SELECT * FROM temperature_details ORDER BY id DESC LIMIT 1 ";
$result=mysql_query($sql);
$Lang = mysql_fetch_assoc($result);
return $Lang['longitude'];
}
?>
<script src="http://maps.googleapis.com/maps/api/js?key=API_KEY&libraries=geometry"></script>
<div id="map" style="height:500px;width:100%;" ></div>
<script>
function initialize() {
var myLatLng = new google.maps.LatLng({lat:<?php echo .$firstrow['latitude'];?>, lng: <?php echo .$firstrow['longitude'];?>}),
myOptions = {
zoom: 16,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
},
map = new google.maps.Map( document.getElementById( 'map' ), myOptions ),
marker = new google.maps.Marker( {position: myLatLng, map: map} );
marker.setMap( map );
moveMarker( map, marker );
}
function moveMarker( map,marker ) {
setInterval( function(){
marker.setPosition(new google.maps.LatLng( {lat:<?php echo getNewLat();?>, lng: <?php echo getNewLang();?>} ));
map.panTo( new google.maps.LatLng( {lat:<?php echo getNewLat();?>, lng: <?php echo getNewLang();?>} ));
console.log("I am working");
}, 3000 );
};
initialize();
</script>
i can to resolve it.
function moveMarker(){
setInterval( function(){
$.get("point", function(data){
var mydata= $.parseJSON(data);
var art1 = mydata.lat; // <----------- access the element
var art2 = mydata.lng;
marker.setPosition( new google.maps.LatLng( {lat:art1 , lng: art2} ) );
map.panTo( new google.maps.LatLng( {lat:art1 , lng: art2} ));
});
}, 3000 );
In the above code,"point" is a page ,It receives the coordinates as a query with laravel framwork
<?php
use App\MoteharekModel;
$lt = MoteharekModel::orderby('id','desc')->first();
echo json_encode($lt);
?>