Help us translate this website and improve this translation and earn free licenses!
Anonymous user  |  Log in  |  Create Account

GeoIP

Thanks to MaxMind we have the geolocation by IP number: from an IP number we will have data about localization, like latitude, longitude and others.

We only need to use the code of the example and download the MaxMind database (direct download).




Code.aspx
<cc1:GMap ID="GMap1" runat="server" />
Code.aspx.cs
string databaseFile = Server.MapPath("~/App_Data/GeoIP/GeoLiteCity.dat");
LookupService service = new LookupService(databaseFile);
Location location = service.getLocation(Request.UserHostAddress);

if (location != null)
{
    GLatLng latlng = new GLatLng(location.latitude, location.longitude);
    GMap1.setCenter(latlng, 10);

    string infoWindowHTML = string.Format(@"
        <div style=""text-align:left"">
            <b>GEOIP PROPERTIES</b>
            <br />
            <b>Area Code</b>: {0}
            <br />
            <b>City</b>: {1}
            <br />
            <b>Country Code</b>: {2}
            <br />
            <b>Country Name</b>: {3}
            <br />
            <b>DMA Code</b>: {4}
            <br />
            <b>Postal Code</b>: {5}
            <br />
            <b>Region</b>: {6}
        </div>
        ", 
         location.area_code,
         location.city,
         location.countryCode,
         location.countryName,
         location.dma_code,
         location.postalCode,
         location.region);

    GMap1.Add(new GInfoWindow(new GMarker(latlng), infoWindowHTML));
}
Powered by Subgurim.NET