c# - 你如何在 C# 中获取用户的国家/地区名称?

标签 c# javascript jquery html geolocation

我目前正在使用地理定位来获取用户的国家/地区名称。这是我的代码:

navigator.geolocation.getCurrentPosition(function (pos) {
    var latlng = pos.coords.latitude + "," + pos.coords.longitude;
    var apiurl = 'http://maps.google.com/maps/geo?output=json&sensor=false&q=' + latlng;
    var yqlqry = encodeURIComponent('select * from json where url="' + apiurl + '"');
    var yqlurl = 'http://query.yahooapis.com/v1/public/yql?q=' + yqlqry + '&format=json&callback=?';
    $.getJSON(yqlurl, function (data) {
        var countryName = data.query.results.json.Placemark.AddressDetails.Country.CountryName;
        var newCountryName = countryName.toLowerCase();
        if (newCountryName == "united states")
            newCountryName = "us";
        else if (newCountryName == "england" || newCountryName == "ireland" || newCountryName == "scotland" || newCountryName == "wales" || newCountryName == "northern ireland")
            newCountryName = "uk";
        else if (newCountryName == "australia")
            newCountryName = "au";
        else if (newCountryName == "canada")
            newCountryName = "ca";
        else
            newCountryName = "world";
        $('a[title = "' + newCountryName + '"]').trigger('click');
    });
});

我宁愿在服务器端使用一些东西。有谁知道您是否可以在 C# 中获取用户的国家/地区名称?

最佳答案

在服务器端,您唯一可以回复的是 IP 地址,您可以使用它来执行位置查找。有免费的 IP 地址到位置映射的数据库,或者您可以使用 HostIP.info。

https://stackoverflow.com/questions/372591/how-to-get-city-country-and-country-code-for-a-particular-ip-address-in-asp-ne了解更多信息。

关于c# - 你如何在 C# 中获取用户的国家/地区名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8897773/

相关文章:

c# - 如何在WFA中将数据添加到BoxPlot?

c# - 一段时间后是否重置静态变量

c# - 我应该如何处理基于 C++ 的 dll 中的错误?

javascript - Vue 将一个对象作为计算属性展开

javascript - 一种使用 .load 和通配符的方法

javascript - 在 EditorFor 字段上使用 Javascript?

javascript - 如何使用 Wordpress 的 REST Api 访问本地主机上的数据?

C# - 从 PDF 签名获取公钥

javascript - 一种在加载时淡入背景的方法?

javascript - 数据表类型错误: f is undefined