Advertisement
Guest User

Untitled

a guest
Nov 28th, 2015
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Json;
  4. using System.IO;
  5. using System.Threading.Tasks;
  6. using System.Text;
  7. using Newtonsoft.Json; // Treba stiahnut nuget package json.net
  8.  
  9. namespace Takemehome
  10. {
  11.     public class RouteClass {
  12.  
  13.         public int Distance{ get; set;}
  14.         public int Duration{ get; set;}
  15.         public string TextDuration{ get; set;}
  16.         public string TextDistance{ get; set;}
  17.         public double[,] Bounce{ get; set;}
  18.  
  19.         public async void prepareRequest(/*string[] waypoints,*/ string origin, string destination) {
  20.             //string Finalwaypoints = "";
  21.             StringBuilder sb = new StringBuilder ();
  22.             //Finalwaypoints = string.Join ("|", waypoints);
  23.             sb.Append("https://maps.googleapis.com/maps/api/directions/json?" +
  24.                 "mode=" +
  25.                 "driving" +
  26.                 "&origin=" +
  27.                 origin +
  28.                 "&destination=" +
  29.                 destination/* +
  30.                 "&waypoints=" +
  31.                 Finalwaypoints*/);
  32.             string _url = sb.ToString();
  33.             JsonValue json = await FetchDataAsync(_url);
  34.             distance = json["routes"][0]["legs"][0]["distance"]["value"];
  35.             duration = json["routes"][0]["legs"][0]["duration"]["value"];
  36.             textDistance = json["routes"][0]["legs"][0]["distance"]["text"];
  37.             textDuration = json["routes"][0]["legs"][0]["duration"]["text"];
  38.             bounce = new double[2,2] {
  39.                 {json["routes"][0]["bounds"][0]["northeast"]["lat"], json["routes"][0]["bounds"][0]["northeast"]["lng"]},
  40.                 {json["routes"][0]["bounds"][0]["southwest"]["lat"], json["routes"][0]["bounds"][0]["southwest"]["lng"]}
  41.             };
  42.         }
  43.  
  44.         private async Task<JsonValue> FetchDataAsync(string url) {
  45.             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create (new Uri (url));
  46.             request.ContentType = "application/json";
  47.             request.Method = "GET";
  48.  
  49.             // Send the request to the server and wait for the response:
  50.             using (WebResponse response = await request.GetResponseAsync ())
  51.             {
  52.                 // Get a stream representation of the HTTP web response:
  53.                 using (Stream stream = response.GetResponseStream ())
  54.                 {
  55.                     // Use this stream to build a JSON document object:
  56.                     JsonValue jsonValue = await Task.Run (() => JsonObject.Load (stream));
  57.  
  58.                     // Return the JSON document:
  59.                     return jsonValue;
  60.                 }
  61.             }      
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement