public double distance(Location loc) { double delta_lat, delta_lon; double temp; double lat1 = latitude; double lon1 = longitude; double lat2 = loc.latitude; double lon2 = loc.longitude; // convert degrees to radians lat1 *= RAD_CONVERT; lat2 *= RAD_CONVERT; // find the deltas delta_lat = lat2 - lat1; delta_lon = (lon2 - lon1) * RAD_CONVERT; // Find the great circle distance temp = Math.Pow(Math.Sin(delta_lat / 2), 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(delta_lon / 2), 2); return EARTH_DIAMETER * Math.Atan2(Math.Sqrt(temp), Math.Sqrt(1 - temp)); }
public Location getLocation(long ipnum) { int record_pointer; byte[] record_buf = new byte[FULL_RECORD_LENGTH]; char[] record_buf2 = new char[FULL_RECORD_LENGTH]; int record_buf_offset = 0; Location record = new Location(); int str_length = 0; int j, Seek_country; double latitude = 0, longitude = 0; try { Seek_country = SeekCountry(ipnum); if (Seek_country == databaseSegments[0]) { return null; } record_pointer = Seek_country + ((2 * recordLength - 1) * databaseSegments[0]); if ((dboptions & GEOIP_MEMORY_CACHE) == 1) { Array.Copy(dbbuffer, record_pointer, record_buf, 0, Math.Min(dbbuffer.Length - record_pointer, FULL_RECORD_LENGTH)); } else { file.Seek(record_pointer, SeekOrigin.Begin); file.Read(record_buf, 0, FULL_RECORD_LENGTH); } for (int a0 = 0; a0 < FULL_RECORD_LENGTH; a0++) { record_buf2[a0] = Convert.ToChar(record_buf[a0]); } // get country record.countryCode = countryCode[unsignedByteToInt(record_buf[0])]; record.countryName = countryName[unsignedByteToInt(record_buf[0])]; record_buf_offset++; // get region while (record_buf[record_buf_offset + str_length] != '\0') str_length++; if (str_length > 0) { record.region = new String(record_buf2, record_buf_offset, str_length); } record_buf_offset += str_length + 1; str_length = 0; // get region_name record.regionName = RegionName.getRegionName(record.countryCode, record.region); // get city while (record_buf[record_buf_offset + str_length] != '\0') str_length++; if (str_length > 0) { record.city = new String(record_buf2, record_buf_offset, str_length); } record_buf_offset += (str_length + 1); str_length = 0; // get postal code while (record_buf[record_buf_offset + str_length] != '\0') str_length++; if (str_length > 0) { record.postalCode = new String(record_buf2, record_buf_offset, str_length); } record_buf_offset += (str_length + 1); // get latitude for (j = 0; j < 3; j++) latitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8)); record.latitude = (float)latitude / 10000 - 180; record_buf_offset += 3; // get longitude for (j = 0; j < 3; j++) longitude += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8)); record.longitude = (float)longitude / 10000 - 180; record.metro_code = record.dma_code = 0; record.area_code = 0; if (databaseType == DatabaseInfo.CITY_EDITION_REV1) { // get metro_code int metroarea_combo = 0; if (record.countryCode == "US") { record_buf_offset += 3; for (j = 0; j < 3; j++) metroarea_combo += (unsignedByteToInt(record_buf[record_buf_offset + j]) << (j * 8)); record.metro_code = record.dma_code = metroarea_combo / 1000; record.area_code = metroarea_combo % 1000; } } } catch (IOException) { Trace.WriteLine("IO Exception while seting up segments"); // Console.Write("IO Exception while seting up segments"); } return record; }