gpt4 book ai didi

javascript - 从 MySQL 数据库更新 Google map 上标记 "Live"的更改坐标

转载 作者:行者123 更新时间:2023-11-29 02:43:06 24 4
gpt4 key购买 nike

我正在尝试找到一种解决方案,使我能够在 Google map 上“实时”绘制许多移动物体的 GPS 坐标。
每个对象的 GPS 坐标将不断更新到 MySQL 数据库中,我想每三秒钟读取一次每个更新的坐标,并在 Google map 上重新绘制标记坐标,而无需用户刷新页面.
我有一个从数据库接收数据的代码,使用 setInterval 每 3 秒读取一次数据库信息。
我的问题在这里: 每个 setinterval 加载未读取新的数据库数据。这意味着数据库信息被读取一次,不会收到任何新信息。我该如何解决这个问题?

<?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>

最佳答案

我可以解决它。

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

在上面的代码中,“点”是一个页面,它接收坐标作为查询使用 Laravel 框架

<?php
use App\MoteharekModel;
$lt = MoteharekModel::orderby('id','desc')->first();
echo json_encode($lt);
?>

关于javascript - 从 MySQL 数据库更新 Google map 上标记 "Live"的更改坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47413171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com