public static int GetDistance(String location1, String location2) { // Haversine formula: // a = sin²(delta_lat/2) + cos(lat1).cos(lat2).sin²(delta_long/2) // c = 2.atan2(sqrt(a), sqrt(1-a)) // d = R.c // where R is earth’s radius (mean radius = 6,371km); // note that angles need to be in radians to pass to trig functions! double lat1, lon1, lat2, lon2, d_lat, d_lon, a1, a2, a3, a, c, distance; lat1 = GXGeolocation.GetLatitude(location1); lon1 = GXGeolocation.GetLongitude(location1); lat2 = GXGeolocation.GetLatitude(location2); lon2 = GXGeolocation.GetLongitude(location2); d_lat = GXGeolocation.DegreesToRadians(lat2 - lat1); d_lon = GXGeolocation.DegreesToRadians(lon2 - lon1); lat1 = GXGeolocation.DegreesToRadians(lat1); lat2 = GXGeolocation.DegreesToRadians(lat2); a1 = Math.Pow(Math.Sin(d_lat / 2), 2); a2 = Math.Pow(Math.Sin(d_lon / 2), 2); a3 = Math.Cos(lat1) * Math.Cos(lat2); a = a1 + a2 * a3; c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); distance = 6371 * c * 1000; return((int)distance); }
public static List <Geospatial> GetLocationGeography(String address) { String urlString = MAPS_URI + "geocode/json?address=" + GXUtil.UrlEncode(address) + "&sensor=false"; String ApiKey = ""; if (Config.GetValueOf("GoogleApiKey", out ApiKey)) { urlString += "&key=" + ApiKey; } String response = GXGeolocation.GetContentFromURL(urlString); List <Geospatial> result = new List <Geospatial>(); try { if (!string.IsNullOrEmpty(response)) { StringReader sr = new StringReader(response); JsonTextReader tr = new JsonTextReader(sr); JObject json = (JObject)(tr.DeserializeNext()); if (json.Contains("results")) { JArray results = (JArray)json["results"]; for (int i = 0; i < results.Length; i++) { JObject jo = (JObject)results[i]; if (jo.Contains("geometry")) { JObject geometry = (JObject)jo["geometry"]; if (geometry.Contains("location")) { JObject location = (JObject)geometry["location"]; if (location != null && (location.Contains("lat")) && (location.Contains("lng"))) { Geospatial point = new Geospatial(Convert.ToDecimal(location["lat"]), Convert.ToDecimal(location["lng"])); result.Add(point); } } } } } } } catch (JsonException ex) { GXLogging.Error(log, "getLocation error json:" + response, ex); } return(result); }
public static List <string> GetAddress(String location) { String urlString = MAPS_URI + "geocode/json?latlng=" + GXUtil.UrlEncode(location) + "&sensor=false"; String ApiKey = ""; if (Config.GetValueOf("GoogleApiKey", out ApiKey)) { urlString += "&key=" + ApiKey; } String response = GXGeolocation.GetContentFromURL(urlString); List <string> result = new List <string>(); try { if (!string.IsNullOrEmpty(response)) { StringReader sr = new StringReader(response); JsonTextReader tr = new JsonTextReader(sr); JObject json = (JObject)(tr.DeserializeNext()); if (json.Contains("results")) { JArray results = (JArray)json["results"]; for (int i = 0; i < results.Length; i++) { JObject jo = (JObject)results[i]; if (jo.Contains("formatted_address")) { result.Add((string)jo["formatted_address"]); } } } } } catch (JsonException ex) { GXLogging.Error(log, "getAddress error json:" + response, ex); } return(result); }
public static double GetLongitude(String geolocation) { return(GXGeolocation.GetComponent(geolocation, 1)); }