如何获取客户/用户位置详细信息

2024-01-05

在我的应用程序中,我想跟踪客户端/用户位置以存储在数据库中。我正在使用此代码来获取用户 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 等。

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

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

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

详细信息如http://www.iplocationtools.com/index.html http://www.iplocationtools.com/index.html


是的,当然,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>";
}

除了纬度/经度之外,google.loader.ClientLocation返回一个地址,其中包括城市、国家、国家代码、地区等...
另外,看看这里的例子 http://gmaps-samples-v3.googlecode.com/svn/trunk/commonloader/clientlocation.html.

即使地址为空,您始终可以将返回的纬度/经度与地理编码服务 https://developers.google.com/maps/documentation/javascript/geocoding获取详细地址。

然而,正如文中提到的官方网站 https://developers.google.com/maps/articles/geolocation:

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

也在问题跟踪器 https://code.google.com/p/google-ajax-apis/issues/detail?id=586:

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

最好的方法是使用基于HTML5 http://www.w3schools.com/html/html5_geolocation.asp方法。但是,如果您使用基于 IP 的服务,请确保您意识到 Google 可以随时放弃对它的支持。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何获取客户/用户位置详细信息 的相关文章

随机推荐