繁体   English   中英

从MySQL刷新Google地图标记

[英]Refresh google map markers from mysql

我可以从MySQL表中获取此信息并显示所需信息,但是我希望获得一些有关如何使用我当前拥有的代码每5秒刷新一次数据的帮助。

没有太多数据可显示,就像任何给定时间的5或8个标记一样。 我已经包含了用于提取数据的当前代码。 我对PHP / MySQL表示满意,但对于Google Maps来说还是很新的。

<script type='text/javascript'>
//This javascript will load when the page loads.
jQuery(document).ready( function($){

        //Initialize the Google Maps
        var geocoder;
        var map;
        var markersArray = [];
        var infos = [];

        geocoder = new google.maps.Geocoder();
        var myOptions = {
              zoom: 8,
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }
        //Load the Map into the map_canvas div
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        //Initialize a variable that the auto-size the map to whatever you are plotting
        var bounds = new google.maps.LatLngBounds();

        //Initialize the encoded string       
        var encodedString;

        //Initialize the array that will hold the contents of the split string
        var stringArray = [];

        //Get the value of the encoded string from the hidden input
        encodedString = document.getElementById("encodedString").value;

        //Split the encoded string into an array the separates each location
        stringArray = encodedString.split("****");

        var x;
     for (x = 0; x < stringArray.length; x = x + 1)
        {
            var addressDetails = [];
            var marker;
            //Separate each field
            addressDetails = stringArray[x].split("&&&");
            //Load the lat, long data
            var lat = new google.maps.LatLng(addressDetails[1], addressDetails[2]);
            var image = new google.maps.MarkerImage(addressDetails[3]);

            //Create a new marker and info window
            var marker = new google.maps.Marker({
                map: map,
                icon: image,
                position: lat,
                content: addressDetails[0]
            });



            //Pushing the markers into an array so that it's easier to manage them
            markersArray.push(marker);
            google.maps.event.addListener( marker, 'click', function () {
                closeInfos();
                var info = new google.maps.InfoWindow({content: this.content});
                //On click the map will load the info window
                info.open(map,this);
                infos[0]=info;
            });




           //Extends the boundaries of the map to include this new location
           bounds.extend(lat);
        }
        //Takes all the lat, longs in the bounds variable and autosizes the map
        map.fitBounds(bounds);



        //Manages the info windows
        function closeInfos(){
       if(infos.length > 0){
          infos[0].set("marker",null);
          infos[0].close();
          infos.length = 0;
       }
        }

});
</script>

</head>
<body>
<div id='input'>

    <?php


    //Initialize your first couple variables
    $encodedString = ""; //This is the string that will hold all your location data
    $x = 0; //This is a trigger to keep the string tidy

    //Now we do a simple query to the database

    // DB  INFO CONNECTION IS HERE AND WORKS

    $result = mysql_query("SELECT * FROM `ulocation` WHERE `ul_lat`!='' AND `ul_long`!='' AND `ul_onduty`='1'",$db1);

    //Multiple rows are returned
    while ($row = mysql_fetch_array($result, MYSQL_NUM))
    {
        //This is to keep an empty first or last line from forming, when the string is split
        if ( $x == 0 )
        {
             $separator = "";
        }
        else
        {
             //Each row in the database is separated in the string by four *'s
             $separator = "****";
        }
        $status='0';
        $cadd  = sql::getval('cal_address', 'call', "WHERE    `cal_id`='$row[14]'");
        $num  = sql::getval('cal_num', 'call', "WHERE `cal_id`='$row[14]'");
        $pcond  = sql::getval('cal_pcond', 'call', "WHERE `cal_id`='$row[14]'");
        $list="$num $cadd";
        //Saving to the String, each variable is separated by three &'s
        $encodedString = $encodedString.$separator.
        "<table border=0 width='350' height='20' class='maincolm' cellpadding=0   cellspacing=0><td align=left valign=top><h2></h2></td><tr><td width=100%><font size=3  face=arial><p><b>".$row[2].
        "</b>".
        "<br>Address: $list".
        "<br>Call Type: $pcond".
        "<br><br>Lat: ".$row[5].
        "<br>Long: ".$row[6].
        "</td></table>".
        "</p>&&&".$row[5]."&&&".$row[6]."&&&".$row[8]."&&&".$row[14];
        $x = $x + 1;
    }        
    ?>

    <input type="hidden" id="encodedString" name="encodedString" value="<?php echo   $encodedString; ?>" />




  <? echo "<body oncontextmenu=\"return false\" style=\"overflow: hidden; \"   topmargin=0 leftmargin=0 rightmargin=0 bottommargin=0>";


  <div id=\"map_canvas\"></div>
  </body>
  </html>";

  ?>

setInterval(function() {
  $.ajax({ url: '/my/site',
     data: {action: 'test'},
     type: 'post',
      success: function(output) {
         // change the DOM with your new output info
      }
  });
}, 300000); 

这将对您想要的任何页面运行ajax调用,每5分钟就会有您想要的任何发布数据。

为了更清晰:

假设我们有一个名为refreshComments.php的PHP页面,它执行以下操作:

$result = mysql_query("SELECT * FROM `comments` WHERE `articleId` = $_POST['articleId']");
while($data = mysql_fetch_array($result)) {
    $output[] = $data;
}
echo json_encode($output);

这是在做一个简单的查询,然后将其打印到JSON对象中

在我们的Javascript中,有一个名为scripts.js的页面可以执行以下操作:

setInterval(function() {
  $.ajax({ url: 'refreshComments.php',
   data: {articleId: '2'},
   type: 'post',
    success: function(output) {
      // dynamically append new comments
      console.log(output); // this just logs is
    }
  });
}, 300000); 

这个JS文件在做什么? 每隔300000毫秒(或5分钟),它将ajax请求发送到refreshComments.php。 refreshComments.php使用新数据的JSON进行响应,在success()函数中,我们使用该新数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM