public GoogleModel Get(GeoCoordinate start, GeoCoordinate end) { fillCoord(start.ToString(), end.ToString()); // Create a request for the URL. WebRequest request = WebRequest.Create(uri); // If required by the server, set the credentials. request.Credentials = CredentialCache.DefaultCredentials; // Get the response. WebResponse response = request.GetResponse(); // Get the stream containing content returned by the server. Stream dataStream = response.GetResponseStream(); // Open the stream using a StreamReader for easy access. StreamReader reader = new StreamReader(dataStream); // Read the content. string responseFromServer = reader.ReadToEnd(); reader.Close(); response.Close(); JObject json = JObject.Parse(responseFromServer); JArray startArr = json.GetValue("origin_addresses").ToObject <JArray>(); JArray endArr = json.GetValue("destination_addresses").ToObject <JArray>(); JArray tmp = json.GetValue("rows").ToObject <JArray>(); JArray tmp2 = tmp[0]["elements"].ToObject <JArray>(); GoogleModel google = new GoogleModel( (int)tmp2[0]["distance"]["value"], (int)tmp2[0]["duration"]["value"], (string)startArr[0], (string)endArr[0] ); return(google); }
public static Position findClosestStation(double departlong, double lattitude) { WebRequestVelibApi test = new WebRequestVelibApi(); List <VelibModel> datas = test.Get(); // for example, using random Paris coordinate var sCoord = new GeoCoordinate(lattitude, departlong); int i = 0; GoogleModel Min = null; Position Arrival = null; foreach (VelibModel data in datas) { if (data.status.Equals("OPEN") && data.available_bikes > 0) { var eCoord = new GeoCoordinate(data.position.lat, data.position.lng); double distance = sCoord.GetDistanceTo(eCoord); if (i < 10 && distance <= 2000) { WebRequestGoogleApi google = new WebRequestGoogleApi(); GoogleModel result = google.Get(sCoord, eCoord); //Cherche la distance minimum if (i == 0) { Min = result; Arrival = new Position(eCoord.Latitude, eCoord.Longitude); } if ((Min != null) && (result.distance < Min.distance)) { Min = result; Arrival = new Position(eCoord.Latitude, eCoord.Longitude); } i++; } } } return(Arrival); }