使用 Google 从 .NET 代码获取用户位置
我想在会话期间存储用户在 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 库,但我在其中找不到任何内容来进行位置查找(也许我错了?)。
I want to store a user's current location on an ASP.NET MVC website for the duration of a session. Because I want to use the location server-side, the way I think this should work (maybe other suggestions?) is when a user first hits the website, I get the location and store it in the session, so on later requests it does not have to be looked up again.
The best way I can find is to use Google's ClientLocation method:
<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>
My question is how best to get the location looked up in this Javascript code back to the server to store in the database or in the session. I know that I could call a controller action using jQuery to pass the location:
$.post("/Home/StoreLocation", location, function(){});
But for performance I don't want this to be happening on every single page load. The other way i thought to do it was to call Google's apis from .NET code by doing something like this (a call to geocoding):
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
But I can't find any equivalent way to call ClientLocation directly from a URL like this - all the examples I have seen use google.loader (which I don't think I can use when doing it this way?) Another thing I have considered is using Google's .NET library, but I can't find anything in there to do a location lookup (maybe I am wrong?).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在你的第二个例子中,我不太明白strAddress。 你有客户的地址吗? 或者说这是他们的IP? 我刚刚用我的 IP 地址尝试了该 URL,它显示我在大约 1000 英里之外。
无论如何,我不明白#1 有什么问题。 只需在您的 Site.Master 中添加类似的内容即可:
edited 将 if 语句放入 ASP 标记中 - 它不是 javascript if。
为那些想要使用此代码的人进行编辑,您必须在控制器中放置一些内容,以便在获取位置时设置 session["has_location"] = true 。
In your second example, I don't quite understand strAddress. Do you have the client's address? Or is that their IP? I just tried that URL with my IP address and it says I'm about 1000 miles away.
Anyways, I don't see what's wrong with #1. Just have something like this in your Site.Master:
edited to put the if statement in ASP tags -- it's not a javascript if.
edit for those that want to use this code you'd have to put something in your controller in order to set session["has_location"] = true when you get the location.
这个问题促使我完成了草稿文件夹中的一篇博客文章:
http://www.alexjamesbrown .com/geek/development/dotnet/reverse-geocoding-with-google-api-and-c/
请告诉我这是否有任何帮助(这不完全是您所要求的......但它可以适应做你想做的事)
This question prompted me to finish off a blog post I had in my draft folder:
http://www.alexjamesbrown.com/geek/development/dotnet/reverse-geocoding-with-google-api-and-c/
Please let me know if that's of any help (it's not quite what you are asking... but it could be adapted to do what you want)
Google 客户端定位方法设计用于前端/浏览器中。 听起来您正在寻找可以在您的服务器上运行的东西 - 无论是 Web 服务还是本地 IP 数据库。
MaxMind 有一个带有 C# API 的免费 GeoIP 数据库。 您将客户端的 IP 地址传递给 GeoIP 库,它会返回城市和州。
http://www.maxmind.com/app/geolitecity
MaxMind 还提供了一个网络服务象征性的费用。
The Google Client Location Method is designed to be used on the front end/in the browser. It sounds like you're looking for something that would run on your server - either a web service or a local IP database.
MaxMind's has a free GeoIP database with a C# api. You pass the client's IP address to the GeoIP library and it returns the city and state.
http://www.maxmind.com/app/geolitecity
MaxMind also has a webservice available for a nominal fee.