public static double convertToDistance(Listing a, Listing b) { double theDistance = (Math.Sin(DegreesToRadians(Convert.ToDouble(a.Lat))) * Math.Sin(DegreesToRadians(Convert.ToDouble(b.Lat))) + Math.Cos(DegreesToRadians(Convert.ToDouble(a.Lat))) * //Haversine formula Math.Cos(DegreesToRadians(Convert.ToDouble(b.Lat))) * Math.Cos(DegreesToRadians(Convert.ToDouble(a.Lng - b.Lng)))); return Convert.ToDouble((RadiansToDegrees(Math.Acos(theDistance)))) * 69.09 * 1.6093; // the distance unit is 69.09 wich returns distance in miles.. there are 1.6 kilometers in a mile. so return is in km. }
public static void getCoordinates(Listing listing) { System.Threading.Thread.Sleep(250); decimal[] geoCode = new decimal[2] { 0, 0 }; string address = "http://maps.googleapis.com/maps/api/geocode/xml?address=" + listing.AddressForURL + "&sensor=false"; var result = new System.Net.WebClient().DownloadString(address); XmlDocument doc = new XmlDocument(); doc.LoadXml(result); string status = doc.DocumentElement.SelectSingleNode("/GeocodeResponse/status").InnerText; XmlNodeList parentNode = doc.GetElementsByTagName("location"); if (status== "OK") { foreach (XmlNode childrenNode in parentNode) { geoCode[0] = Convert.ToDecimal(childrenNode.SelectSingleNode("lat").InnerText, new CultureInfo("en-US")); geoCode[1] = Convert.ToDecimal(childrenNode.SelectSingleNode("lng").InnerText, new CultureInfo("en-US")); } listing.GetGeoCode(geoCode); } else if (status == "ZERO_RESULTS") { //kast exception der fortæller at addressen ikke findes og/eller er stavet forkert. Console.WriteLine("Addressen findes ikke og/eller er stavet forkert."); Console.ReadKey(); } else if (status == "OVER_QUERY_LIMIT") { //Kast exception der fortæller at de har opbrugt kvote af lookups. Console.WriteLine("Kvote af lookups er brugt op"); Console.ReadKey(); } else { Console.WriteLine(status); Console.ReadKey(); } }