Skip to content

Instantly share code, notes, and snippets.

@xivk
Created May 30, 2014 15:45
Show Gist options
  • Save xivk/78b0b309f2cce3e80428 to your computer and use it in GitHub Desktop.
Save xivk/78b0b309f2cce3e80428 to your computer and use it in GitHub Desktop.
using Newtonsoft.Json;
using OsmSharp.Math.Geo;
using OsmSharp.Routing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace OsmSharp.Service.Client.PCL
{
public static class RoutingFacade
{
public static Route Calculate(Vehicle vehicle, GeoCoordinate from, GeoCoordinate to)
{
var requestData = "{ " +
"\"Type\": \"Regular\", " +
"\"ReturnType\": \"Route\", " +
"\"Vehicle\": \"{vehicle}\", " +
"\"Hooks\": [ " +
"{ " +
"\"Id\": 1, " +
"\"Latitude\": {lat_from}, " +
"\"Longitude\": {lon_from}, " +
"\"Tags\": [ " +
"{ " +
" \"Key\": \"key\", " +
" \"Value\": \"value\" " +
"} " +
"] " +
"}, " +
"{ " +
"\"Id\": 2, " +
"\"Latitude\": {lat_to}, " +
"\"Longitude\": {lon_to}, " +
"\"Tags\": [ " +
"{ " +
" \"Key\": \"key\", " +
" \"Value\": \"value\" " +
"} " +
"] " +
"} " +
"] " +
"} ";
var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:666/routing?format=json");
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
var json = requestData.Replace("{vehicle}", vehicle.UniqueName).Replace("{lat_from}", from.Latitude.ToInvariantString()).Replace("{lon_from}", from.Longitude.ToInvariantString())
.Replace("{lat_to}", to.Latitude.ToInvariantString()).Replace("{lon_to}", to.Longitude.ToInvariantString());
streamWriter.Write(json);
streamWriter.Flush();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
string routeJSON = string.Empty;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
routeJSON= streamReader.ReadToEnd();
}
return JsonConvert.DeserializeObject<Route>(routeJSON);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment