[英]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);
});
});
}
});
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);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.