javascript 如何使用 geonames 服务 api 而非 html5 位置对象来获取国家/地区代码信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18036690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:33:19  来源:igfitidea点击:

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

javascriptjqueryhtmlgeolocationgeonames

提问by arsenal

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.

我正在尝试使用 Geonames Servcie API 获取用户当前位置的国家/地区代码。我相信 geonames 服务 API 会返回两个字母的国家代码而不是三个字母的国家代码,我需要三个字母的国家代码。为此,我做了两字母国家代码和三字母国家代码之间的映射。

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

Uncaught ReferenceError: position is not defined

回答by adeneo

Using HTML5 Geolocation you could do :

使用 HTML5 地理定位,您可以执行以下操作:

$(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

小提琴