gpt4 book ai didi

mysql - 是否可以立即刷新 Google map ?

转载 作者:行者123 更新时间:2023-11-30 00:14:08 25 4
gpt4 key购买 nike

我可以从我的 MySQL 表中提取此信息并显示我需要的内容,但我需要一些帮助来了解如何使用我当前的代码每 5 秒左右刷新此数据。

没有太多数据可显示,就像任何给定时间的 5 或 8 个标记一样。我已经包含了我用来提取数据的当前代码。我对 PHP/MySQL 比较熟悉,但对 Google map 还很陌生。我尝试添加代码来刷新谷歌地图

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

但还是不行。我的代码有什么问题吗?mysql数据库有更新时是否可以从mysql获取新数据?

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?  sensor=true" /></script>

<script type='text/javascript'>
//This javascript will load when the page loads.
jQuery(document).ready( function($){
setInterval(function() {
$.ajax({ url: '/localhost/my/site.php',
data: {action: 'test'},
type: 'post',
success: function(output) {
alert('hi');
// change the DOM with your new output info
}
});
}, 5000);

//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>";
?>

任何帮助将不胜感激

最佳答案

根据此处的链接

Live update notification on database changes MYSQL PHP

How can I view live MySQL queries?

我的结论是建议使用轮询,它每隔 X 秒调用一次数据库来更新标记信息。

另外,有一篇文章提到了在mysql中使用触发器,你也可以看看。

鉴于mysql不通知

作为对您的目的的建议。如果您想从标记刷新数据,例如标记 onclick 事件,您可以从那里进行 db 调用来刷新标记。这就是我为即时标记 onclick 所做的事情

希望对你有帮助

关于mysql - 是否可以立即刷新 Google map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23796940/

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