Get local currency code from geolocation

Uses YQL and mayavps.com/download/18/currencies.csv for country code to currency code mapping

by hippietrail

JavaScript

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        $.getJSON('http://ws.geonames.org/countryCode', {
            lat: position.coords.latitude,
            lng: position.coords.longitude,
            type: 'JSON'
        }, function(result) {
            $.getJSON('http://query.yahooapis.com/v1/public/yql', {
                q: 'select col1, col4 from csv where url = ' + "'http://mayavps.com/download/18/currencies.csv'",
                format: 'json'
            }, function(data) {
                var currencyCode = null;
                for (var i = 2; i < data.query.results.row.length; i++) {
                    if (data.query.results.row[i].col4 == result.countryCode) {
                        currencyCode = data.query.results.row[i].col1;
                        break;
                    }
                }
                $("body").append("<div>").text('* ' + result.countryCode + ' → ' + currencyCode);
            });
        });
    });
}