1

I am trying to get the country code of the user's current location using Geonames Servcie API. And I believe geonames service API will return me two letter country code instead of three letter country code and I need three letter country code. So for this, I have made the mapping between two letter country code and three letter country code.

Below is my code, and some how, my alert box is not working at all. I am pretty much sure I am missing something?

<html>
    <head>
        <title>Visitor's location</title>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>

    $(document).ready( function() {
        $.getJSON('http://ws.geonames.org/countryCode', {
            lat: position.coords.latitude,
            lng: position.coords.longitude,
            type: 'JSON'
        }, function(result) {
            alert('Country: ' + result.countryName + '\n' + 'Code: ' + result.countryCode);
        $('#newURL').attr('href','https://www.google.com&jobid='+result.countryCode);
        });
}); 


    </script>   
    </head>
    <body>

    <a id="newURL">URL</a>

    </body>
</html>

What wrong I am doing in my above code? Below is the error I am getting on the console.

Uncaught ReferenceError: position is not defined

arsenal
  • 23,366
  • 85
  • 225
  • 331
  • I think the Error is pretty clear about what's wrong, isn't it? – Manuel Görlich Aug 03 '13 at 19:53
  • Maybe you are not setting the position??? – Daniele Aug 03 '13 at 19:54
  • Yeah.. But I am not sure how I am going to get this position object without asking for the user to enable the physical location thing. Or is there any other way around for this? – arsenal Aug 03 '13 at 19:58
  • So, your actual question is how to find out the users current position, not how to get the country code informations/why your code isn't working? – Manuel Görlich Aug 03 '13 at 20:01
  • My main question is to get the user location and then extract country code from it using geonames. My code is not working because of the error I mentioned in my question. – arsenal Aug 03 '13 at 20:02
  • Like i said, you want to know how to get the users location, you already seem to know how to get the country name/code, since your code work in case it's fed with valid geocoordinates: http://jsfiddle.net/YSnKV/1/ So you should probably edit your question to ask what you really wan't to know and not just to tell what you already know, maybe with some info about what you already know about the subject of your question for example that you know about the HTML5 Geolocation API and need alternatives. – Manuel Görlich Aug 03 '13 at 20:06
  • There are two options, HTML5 Geolocation API, which would "ask the user to enable the location thingy", or using a service to geocode the users IP. Either way, you should rephrase the question and tell us what you're really trying to do! – adeneo Aug 03 '13 at 20:08
  • This might be helpful too: http://stackoverflow.com/questions/409999/getting-the-location-from-an-ip-address – Manuel Görlich Aug 03 '13 at 20:09
  • @adeneo: I edited my question. I need to use the second option you mentioned to me. using the service option. – arsenal Aug 03 '13 at 20:13
  • Added both options as an answer. – adeneo Aug 03 '13 at 20:15

1 Answers1

6

Using HTML5 Geolocation you could do :

$(document).ready( function() {
    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) {
                alert('Country: ' + result.countryName + '\n' + 'Code: ' + result.countryCode);
                $('#newURL').attr('href','https://www.google.com&jobid='+result.countryCode);
            });
        });
    }
}); 

FIDDLE

Or you could use a service:

$(document).ready( function() {
    $.getJSON("http://freegeoip.net/json/", function(result){
        alert('Country: ' + result.country_name + '\n' + 'Code: ' + result.country_code);
        $('#newURL').attr('href','https://www.google.com&jobid='+result.country_code);
    });
}); 

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • I cannot use geonames api for the service part as well? – arsenal Aug 03 '13 at 20:17
  • I don't really know geonames that well, but if they offer a IP lookup geolocation service, you can of course use that. If they don't offer anything like that, there's no way to get the coordinates, and most services that gives you the coordinates, like the one I used above, will also give you the countrycode and countryname, so it's not neccessary to look it up once more ? – adeneo Aug 03 '13 at 20:25