[8040] | 1 | /** |
---|
| 2 | * Location.cs |
---|
| 3 | * |
---|
| 4 | * Copyright (C) 2008 MaxMind Inc. All Rights Reserved. |
---|
| 5 | * |
---|
| 6 | * This library is free software; you can redistribute it and/or |
---|
| 7 | * modify it under the terms of the GNU Lesser General Public |
---|
| 8 | * License as published by the Free Software Foundation; either |
---|
| 9 | * version 2.1 of the License, or (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This library is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 14 | * Lesser General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU Lesser General Public |
---|
| 17 | * License along with this library; if not, write to the Free Software |
---|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | using System; |
---|
| 23 | using System.IO; |
---|
| 24 | //using System.Math; |
---|
| 25 | |
---|
| 26 | public class Location { |
---|
| 27 | public String countryCode; |
---|
| 28 | public String countryName; |
---|
| 29 | public String region; |
---|
| 30 | public String city; |
---|
| 31 | public String postalCode; |
---|
| 32 | public double latitude; |
---|
| 33 | public double longitude; |
---|
| 34 | public int dma_code; |
---|
| 35 | public int area_code; |
---|
| 36 | public String regionName; |
---|
| 37 | public int metro_code; |
---|
| 38 | |
---|
| 39 | private static double EARTH_DIAMETER = 2 * 6378.2; |
---|
| 40 | private static double PI = 3.14159265; |
---|
| 41 | private static double RAD_CONVERT = PI / 180; |
---|
| 42 | |
---|
| 43 | public double distance (Location loc) { |
---|
| 44 | double delta_lat, delta_lon; |
---|
| 45 | double temp; |
---|
| 46 | |
---|
| 47 | double lat1 = latitude; |
---|
| 48 | double lon1 = longitude; |
---|
| 49 | double lat2 = loc.latitude; |
---|
| 50 | double lon2 = loc.longitude; |
---|
| 51 | |
---|
| 52 | // convert degrees to radians |
---|
| 53 | lat1 *= RAD_CONVERT; |
---|
| 54 | lat2 *= RAD_CONVERT; |
---|
| 55 | |
---|
| 56 | // find the deltas |
---|
| 57 | delta_lat = lat2 - lat1; |
---|
| 58 | delta_lon = (lon2 - lon1) * RAD_CONVERT; |
---|
| 59 | |
---|
| 60 | // Find the great circle distance |
---|
| 61 | temp = Math.Pow(Math.Sin(delta_lat/2),2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(delta_lon/2),2); |
---|
| 62 | return EARTH_DIAMETER * Math.Atan2(Math.Sqrt(temp),Math.Sqrt(1-temp)); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|