【发布时间】:2016-07-20 18:11:09
【问题描述】:
数据库表每 20 秒更新一次新数据。我不想用新的经纬度移动地图上的标记。我尝试每 30 秒运行一次查询以获取表的最后一条记录并更新标记的位置。地图加载,标记显示并且javascript函数也被调用。但是标记没有移动。请帮忙..
<?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 getNewLatLang(){
$sql="SELECT * FROM temperature_details ORDER BY id DESC LIMIT 1 ";
$result=mysql_query($sql);
$latLang = mysql_fetch_assoc($result);
echo $latLang['latitude'].','. $latLang['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( <?php echo $firstrow['latitude'].','. $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( <?php echo getNewLatLang(); ?> ) );
map.panTo( new google.maps.LatLng( <?php echo getNewLatLang(); ?>));
console.log("I am working");
}, 30000 );
};
initialize();
</script>
【问题讨论】:
-
我删除了标记并在类似情况下设置了一个新标记。解决方法,但可以完成工作。
-
@Max 请把代码..
-
现在不能。在路上多呆了几天,只限电话。对不起
-
我试过了。但它仍然在原地不动。
标签: javascript php mysql google-maps google-maps-api-3