【问题标题】:how to get the client/user location details如何获取客户端/用户位置详细信息
【发布时间】:2015-09-01 11:11:31
【问题描述】:

在我的应用程序中,我想跟踪客户端/用户位置以存储在数据库中。我正在使用此代码获取用户 IP 地址

 string VisitorsIPAddr = string.Empty;
        if (HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"] != null)
        {
            VisitorsIPAddr = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
        }
        else if (HttpContext.Current.Request.UserHostAddress.Length != 0)
        {
            VisitorsIPAddr = HttpContext.Current.Request.UserHostAddress;
        }
        ipAddress = VisitorsIPAddr;

使用上面的代码,我得到了 IP 地址。现在下一步是获取时区、地区、城市名称、国家名称、国家代码、气象站、ISP 等。

但是,我不想使用任何其他第三方 API 来获取除 Google 之外的详细信息。任何人都可以回复以解决我的问题。

是否可以使用 c# 在 asp.net 中获取上述详细信息,或者我必须为此使用任何其他服务。

使用IP我想得到客户端地址的精确位置。

详情如http://www.iplocationtools.com/index.html

【问题讨论】:

    标签: google-maps geolocation google-api ip-geolocation


    【解决方案1】:

    您可以使用 geoIP MaxMind Web 服务在 C# 中获取位置详细信息。 以下是可能对您有帮助的链接。

    http://dev.maxmind.com/geoip/geoip2/web-services/

    这里是可用的 Github 存储库。

    https://github.com/maxmind/GeoIP2-dotnet

    【讨论】:

    • 我认为您没有正确阅读我的问题。我已经清楚了,我不想使用任何其他第三方 API 来获取除 Google 之外的详细信息。
    【解决方案2】:

    是的,当然,Google Maps JS API v3 确实为位置地理编码服务提供了 IP。您可以使用此代码根据 IP 地址获取用户位置:

    // If ClientLocation was filled in by the loader, use that info instead
    if (google.loader.ClientLocation) {
      zoom = 13;
      latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
      location = "Showing IP-based location: <b>" + getFormattedLocation() + "</b>";
    }
    

    在 Lat/Lng 旁边,google.loader.ClientLocation 返回一个地址,其中包括城市、国家、国家/地区代码、地区等...
    另外,看看the example here

    即使地址为空,您始终可以使用返回的 Lat/Lng 和 Geocoding Service 来获取地址详细信息。

    但是,正如official site中提到的那样:

    ...我们不建议使用这种方法(使用 IP 地址来检测用户的位置)进行地理定位。 W3C 方法是最简单且最受支持的方法,因此应优先于其他方法。

    同样在issue tracker:

    ...我们在几年前停止记录它并推荐基于 HTML 的解决方案,因为它们提高了准确性,但功能本身并没有从加载器中删除...

    最好的方法是使用HTML5-based 方法。但是,如果您使用的是基于 IP 的,请确保您意识到 Google 可以随时放弃对它的支持。

    【讨论】:

    • 从google的官方网站上显示的结果是developers.google.com/maps/documentation/javascript/examples/…
    • 但我想要 JSON 格式的城市、密码等数据,因为我想将用户详细信息存储在我的数据库中
    • 您可以使用反向地理编码服务来查找该信息。演示:jsfiddle.net/jbbuc1th
    • not enough 答案我想详细地从 c# 代码中以字符串格式存储
    • 你能帮我在后面的代码中获取详细信息
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 2017-08-04
    • 2017-10-02
    • 2010-11-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多