最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get country code information using geonames service api not the html5 location object thing - Stack Overflow

programmeradmin1浏览0评论

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=".8.3/jquery.min.js"></script>
    <script>

    $(document).ready( function() {
        $.getJSON('', {
            lat: position.coords.latitude,
            lng: position.coords.longitude,
            type: 'JSON'
        }, function(result) {
            alert('Country: ' + result.countryName + '\n' + 'Code: ' + result.countryCode);
        $('#newURL').attr('href',';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

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./ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script>

    $(document).ready( function() {
        $.getJSON('http://ws.geonames/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.&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

Share Improve this question edited Aug 3, 2013 at 20:11 arsenal asked Aug 3, 2013 at 19:51 arsenalarsenal 24.2k91 gold badges230 silver badges334 bronze badges 10
  • I think the Error is pretty clear about what's wrong, isn't it? – Manuel Görlich Commented Aug 3, 2013 at 19:53
  • Maybe you are not setting the position??? – Daniele Commented Aug 3, 2013 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 Commented Aug 3, 2013 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 Commented Aug 3, 2013 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 Commented Aug 3, 2013 at 20:02
 |  Show 5 more ments

1 Answer 1

Reset to default 6

Using HTML5 Geolocation you could do :

$(document).ready( function() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            $.getJSON('http://ws.geonames/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.&jobid='+result.countryCode);
            });
        });
    }
}); 

FIDDLE

Or you could use a service:

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

FIDDLE

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论