Nav links

Thursday, 2 July 2009

Double the fun in Country Bumpkin

Lovely satellite imageI've rewritten the Country Bumpkin quiz to use a more reliable map provider, and whilst knee-deep in code I took the opportunity of updating the quiz mechanics to make it enjoyable to play (I hope). Instead of asking ten questions and getting a total score from them you now only accumulate a score until you make a mistake. In addition the questions are asked in a rough order of increasing difficulty.



At this stage there's an intermittent bug that manifests in IE8 as a script error. Just refresh the page if you see it, and I'll fix it as soon as I can.



Technical notes



Although the OpenLayers documentation is relatively complete it is difficult to use because it is scattered over various sites:

If these could be integrated, such as is done on the PHP site then it would ease the load on developers.




Whilst debugging within Firefox using Firebug the page would regularly hang on loading, leaving a blank page with the location bar saying waiting for gg.google.com. Eventually I discovered this was a bug in Firebug 1.3.3 relating to the Google Maps API. It has been fixed in more recent Firebugs.



Another error that showed a similar blank page was evident in Internet Explorer 8. This was an "HTML Parsing Error: Unable to modify the parent container element before the child element is closed (KB927917)". I believe this occurrence has been greatly reduced by deferring execution of the quite lengthy Javascript within the page until the HTML has been generated, by using the "defer" tag:
<script src="script.js" type="text/javascript" defer="defer">. However, this error still occurs occasionally, so I may look at rearranging the elements within the page itself, as suggested by Microsoft.



When this quiz was originally written in 2007 OpenLayers did not allow the accurate overlaying of vectors in lat/long standard projection onto commercial maps, such as Google Maps. Since then this has been enabled using the Spherical Mercator projection. Although that page pointed me in the right direction, I still needed a bit of trial and error to get it working. The end result of this is below, showing the crucial element of transforming each feature in place:




        var fromProj = new OpenLayers.Projection("EPSG:4326");

        var toProj = new OpenLayers.Projection("EPSG:900913");



        var vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry");

        

        var wktData = '<?php echo($country["WKT_GEOMETRY"]); ?>';

        wkt = new OpenLayers.Format.WKT();



        var features = wkt.read(wktData);

        var smFeatures = [];

        var bounds;



        if(features) {

            if(features.constructor != Array{

                features = [features];

            }

            for(var i=0; i<features.length; ++i) {

                smFeatures.push(features[i].geometry.transform(fromProj, toProj));

                features[i].geometry = smFeatures[i];

                if (!bounds) {

                    bounds = smFeatures[i].getBounds();

                } else {

                    bounds.extend(smFeatures[i].getBounds());

                }

                

            }

            vectorLayer.addFeatures(features);

        } else {

           alert('Country in turmoil (Bad WKT)');

        }