Sebulan yang lalu saya telah membuat Prayer Time widget yang memanfaatkan service IP to geo. Service tersebut sekarang sudah tidak dapat digunakan lagi. Sebagai gantinya saya gunakan service dari hostip.info. Service hostip.info tidak terlalu handal dan mencakup semua IP di seluruh dunia, namun service ini lebih stabil daripada sevice yang saya gunakan sebelumnya.
Berikut adalah potongan kode yang saya guakan untuk mengambil informasi lokasi dari service 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;
}
}
Kode secara lengkap dapat didownload dari sini.
Semoga versi ini lebih stabil dari sebelumnya.