I was developed Prayer Time widget a month ago. I was used IP to geo service that not available anymore. This update using hostip.info service instead. The hostip.info service is not reliable enough for all IP world wide. Some IPs are not covered yet but more stable than previous service.
Here’s core of my code to retrieve geographic information from hostip.info.
WebClient webClient = new WebClient();
string responseString = null;
try
{
responseString =
webClient.DownloadString(string.Format("http://api.hostip.info/get_html.php?ip={0}&position=true",
ipAddress));
}
catch (WebException)
{
responseString = string.Empty;
}
catch (Exception)
{
responseString = string.Empty;
}
Regex regex = new Regex("Country\\:\\s(?<Country>[A-Za-z0-9]+)\\s\\((?<CountryId>[A-Z]{2})\\)\\nCity\\:\\s(?<City>[A-Za-z0-9]+)\\nLatitude\\:\\s(?<Latitude>\\-?[0-9\\.]+)\\nLongitude\\:\\s(?<Longitude>\\-?[A-Za-z0-9\\.]+)");
if (!string.IsNullOrEmpty(responseString))
{
Match m = regex.Match(responseString);
if (m.Success)
{
city = m.Groups["City"].Value;
country = m.Groups["Country"].Value;
countryId = m.Groups["CountryId"].Value;
longitude = m.Groups["Longitude"].Value;
latitude = m.Groups["Latitude"].Value;
}
}
The complate source code can be downloaded here.
I hope this update will be more reliable that previous version.