简体   繁体   English

如何使用地理名称服务API而不是html5位置对象来获取国家/地区代码信息

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

I am trying to get the country code of the user's current location using Geonames Servcie API. 我正在尝试使用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. 而且我相信地名服务API将返回两个字母的国家代码(而不是三个字母的国家代码),并且我需要三个字母的国家代码。 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

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 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用国家代码或国家名称获取国家拨号代码? - How to get country dialling code using country code or country name? 如何使用HTML5 Javascript和Phonegap获取设备信息 - How to get Device Information Using HTML5 Javascript & Phonegap 如何使用webkitdirectory获取目录大小,HTML5文件api - How to get Directory size, HTML5 file api, using webkitdirectory 如何在react js中使用IP获取国家代码和国家名称 - How to get country code and Country name using IP in react js 如何使用html / html5中的用户输入获取完整的地理位置详细信息? - How to get full geo location details using user input in html/html5? 是否可以在Manifest v2下使用HTML5地理位置API将经/纬度转换为国家/地区? - Possibile to convert lat/long to country using HTML5 geolocation API under Manifest v2? HTML5如何获取被单击对象的ID? - HTML5 how to get the id of the object that is clicked on? 如何使用 JavaScript 从 BING 地图 API 获取地址(城市、州、国家、邮政编码)? - How to get address(city,state, country, zip code) from BING map API using JavaScript? HTML5文件API:在FileReader回调中获取File对象 - HTML5 File API: get File object within FileReader callback 如何从 Google Places API 获取国家/地区代码 - How to get the country code from Google Places API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM