The latest update to the Maps API (v2.74) includes better error checking for GDownloadURL
. Previously, if you sent a request to download a file with GDownloadURL
and the server timed out the request, the onload
function might never be called. Now, GDownloadURL
will enter the onload
function and return null for the data parameter and a -1
as the response status code parameter.
In the example code below, we process the data if the request was successful, and then alert messages if the request has timed out or errored. In your own applications, you may decide to do more or less error checking, depending on your needs.
// Download the data in data.xml and load it on the map. GDownloadUrl("data.xml", function(data, responseCode) { // To ensure against HTTP errors that result in null or bad data, // always check status code is equal to 200 before processing the data if(responseCode == 200) { var xml = GXml.parse(data); var markers = xml.documentElement.getElementsByTagName("marker"); for (var i = 0; i < markers.length; i++) { var point = new GLatLng(parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng"))); map.addOverlay(new GMarker(point)); } } else if(responseCode == -1) { alert("Data request timed out. Please try later."); } else { alert("Request resulted in error. Check XML file is retrievable."); } });
View example (async.htm). This example uses the external XML data file data.xml.