【发布时间】:2012-01-17 16:02:45
【问题描述】:
我目前正在使用地理定位来获取用户的国家/地区名称。这是我的代码:
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# 中获取用户的国家/地区名称?
【问题讨论】:
-
在 C# 中发出相同的请求 - 获取您从导航器的 GeoAPI 获得的位置并将其转发到服务器上您自己的 Web 服务,该服务器使用
WebClient类发出 Get 请求 -
我想向您指出,爱尔兰不在英国。北爱尔兰是,但爱尔兰是爱尔兰共和国,它本身就是一个国家。
标签: c# javascript jquery html geolocation