public static Dictionary <string, string> GetDistance(string origin, string destination, string key, string waipoint = "") { Dictionary <string, string> result = new Dictionary <string, string>(); try { string apiUrl = "https://maps.googleapis.com/maps/api/directions/json?origin={0}&destination={1}{2}&mode=driving&sensor=false&key={3}"; apiUrl = string.Format(apiUrl, origin, destination, waipoint, key); WebRequest request = HttpWebRequest.Create(apiUrl); WebResponse response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); JavaScriptSerializer parser = new JavaScriptSerializer(); string responseStringData = reader.ReadToEnd(); RootObject responseData = parser.Deserialize <RootObject>(responseStringData); if (responseData != null) { if (responseData.routes.Count > 0) { Leg leg = responseData.routes[0].legs[0]; result.Add("start_address", leg.start_address); result.Add("start_location_lat", leg.start_location.lat.ToString().Replace(',', '.')); result.Add("start_location_lng", leg.start_location.lng.ToString().Replace(',', '.')); result.Add("end_address", leg.end_address); result.Add("end_location_lat", leg.end_location.lat.ToString().Replace(',', '.')); result.Add("end_location_lng", leg.end_location.lng.ToString().Replace(',', '.')); result.Add("distance", leg.distance.text); result.Add("distance2", leg.distance.value.ToString()); result.Add("duration", leg.duration.text); double distance = responseData.routes.Sum(r => r.legs.Sum(l => l.distance.value)); if (distance == 0) { DistanceZero(result); } } else { DistanceZero(result); } } else { DistanceZero(result); } } catch (Exception ex) { MessageBox.Show(ex.Message); } return(result); }
public static Dictionary <string, string> GetDistance(string origin, string destination, string key) { Dictionary <string, string> result = new Dictionary <string, string>(); try { string apiUrl = "https://maps.googleapis.com/maps/api/directions/json?origin={0},&destination={1}&sensor=false&key={2}"; apiUrl = string.Format(apiUrl, origin, destination, key); WebRequest request = HttpWebRequest.Create(apiUrl); WebResponse response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); JavaScriptSerializer parser = new JavaScriptSerializer(); string responseStringData = reader.ReadToEnd(); RootObject responseData = parser.Deserialize <RootObject>(responseStringData); if (responseData != null) { Leg leg = responseData.routes[0].legs[0]; result.Add("start_address", leg.start_address); result.Add("start_location_lat", leg.start_location.lat.ToString().Replace(',', '.')); result.Add("start_location_lng", leg.start_location.lng.ToString().Replace(',', '.')); result.Add("end_address", leg.end_address); result.Add("end_location_lat", leg.end_location.lat.ToString().Replace(',', '.')); result.Add("end_location_lng", leg.end_location.lng.ToString().Replace(',', '.')); result.Add("distance", leg.distance.text); result.Add("duration", leg.duration.text); double distance = responseData.routes.Sum(r => r.legs.Sum(l => l.distance.value)); if (distance == 0) { throw new Exception("Google cannot find road route"); } return(result); } else { throw new Exception("Unable to get location from google"); } } catch (Exception ex) { throw ex; } }
public Leg GetLegByGoogleApi(IPoint startPoint, IPoint endPoint) { Leg leg = new Leg(); string origin = $"origin={startPoint.Lat},{startPoint.Lng}"; string destination = $"destination={endPoint.Lat},{endPoint.Lng}"; string url = googleDirectionUrl + origin + "&" + destination + "&key=" + googleApiKey; WebRequest request = WebRequest.Create(url); request.ContentType = "application/json; charset=utf-8"; request.Method = WebRequestMethods.Http.Get; WebResponse response = request.GetResponse(); Stream data = response.GetResponseStream(); StreamReader reader = new StreamReader(data); string responseFromServer = reader.ReadToEnd(); reader.Close(); data.Close(); response.Close(); JObject jObj = JObject.Parse(responseFromServer); JToken jRoute = jObj["routes"].FirstOrDefault(); JToken jLeg = jRoute.SelectToken("legs").FirstOrDefault(); leg.Polyline = jRoute.SelectToken("overview_polyline").SelectToken("points").ToString(); leg.Distance = (int)jLeg.SelectToken("distance").SelectToken("value"); leg.Duration = (int)jLeg.SelectToken("duration").SelectToken("value"); return(leg); }
public IGoogleOptimalRouteResponce GetGoogleOptimalRoute(IPoint startPoint, IPoint endPoint, IEnumerable <IPoint> wayPoints, string options = "") { string origin = "", destination = "", wayPointsStr = ""; origin = $"origin={startPoint.Lat},{startPoint.Lng}"; destination = $"&destination={endPoint.Lat},{endPoint.Lng}"; foreach (var point in wayPoints) { if (string.IsNullOrEmpty(wayPointsStr)) { wayPointsStr += $"&waypoints=optimize:true|{point.Lat},{point.Lng}"; } else { wayPointsStr += $"|{point.Lat},{point.Lng}"; } } string url = googleDirectionUrl + origin + destination + wayPointsStr + options + "&key=" + googleApiKey; WebRequest request = WebRequest.Create(url); request.ContentType = "application/json; charset=utf-8"; request.Method = WebRequestMethods.Http.Get; WebResponse webResponse = request.GetResponse(); Stream data = webResponse.GetResponseStream(); StreamReader reader = new StreamReader(data); string responseFromServer = reader.ReadToEnd(); reader.Close(); data.Close(); webResponse.Close(); JObject jObj = JObject.Parse(responseFromServer); JToken jRoute = jObj["routes"].FirstOrDefault(); string polyline = jRoute.SelectToken("overview_polyline").SelectToken("points").ToString(); var jLegs = jRoute.SelectToken("legs"); var legs = new List <Leg>(); foreach (var jLeg in jLegs) { Leg leg = new Leg(); leg.Distance = (int)jLeg.SelectToken("distance").SelectToken("value"); leg.Duration = (int)jLeg.SelectToken("duration").SelectToken("value"); legs.Add(leg); } var waypoint_order = jRoute.SelectToken("waypoint_order").Select(x => (int)x).ToList(); return(new GoogleOptimalRouteResponce() { Polyline = polyline, WaypointOrder = waypoint_order, Legs = legs }); }