HTML5 GEOLOCATION - Deferred look up country code via multiple geolocation webapis

now working

by bizamajig

JavaScript

//////////////////////////////////////////////////

function lookupLatLongWithBrowserGeolocation() {
    var me = 'browser geolocation',
        dfr = $.Deferred();

    navigator.geolocation.getCurrentPosition(function(position) {
        console.log(me + ' resolving: ' + position.coords.latitude + ',' + position.coords.longitude);

        dfr.resolve({who: me, coords: position.coords});
    });

    return dfr;
}

// supports CORS and JSONP
function latLongToCountryWithGeonames(data) {
    var me = 'geonames',
        dfr = $.ajax({
            url: 'http://ws.geonames.org/countryCode',
            data: {
                lat: data.coords.latitude,
                lng: data.coords.longitude,
                type: 'JSON'
            },
            dataType: $.support.cors ? 'json' : 'jsonp'
        });

    dfr = dfr.pipe(function(results) {
        if (results.countryCode === 'XX') {
            console.log(me + ' rejecting: ' + results.countryCode);

            return $.Deferred().reject();
        } else {
            console.log(me + ' returning: ' + results.countryCode);

            return {who: me, code: results.countryCode};
        }
    });
    return dfr;
}

// supports CORS only
function lookupCountryWithHostipInfo() {
    if ($.support.cors) {  // doesn't support JSONP
        var me = 'hostip.info',
            dfr = $.ajax({
                url: 'http://api.hostip.info/get_json.php',
                dataType: 'json'
            });

        dfr = dfr.pipe(function(results) {
            if (results.country_code === 'XX') {
                console.log(me + ' rejecting: ' + results.country_code);

                return $.Deferred().reject();
            } else {
                console.log(me + ' returning: ' + results.country_code);

                return {who: me, code: results.country_code};
            }
        });
        return dfr;
    } else {
        return undefined;
    }
}

// supports JSONP only
function...