我在Google地圖上自動顯示我的gps座標時遇到問題。當我實現地圖是靜態的時候一切正常,但現在我動態地嘗試它。這意味着當新的GPS座標將被保存到數據庫時,地圖應該自動刷新。自動查詢MySQL並使用AJAX輸出結果
我在一個.php文件中實現了一切。爲了顯示與COORDS的地圖,用戶必須選擇一個條目,鏈接到一個條目是這樣的 - >http://trackmyrun1.at/index.php?page=show_runs&data1=get&data2=1
<?php
$result = mysql_query("SELECT * FROM fifa_gps WHERE run_id=".$_GET['data2']."");
while ($line = mysql_fetch_array($result)) {
$cords[] = "new google.maps.LatLng(" . $line['lat'] . ", " . $line['longt'] . "),";
}
$cord_start = explode("(", $cords[0]);
$cord_pos_start = explode(")", $cord_start[1]);
$cord_end = explode("(", $cords[count($cords)-1]);
$cord_pos_end = explode(")", $cord_end[1]);
echo $sys->overall_distance($_GET['data2']);
?>
<div id="map-canvas" style="height:400px"/>
<script type="text/javascript">
var seconds = 1;
var divid = "map-canvas";
var url = "http://localhost/alt/FIFA-Europaliga/index.php?page=show_runs&data1=get&data2=<?php echo $_GET['data2']; ?>";
function refreshdiv(){
// The XMLHttpRequest object
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}
// Timestamp for preventing IE caching the GET request
fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}
var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;
// The code...
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
setTimeout('refreshdiv()',seconds*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}
// Start the refreshing process
var seconds;
window.onload = function startrefresh(){
setTimeout('refreshdiv()',seconds*1000);
}
<!--
refreshdiv();
// -->
</script>
<script type="text/javascript">
function initialize() {
var myLatLng = new google.maps.LatLng(<?php echo $cord_pos_start[0]; ?>);
var mapOptions = {
zoom: 15,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
var flightPlanCoordinates = [
<?php echo implode('', $cords); ?>
];
var image_start = 'http://labs.google.com/ridefinder/images/mm_20_green.png';
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title:"Start",
icon: image_start
});
var image_end = 'http://labs.google.com/ridefinder/images/mm_20_red.png';
var endMarker = new google.maps.Marker({
position: new google.maps.LatLng(<?php echo $cord_pos_end[0]; ?>),
map: map,
title:"Ende",
icon: image_end
});
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
strokeColor: "#FF0000",
strokeOpacity: 0.5,
strokeWeight: 5
});
flightPath.setMap(map);
}
initialize();
</script>
....
第一刷新我看看地圖就可以了後一個刷新我的路徑前得到這個錯誤:
Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Alt\FIFA-Europaliga\content\show_runs.php on line 4
Notice: Undefined variable: cords in C:\xampp\htdocs\Alt\FIFA-Europaliga\content\show_runs.php on line 8
Notice: Undefined offset: 1 in C:\xampp\htdocs\Alt\FIFA-Europaliga\content\show_runs.php on line 9
Notice: Undefined variable: cords in C:\xampp\htdocs\Alt\FIFA-Europaliga\content\show_runs.php on line 11
有人能幫助我嗎?
我將index.php文件中的我連接到數據庫。文件show_runs包含在index.php中,然後打開querry。 – user2326712 2013-05-02 19:05:01
然後唯一可能的答案是您的查詢失敗。你可以在數據庫服務器上運行查詢嗎?我的猜測是你會得到一個錯誤。 – mbarlocker 2013-05-02 19:09:26
第一次刷新之前,我在地圖上找到正確的地圖,第二次刷新則拋出錯誤 – user2326712 2013-05-02 19:11:55