Friday, 16 December 2011

Debugging kml & js in google earth

Today I was mainly trying to create a test case of a jquery AJAX request failing from a Google Earth info window bubble. I found that actually the problem didn't seem to be with jquery, but Google Earth. The status of the XMLHttpRequest object was returning 0 on the server's response, when the actual HTTP status code returned by the server was 200. Since jquery checks the status, and sees that it isn't 200, it thinks the request failed and fires the error handler.

I also tried using jsonp in case it was a cross domain issue (I'm not sure whether contacting your server from a Google Earth info window bubble counts as cross domain or not). Anyway, this didn't make any difference, and I still got a status of 0, causing jquery to fire the error handler and not the success handler.

Here's a funny email I got from DXO today, it seems they are having a special offer throughout December where they have increased the price of DXO Optics Pro from £89 to £135:

Link to the email here, though they might correct the image on the link. I'm guessing that it is meant to be the other way round (normally £135, special offer £89).

And here's another weird thing, searching for help on the jquery forum told me there were lots of results, but wouldn't let me see any of them:

Eventually I worked out how to get AJAX working properly in Google Earth Info Window bubbles - add an error handler that checks everything okay, and the error is just GE being buggy, then parses the JSON response and fires the success handler, e.g.

$.ajax({ "url": dk.WWW+'/AJAXandDBFuncs.xhtml?getImgData='+img_id+'&format=json',
                    "dataType": "json",
                    //hack to get around GE intermittently reporting status as 0 when is actually 200 OK
                    "error" : function(jqXHR, textStatus, errorThrown){
                        if(!errorThrown && jqXHR.status === 0 && jqXHR.responseText){

The other alternative would be to use the "complete" property of the object you pass to jquery's ajax method. complete fires when the request has completed, regardless of whether the result was an error or success.

After getting that working though, I found that image links weren't working (they are meant to zoom into where the image is and load its info bubble). I saw that the request was returning kml, that looked okay. I tried to validate the KML, but didn't get very far with that.

I downloaded the feedvalidator software from, but when I ran it, it just seemed to get stuck in the background.

So next I tried to find something for Komodo Edit. After some googling and testing I found the following command to validate an xml file against an xsd schema. (The KML schema is here). In Komodo Edit I went to Tools > Run Command... and ran

xmllint %F --noout --schema /home/djeyewater/Desktop/kml21.xsd

That gave me

/home/djeyewater/Desktop/Text-1.txt:3: element kml: Schemas validity error : Element '{}kml': No matching global declaration available for the validation root.

Hmm... no idea what that means.

Then, when I was writing the above, I realised that the reason the feedvalidator program probably went into the background and didn't work was because it works on a specified URL, and the URL I input had an & in it. So I retried with the URL quoted, which gave me the error

line 25, column 49: Invalid altitudeMode

I checked it up, and I had clampedToGround, when it should be clampToGround. I don't think that will be the problem causing my links not to work, but at least I can correct it. the feedvalidator program is also better than using the xmllint command in Komodo Edit as it works on URLs rather than files. So I can point the feedvalidator program to my KML generating script rather than having to run the KML generating script manually and saving the results in a file.

Rather annoyingly, the image links work on a static KML file, even though they are exactly the same as the ones that don't work on the dynamic file. Also, the links do work on the dynamic file on my live website (which is the same as my testing website)! So I am going to have leave debugging this problem until tomorrow I think.

No comments: