如何获取客户/用户位置的详细信息 [英] how to get the client/user location details

查看:121
本文介绍了如何获取客户/用户位置的详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我想跟踪客户端/用户位置以存储在数据库中。我使用此代码来获取用户的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中获得以上细节,或者我必须使用任何其他服务。 / p>

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



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

解决方案

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

  //如果ClientLocation由加载器填充,使用该信息,而不是
if(google.loader.ClientLocation){
zoom = 13;
latlng = new google.maps.LatLng(google.loader.ClientLocation.latitude,google.loader.ClientLocation.longitude);
location =显示基于IP的位置:< b> + getFormattedLocation()+< / b>;
}

在Lat / Lng旁边, google.loader.ClientLocation 返回包含城市,国家,国家代码,地区等的地址...

另请参阅这里的例子



即使地址为空,你可以随时使用带有地理编码服务的返回Lat / Lng来获取详细地址。



然而,正如官方网站 b
$ b


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


同样在问题跟踪器


...我们在几年前停止记录它,并且已经推荐基于HTML的解决方案,因为它们的准确性有所提高,但功能本身并未从Loader中移除...

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


In my application I want to track the client/user location to store in database. I'm using this code to get the user IP Address

 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;

Using above code I'm getting the IP address. now the next step to get the timezone, Region, City Name, Country Name, Country Code, Weather Station, ISP etc.

But, I don't want to use any other third party API to get the details except Google. Anyone can reply to get my problem solved.

Is this possible to get the above details in asp.net using c# or I have to use any other service for that.

Using the IP I want to get the precise location of client address.

Details like http://www.iplocationtools.com/index.html

解决方案

Yes, sure, Google Maps JS API v3 does provide an IP to location geocoding services. You can use this code to get the users location based on their IP address:

// 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>";
}

Beside Lat/Lng, google.loader.ClientLocation returns an address which include city, country, country_code, region etc...
Also, look at the example here.

Even if the address is null, you can always use the returned Lat/Lng with the Geocoding Service to get the address detail.

However, just as it is mentioned in the official site:

... we don't recommend using this approach (using IP addresses to detect a user's location) for geolocation. The W3C approach is the easiest and most fully-supported so it should be prioritized over other methods.

Also in the issue tracker:

... We stopped documenting it several years ago and have recommended the HTML-based solutions due to their improved accuracy, but the functionality itself has not been removed from the Loader...

The best way to do this is with the HTML5-based approach. However, if you are using the IP-based, be sure that you realize Google can drop support to it really any time.

这篇关于如何获取客户/用户位置的详细信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆