0

我正在尝试查找用户在哪个国家/地区浏览的位置含义。我需要为此使用 HTML 和 Javascript。我在 stackoverflow 上看到过各种帖子,人们建议使用不同的 API。我最感兴趣的是任何免费的 API,它可以给我三个字母的国家代码而不是两个。任何其他 API 对我来说都很好,我不需要使用 IPInfo,我也可以使用地理名称。

而在找到用户的国家代码后,我需要做一个这样的网址,假设国家代码是美国,那么应该是这样的——

some_url&countryCode=USA

如果 countryCode 是 IND,那么应该是这样的——

some_url&countryCode=IND

我有下面的代码,可以找到 currentLocation 但 countryCode 只有两个字母。我需要三个字母的国家代码,然后相应地制作网址。

<html>
    <head>
        <title>Get web visitor's location</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

    <script>

    $.get("http://ipinfo.io", function (response) {
    $("#ip").html("IP: " + response.ip);
    $("#address").html("Location: " + response.city + ", " + response.region);
    $("#details").html(JSON.stringify(response, null, 4));
    }, "jsonp");

    </script>

    </head>
    <body>
<hr/>
<div id="ip"></div>
<div id="address"></div>
<hr/>Full response: <pre id="details"></pre>

<a href="url&country=countryCode">url</a>

    </body>
</html>

谁能帮我这个?

4

2 回答 2

1

ipinfo.io提供三个字母的国家代码。您必须手动生成一个数组:

 var threeLtrCC.US = 'USA';
 var threeLtrCC.IN = 'IND';
 ...
 $.get("http://ipinfo.io", function (response) {
     var cc = threeLtrCC[response.country];
     $('#newURL').text('some_url?countryCode=' + cc);
     $('#newURL').attr('href','some_url?countryCode=' + cc);         
     $("#details").html(JSON.stringify(response, null, 4));
 }, "jsonp");

然后response.country用作从自制数组中查找值的键。

HTML

<a id="newURL"></a>
于 2013-08-03T04:56:00.460 回答
-1

我不认为 ipinfo 提供三个字母的国家代码。我们必须使用包含所有可用国家代码的数组并匹配三个字母的国家代码。

http://www.worldatlas.com/aatlas/ctycodes.htm

希望能帮助到你

于 2013-08-03T05:01:22.197 回答