如何获得用户';用C#表示的国家名称?

如何获得用户';用C#表示的国家名称?,c#,javascript,jquery,html,geolocation,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

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

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


查看了解更多信息。

不知道这是否有帮助,但在本地

CultureInfo.CurrentCulture.Name

根据您当前的语言环境返回类似“en-GB”或“fr-fr”的内容

我对WP7执行了此操作,但在standard.net framework中的代码几乎相同: 做这项工作的班级在下面。只需删除作为WP7依赖项的ICivicAddressResolver并创建自己的接口

 public class AddressResolver:ICivicAddressResolver
    {
        string language = "en-GB";
        public AddressResolver()
        {

        }
        public AddressResolver(string language)
        {
            this.language = language;
        }
        [DataContract]
        public class AddressInfo
        {
            [DataMember(Name = "formatted_address")]
            public string FormattedAddress { get; set; }
        }
        [DataContract]
        public class ResultInfo
        {
            [DataMember(Name = "results")]
            public AddressInfo[] Info { get; set; }
        }
        readonly string URITemplate = "http://maps.googleapis.com/maps/api/geocode/json?latlng={0},{1}&sensor=true&language={2}";
        #region ICivicAddressResolver Members

        public CivicAddress ResolveAddress(GeoCoordinate coordinate)
        {
            throw new NotImplementedException("Use async version instead");
        }

        public void ResolveAddressAsync(GeoCoordinate coordinate)
        {
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;
            client.DownloadStringCompleted += (s, e) =>
            {
                if (e.Error == null)
                {
                    var ainfo = ReadToObject<ResultInfo>(e.Result);
                    ResolveAddressCompleted(this, new ResolveAddressCompletedEventArgs(ToCivic(ainfo),e.Error,false,null));
                }
                else
                {
                    ResolveAddressCompleted(this, new ResolveAddressCompletedEventArgs(new CivicAddress(), e.Error, false, null));
                }
            };
            client.DownloadStringAsync(new Uri(GetFormattedUri(coordinate)));
        }

        private CivicAddress ToCivic(ResultInfo ainfo)
        {
            List<string> res = new List<string>();
            foreach (var single in ainfo.Info)
            {
                res.Add(single.FormattedAddress);
            }
            if (res.Count > 0)
                return new CivicAddress() { AddressLine1 = res[0] };
            else
                return new CivicAddress() { AddressLine1 = "#UNKNOWN#" };
        }

        public event EventHandler<ResolveAddressCompletedEventArgs> ResolveAddressCompleted = delegate { };

        #endregion
        private string GetFormattedUri(GeoCoordinate coord)
        {
            return string.Format(CultureInfo.InvariantCulture, URITemplate, coord.Latitude, coord.Longitude,language);
        }
        public static T ReadToObject<T>(string json) where T : class
        {
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
            T res = ser.ReadObject(ms) as T;
            ms.Close();
            return res;
        }
    }
公共类AddressResolver:ICivicAddressResolver
{
字符串语言=“en GB”;
公共地址解析程序()
{
}
公共地址解析程序(字符串语言)
{
这种语言=语言;
}
[数据合同]
公共类地址信息
{
[DataMember(Name=“格式化的地址”)]
公共字符串格式化地址{get;set;}
}
[数据合同]
公共类结果信息
{
[DataMember(Name=“results”)]
公共地址信息[]信息{get;set;}
}
只读字符串URITemplate=”http://maps.googleapis.com/maps/api/geocode/json?latlng={0}、{1}&sensor=true&language={2}”;
#区域地址解析程序成员
公共公民地址解析地址(地理坐标)
{
抛出新的NotImplementedException(“改用异步版本”);
}
公共void ResolveAddressAsync(地理坐标)
{
WebClient客户端=新的WebClient();
client.Encoding=Encoding.UTF8;
client.DownloadStringCompleted+=(s,e)=>
{
如果(e.Error==null)
{
var ainfo=ReadToObject(即结果);
ResolveAddressCompleted(这是新的ResolveAddressCompletedEventArgs(ToCivic(ainfo),例如Error、false、null));
}
其他的
{
ResolveAddressCompleted(这是新的ResolveAddressCompletedEventArgs(新的CivicAddress(),例如Error、false、null));
}
};
DownloadStringAsync(新Uri(GetFormattedUri(坐标));
}
私人公民地址至CIVIC(ResultInfo ainfo)
{
List res=新列表();
foreach(ainfo.Info中的单个变量)
{
res.Add(单个格式化地址);
}
如果(分辨率计数>0)
返回新的CivicAddress(){AddressLine1=res[0]};
其他的
返回新的CivicAddress(){AddressLine1=“#未知#”};
}
公共事件事件处理程序ResolveAddressCompleted=委托{};
#端区
私有字符串GetFormattedUri(地理坐标坐标坐标)
{
返回string.Format(CultureInfo.InvariantCulture、URITemplate、坐标纬度、坐标经度、语言);
}
公共静态T ReadToObject(字符串json),其中T:class
{
MemoryStream ms=新的MemoryStream(Encoding.UTF8.GetBytes(json));
DataContractJsonSerializer ser=新的DataContractJsonSerializer(类型(T));
T res=ser.ReadObject(ms)作为T;
Close女士();
返回res;
}
}

为什么不试试IP2Location的API

你可以这样传递你的IP地址

http://api.ip2location.com/?ip=145.228.219.221&key=demo
更多信息请访问


在C#中提出相同的请求-从navigator的GeoAPI获取您的位置,并将其转发到服务器上您自己的web服务,该服务器使用
WebClient
Classic发出get请求。我想向您指出,爱尔兰不在英国。北爱尔兰是,但爱尔兰是爱尔兰共和国,本身就是一个国家。