代码之家  ›  专栏  ›  技术社区  ›  James Allen

使用Google从获取用户的位置。NET代码

  •  1
  • James Allen  · 技术社区  · 17 年前

    <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(){});
    

    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
    

    Google's .NET library ,但我在那里找不到任何可以进行位置查找的东西(也许我错了?)。

    3 回复  |  直到 17 年前
        1
  •  1
  •   Alex    17 年前

    你有客户的地址吗?这是他们的IP吗?我刚刚用我的IP地址尝试了那个URL,它说我大约在1000英里外。

    不管怎样,我看不出#1有什么问题。在你的网站上有这样的东西。大师:

    <script type="text/javascript">
      //other javascript here
      <% if (Sesssion.Contents["has_location"] == null) { %>
        $(document).ready(function_to_submit_location)
      <% } %>
    </script>
    

    为那些想使用此代码的人编辑

        3
  •  0
  •   Andrew Hopper    17 年前

    Google客户端定位方法设计用于前端/浏览器中。听起来你在寻找可以在服务器上运行的东西——无论是web服务还是本地IP数据库。

    MaxMind有一个免费的带有C#api的GeoIP数据库。您将客户端的IP地址传递给GeoIP库,它将返回城市和州。

    http://www.maxmind.com/app/geolitecity