/// <summary> /// Sets the geo ip related props /// </summary> /// <param name="geoIp"></param> public void SetGeoIp(MaxMind.GeoIP2.Responses.CityResponse geoIp) { if (geoIp == null) { return; } this.CountryIso = geoIp.Country.IsoCode; this.Country = geoIp.Country.Name; this.City = geoIp.City.Name; this.Lon = geoIp.Location.Longitude; this.Lat = geoIp.Location.Latitude; }
private CityInfo GetCityByIP(String IPAddress) { //Weather Information CityInfo ThisCity = new CityInfo(); ThisCity.Lat = 25.2048; ThisCity.Lng = 55.2708; //If local server set it to dubai address MaxMind.GeoIP2.Responses.CityResponse city = new MaxMind.GeoIP2.Responses.CityResponse(); String DatabaseLocation = HttpContext.Current.Server.MapPath("/GeoLiteCity/GeoLite2-City.mmdb"); using (var reader = new DatabaseReader(DatabaseLocation)) { // Replace "City" with the appropriate method for your database, e.g., // "Country". try { city = reader.City(IPAddress); ThisCity.Country = city.Country.Name; // 'United States' ThisCity.City = city.City.Name; ThisCity.Lat = city.Location.Latitude == null ? 0 : (Double)city.Location.Latitude; ThisCity.Lng = city.Location.Longitude == null ? 0 : (Double)city.Location.Longitude; } catch { //do any error processing } }//using(var reader) if (String.IsNullOrEmpty(ThisCity.Country)) { ThisCity.Country = "United Arab Emirates"; } if (String.IsNullOrEmpty(ThisCity.City)) { ThisCity.City = ThisCity.Country; } return(ThisCity); }
public static WeatherViewModel getLocalWeather(String UserIP) { String JSonContent = String.Empty; int DelayHours = 0; //If local server set it to dubai address MaxMind.GeoIP2.Responses.CityResponse city = new MaxMind.GeoIP2.Responses.CityResponse(); var WeatherView = new WeatherViewModel(); WeatherView.Forecast = new List <Forcast>(); String DatabaseLocation = HttpContext.Current.Server.MapPath("/GeoLiteCity/GeoLite2-City.mmdb"); using (var reader = new DatabaseReader(DatabaseLocation)) { // Replace "City" with the appropriate method for your database, e.g., // "Country". try { city = reader.City(UserIP); WeatherView.Country = city.Country.Name; // 'United States' WeatherView.City = city.City.Name; } catch { //do any error processing } }//using(var reader) if (String.IsNullOrEmpty(WeatherView.Country)) { WeatherView.Country = "United Arab Emirates"; } if (String.IsNullOrEmpty(WeatherView.City)) { WeatherView.City = "Dubai"; } do { String CacheFile = getWeatherFile(WeatherView.Country, WeatherView.City, DelayHours); if (!File.Exists(CacheFile)) { JSonContent = DownloadWeatherInfo(WeatherView.Country, WeatherView.City, CacheFile); } else { JSonContent = File.ReadAllText(CacheFile); } DelayHours = DelayHours - 4; } while (JSonContent.Length <= 10); dynamic WeatherInfo = System.Web.Helpers.Json.Decode(JSonContent); if (WeatherInfo != null) { var WeatherNow = WeatherInfo.data.current_condition[0]; WeatherView.ConditionText = WeatherNow.weatherDesc[0].value; WeatherView.ConditionCode = WeatherNow.weatherCode; WeatherView.Speed = WeatherNow.windspeedKmph; WeatherView.ConditionTemperature = Util.toDouble(WeatherNow.temp_C).ToString("0.0"); if (WeatherView.ConditionTemperature == "0.0") { Double Temp = (Util.toDouble(WeatherNow.temp_F) - 32) / 1.8; WeatherView.ConditionTemperature = Temp.ToString("0.0"); } WeatherView.TemperatureUnit = "°C"; WeatherView.Pressure = Util.toDouble(WeatherNow.pressure); WeatherView.Wind = WeatherNow.windspeedKmph; WeatherView.Direction = WeatherNow.winddirDegree; WeatherView.Visibility = Util.toDouble(WeatherNow.visibility); WeatherView.Humidity = WeatherNow.humidity; foreach (var ForcastDay in WeatherInfo.data.weather) { var thisForcast = new Forcast { Code = Util.toInt(ForcastDay.hourly[3].weatherCode), Date = (DateTime.Parse(ForcastDay.date)).ToString("dd MMM"), status = ForcastDay.hourly[3].weatherDesc[0].value, TempHigh = ForcastDay.maxtempC, TempLow = ForcastDay.mintempC }; WeatherView.Forecast.Add(thisForcast); } } //var intAddress = BitConverter.ToInt64(IPAddress.Parse(UserIP).GetAddressBytes(), 0); return(WeatherView); }