How to get customer / user location data

In my application, I want to track the client / user location for storage in the database. I am using this code to get the user's 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 the above code, I get the IP address. now the next step is to get the time zone, region, city name, country name, country code, weather station, internet service provider, etc.

But I don't want to use any other third party API to get information other than google. Anyone can answer to solve my problem.

Is it possible to get the above data in asp.net using C #, or should I use some other service for this.

Using IP, I want to get the exact location of the client's address.

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

+3


source to share


2 answers


Yes, of course Google Maps JS API v3 provides IP location geocoding services. This code can be used to locate users 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>";
}

      

In addition to Lat / Lng, it google.loader.ClientLocation

returns an address that includes city, country, country_code, region, etc ... Also consider an example here .

Even if the address is NULL, you can always use the returned Lat / Lng with a geocoding service to get the details of the address.

However, as stated on the official website:



... we do not recommend using this approach (using IP addresses to determine the user's location) for geolocation. The W3C approach is the simplest and most fully supported, so it should take precedence over other methods.

Also in the bug tracker :

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

The best way to do this is with an HTML5 based approach . However, if you are using IP-based, make sure you understand that Google may opt out at any time.

+4


source


You can use geoIP MaxMind web services to get location information in C #. Here is a link that you might find helpful.

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



The Github repository is available here.

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

0


source







All Articles