我想在会话期间将用户的当前位置存储在 ASP.NET MVC 网站上。因为我想使用位置服务器端,所以我认为这应该起作用的方式(也许其他建议?)是当用户第一次访问网站时,我获取位置并将其存储在会话中,所以稍后请求它确实不必再次查找。
我能找到的最好方法是使用 Google 的 ClientLocation 方法:
<script src="http://www.google.com/jsapi?key=YOURAPIKEY" type="text/javascript"></script>
<script>
google.load("jquery", "1.2.6");
google.load("jqueryui", "1.5.2");
var yourLocation = google.loader.ClientLocation.address.city + ", "
+ google.loader.ClientLocation.address.region;
</script>
我的问题是如何最好地将在此 Javascript 代码中查找的位置返回到服务器以存储在数据库或会话中。我知道我可以使用 jQuery 调用控制器操作来传递位置:
$.post("/Home/StoreLocation", location, function(){});
但是为了性能,我不希望在每次加载页面时都发生这种情况。我认为这样做的另一种方法是通过执行以下操作(调用地理编码)从.NET代码调用Google的api:
string path = "http://maps.google.com/maps/geo?q=" + strAddress + "&output=csv&key=" + apiKey;
WebClient client = new WebClient();
string[] eResult = client.DownloadString(sPath).ToString().Split(',');
//Parse the array
但是我找不到任何等效的方法来直接从这样的 URL 调用 ClientLocation - 我看到的所有示例都使用 google.loader (我认为这样做时我不能使用它?)我有另一件事考虑使用Google 的 .NET 库,但我在其中找不到任何东西来进行位置查找(也许我错了?)。