【问题标题】:Move marker on google map with real time data使用实时数据在谷歌地图上移动标记
【发布时间】: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


【解决方案1】:

似乎是一个 var 范围界定问题 .... 尝试声明 var 映射和标记

<script>

// Declare the var for global scoping visibilty
var map;
var marker;

function initialize() {

  var myLatLng = new google.maps.LatLng( <?php echo $firstrow['latitude'].','. $firstrow['longitude'];?> ),
      myOptions = {
      zoom: 16,
      center: myLatLng,
      ......

【讨论】:

    【解决方案2】:

    将 movemarker() 更改为:

    function moveMarker(){
      setInterval( function(){ 
          $.get("point", function(data){
          var latlng= $.parseJSON(data);
          mymarker.setPosition( new google.maps.LatLng( {latlng} ) );
       });
    }, 30000 );
    

    对于“点”页面做:

    <?php
         //get last lat and long and send with under code
         echo json_encode($arr);
    ?>
    

    【讨论】:

      猜你喜欢
      • 2020-05-07
      • 2017-09-21
      • 2013-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      • 2015-04-08
      相关资源
      最近更新 更多