Hello everyone. I am recently developping a web application based on
google map. My plan is to retrieve aircraft lat/lon from mysql
database via php and display on google map. I follow the article
http://code.google.com/apis/maps/articles/phpsqlajax_v3.html and it
works fine. My question is how to update the marker position without
reloading the all page. I try to setTimeout, but it doesnot work.
The following is my javascript:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0px; padding: 0px }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="
http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript" src="prototype.js"></script>
<script type="text/javascript">
//<![CDATA[
// Creating a aircraftIcon
var aircraftIcon = new google.maps.MarkerImage('img/
Airport.png',null,null,null,new google.maps.Size(30,30));
function initialize() {
var latlng = new google.maps.LatLng(53.33, 9.59);
var myOptions = {
zoom: 7,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new
google.maps.Map(document.getElementById("map_canvas"),
myOptions);
downloadUrl("phpsqlajax_genxml.php",function(data){
var xml = data.responseXML;
var aircraft =
xml.documentElement.getElementsByTagName("Aircraft");
for(var i=0;i<aircraft.length;i++)
{
var ICAO = aircraft[i].getAttribute("ICAO");
var point = new
google.maps.LatLng(parseFloat(aircraft[i].getAttribute("latitude")),parseFloat(aircraft[i].getAttribute("longtitude")));
var marker = new google.maps.Marker({
map:map,
position:point,
icon: aircraftIcon
});
}
});
}
// load the xml file into the page, XMLHttpRequest is used to
retrieve a file that reside on the same domain as the requesting
webpage
// url specifies the path to either your xml file or the PHP script
that generates the file
function downloadUrl(url,callback)
{
var request = window.ActiveXObject ? new
ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest;
request.onreadystatechange = function(){
if(request.readyState==4){
callback(request,request.status);
}
};
request.open('GET',url,true);
request.send(null);
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>