Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Json;
- using System.IO;
- using System.Threading.Tasks;
- using System.Text;
- using Newtonsoft.Json; // Treba stiahnut nuget package json.net
- namespace Takemehome
- {
- public class RouteClass {
- public int Distance{ get; set;}
- public int Duration{ get; set;}
- public string TextDuration{ get; set;}
- public string TextDistance{ get; set;}
- public double[,] Bounce{ get; set;}
- public async void prepareRequest(/*string[] waypoints,*/ string origin, string destination) {
- //string Finalwaypoints = "";
- StringBuilder sb = new StringBuilder ();
- //Finalwaypoints = string.Join ("|", waypoints);
- sb.Append("https://maps.googleapis.com/maps/api/directions/json?" +
- "mode=" +
- "driving" +
- "&origin=" +
- origin +
- "&destination=" +
- destination/* +
- "&waypoints=" +
- Finalwaypoints*/);
- string _url = sb.ToString();
- JsonValue json = await FetchDataAsync(_url);
- distance = json["routes"][0]["legs"][0]["distance"]["value"];
- duration = json["routes"][0]["legs"][0]["duration"]["value"];
- textDistance = json["routes"][0]["legs"][0]["distance"]["text"];
- textDuration = json["routes"][0]["legs"][0]["duration"]["text"];
- bounce = new double[2,2] {
- {json["routes"][0]["bounds"][0]["northeast"]["lat"], json["routes"][0]["bounds"][0]["northeast"]["lng"]},
- {json["routes"][0]["bounds"][0]["southwest"]["lat"], json["routes"][0]["bounds"][0]["southwest"]["lng"]}
- };
- }
- private async Task<JsonValue> FetchDataAsync(string url) {
- HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create (new Uri (url));
- request.ContentType = "application/json";
- request.Method = "GET";
- // Send the request to the server and wait for the response:
- using (WebResponse response = await request.GetResponseAsync ())
- {
- // Get a stream representation of the HTTP web response:
- using (Stream stream = response.GetResponseStream ())
- {
- // Use this stream to build a JSON document object:
- JsonValue jsonValue = await Task.Run (() => JsonObject.Load (stream));
- // Return the JSON document:
- return jsonValue;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement