-2
下面的java腳本正在從PHP
下載XML(請參閱第二個XML片段),但是當它完成後,downloadUrl
將顯示XML數據(而不是帶有標記的谷歌地圖)數據流。在屏幕更改之前,我可以在地圖上看到標記,所以我很確定PHP
中的XML正確地發送到了Java腳本,我只是不確定爲什麼PHP
本身在病房之後運行。Java Script PHP XML downloadUrl
function load() {
var infoWindow = new google.maps.InfoWindow;
initMap();
// Change this depending on the name of your PHP file
downloadUrl("gpsx.php", function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
var keyid = markers[i].getAttribute("keyid");
console.log(keyid);
var deviceid = markers[i].getAttribute("deviceid");
var accessid = markers[i].getAttribute("accessid");
var datex = markers[i].getAttribute("date");
var timex = markers[i].getAttribute("time");
var point = new google.maps.LatLng(
parseFloat(markers[i].getAttribute("lng")),
parseFloat(markers[i].getAttribute("lat")));
var sats = markers[i].getAttribute("sats");
var speedm = markers[i].getAttribute("speedm");
var histon = markers[i].getAttribute("histon");
var histstart = markers[i].getAttribute("histstart");
var histon = markers[i].getAttribute("hist_many");
var html = "<b>" + datex + "</b> <br/>" + timex;
var dtstr = "Satellites " + sats + " Speed " + speedm + " D/T: " + datex + " " + timex;
var image = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=" + i + "|FF0000|000000";
var marker = new google.maps.Marker({map: map,position: point,title: dtstr,icon: image });
bindInfoWindow(marker, map, infoWindow, html);
}
});
}
function bindInfoWindow(marker, map, infoWindow, html) {
google.maps.event.addListener(marker, 'click', function() {
infoWindow.setContent(html);
infoWindow.open(map, marker);
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 39.833, lng: -98.585},
zoom: 12
});
}
<?php
// session_start();
$dom = new DOMDocument("1.0","UTF-8");
$node = $dom->createElement("markers");
$parnode = $dom->appendChild($node);
$servername ="localhost";
$username = "username";
$password = "password";
$dbname = "gps";
$dev = $_SESSION["devid"];
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
$sql = "SELECT keyID , deviceID, accessID, lng, lat, date, time, pstn,sats,speedmph FROM hist_data WHERE deviceID='" . $dev . "' ORDER BY ABS(pstn);";
$result = mysqli_query($conn,$sql);
$nums = mysqli_num_rows($result);
if ($nums > 0) {
header("Content-type: text/xml");
while($row = mysqli_fetch_array($result)) {
$node = $dom->createElement("marker");
$newnode = $parnode->appendChild($node);
$newnode->setAttribute("keyid", $row['keyID']);
$newnode->setAttribute("deviceid",$row['deviceID']);
$newnode->setAttribute("accessid",$row['accessID']);
$newnode->setAttribute("lng",$row['lng']);
$newnode->setAttribute("lat",$row['lat']);
$newnode->setAttribute("date",$row['date']);
$newnode->setAttribute("time",$row['time']);
$newnode->setAttribute("sats",$row['sats']);
$newnode->setAttribute("speedm",$row['speedmph']);
$newnode->setAttribute("histon",$hist_on);
$newnode->setAttribute("histstart",$hist_start);
$newnode->setAttribute("histmany",$hist_many);
}
} else {
echo "0 results";
}
echo $dom->saveXML();
?>
<markers>
<marker keyid="94454" deviceid="1e002c001547353236343033" accessid="1fa33427cd2b9f8beb75b6e156e545d5b20e33e7" lng="29.635313" lat="-95.773415" date="2016-08-15" time="02:02:05.722Z" sats=" 0.000000" speedm=" 0.552374" histon="Y" histstart="20" histmany="9"/>
<marker keyid="3339" deviceid="1e002c001547353236343033" accessid="1fa33427cd2b9f8beb75b6e156e545d5b20e33e7" lng="29.635300" lat="-95.773338" date="2016-08-15" time="02:17:05.644Z" sats=" 0.000000" speedm=" 0.621421" histon="Y" histstart="20" histmany="9"/>
<marker keyid="2552" deviceid="1e002c001547353236343033" accessid="1fa33427cd2b9f8beb75b6e156e545d5b20e33e7" lng="29.635216" lat="-95.773270" date="2016-08-15" time="02:32:05.787Z" sats=" 0.000000" speedm=" 0.448804" histon="Y" histstart="20" histmany="9"/>
<marker keyid="95112" deviceid="1e002c001547353236343033" accessid="1fa33427cd2b9f8beb75b6e156e545d5b20e33e7" lng="29.635225" lat="-95.773315" date="2016-08-15" time="02:47:05.711Z" sats=" 0.000000" speedm=" 0.218648" histon="Y" histstart="20" histmany="9"/>
<marker keyid="32275" deviceid="1e002c001547353236343033" accessid="1fa33427cd2b9f8beb75b6e156e545d5b20e33e7" lng="29.635281" lat="-95.773399" date="2016-08-15" time="03:02:05.735Z" sats=" 0.000000" speedm=" 0.782530" histon="Y" histstart="20" histmany="9"/>
<marker keyid="41199" deviceid="1e002c001547353236343033" accessid="1fa33427cd2b9f8beb75b6e156e545d5b20e33e7" lng="29.635210" lat="-95.773216" date="2016-08-15" time="03:17:05.763Z" sats=" 0.000000" speedm=" 0.517851" histon="Y" histstart="20" histmany="9"/>
<marker keyid="33761" deviceid="1e002c001547353236343033" accessid="1fa33427cd2b9f8beb75b6e156e545d5b20e33e7" lng="29.635267" lat="-95.773315" date="2016-08-15" time="03:32:05.780Z" sats=" 0.000000" speedm=" 0.368249" histon="Y" histstart="20" histmany="9"/>
</markers>
你能解釋一下你的意思嗎'不確定爲什麼PHP本身在病房後面運行。和'request.onreadystatechange = doNothing;'的目的是什麼? – RamRaider
基本上,函數load()運行,它顯示所有標記的谷歌地圖約1/2秒。之後,我將XML顯示在屏幕上。我添加了一個exit();調用PHP代碼的結尾,現在我沒有在地圖顯示之後獲取屏幕上顯示的XML。 – user3308131
你在哪裏/何時調用'load'函數? – geocodezip