Calculate Route
Posted: Mon Jan 09, 2017 12:23 pm
I try to calculate route but always I have this error message - "linking to waypoint (zero-based index list: unknown) failed".
Below is my code.
Below is my code.
Code: Select all
public class Location
{
public Location(double latitude, double longitude)
{
this.Latitude = latitude;
this.Longitude = Longitude;
}
/// <summary>
/// Get or sets latitude.
/// </summary>
public double Latitude { get; set; }
/// <summary>
/// Get or sets longitude.
/// </summary>
public double Longitude { get; set; }
}
public class PtvRoute
{
private XRouteWSClient xRouteClient;
public PtvRoute()
{
xRouteClient = new XRouteWSClient();
xRouteClient.ClientCredentials.UserName.UserName = "UserName";
xRouteClient.ClientCredentials.UserName.Password = "Password";
xRouteClient.Endpoint.Address = new EndpointAddress("https://xroute-eu-n-test.cloud.ptvgroup.com/xroute/ws/XRoute");
(xRouteClient.Endpoint.Binding as BasicHttpBinding).MaxReceivedMessageSize = 4 << 20;
(xRouteClient.Endpoint.Binding as BasicHttpBinding).ReaderQuotas.MaxBytesPerRead = 4 << 20;
// set timeouts
xRouteClient.Endpoint.Binding.SendTimeout = TimeSpan.FromMilliseconds(5000);
xRouteClient.Endpoint.Binding.ReceiveTimeout = TimeSpan.FromMilliseconds(10000);
}
public ExtendedRoute CalcRoute(Location from, Location to)
{
Point startPoint = new Point();
startPoint.point = new PlainPoint { x = from.Longitude, y = from.Latitude };
WaypointDesc wpdStart = new WaypointDesc();
wpdStart.wrappedCoords = new Point[] { startPoint };
wpdStart.linkType = LinkType.AUTO_LINKING;
Point destPoint = new Point();
destPoint.point = new PlainPoint { x = to.Longitude, y = to.Latitude };
WaypointDesc wpdDest = new WaypointDesc();
wpdDest.wrappedCoords = new Point[] { destPoint };
wpdDest.linkType = LinkType.AUTO_LINKING;
ResultListOptions resultOptions = new ResultListOptions()
{
polygon = true,
detailLevel = DetailLevel.STANDARD,
};
CountryInfoVehicleOptions countryInfoVehicleOptions = new CountryInfoVehicleOptions();
countryInfoVehicleOptions.allEuro = true;
countryInfoVehicleOptions.detailedTollCosts = true;
// Define vehicleOptions
countryInfoVehicleOptions.wrappedOptions = new VehicleOption[]{
new VehicleOption{ parameter = VehicleParameter.TYPE, value = "TRU"},
new VehicleOption{ parameter = VehicleParameter.TOTAL_WEIGHT, value = "15000"},
//new XRoute.VehicleOption{ parameter = VehicleParameter.TRAILER_WEIGHT, value = "20000"},
//new XRoute.VehicleOption{ parameter = VehicleParameter.NUMBER_OF_AXLES, value = "4"},
new VehicleOption{ parameter = VehicleParameter.EMISSION_CLASS, value = "EURO_4"},
//new XRoute.VehicleOption{ parameter = XRoute.VehicleParameter.HEIGHT, value = "400"},
//new XRoute.VehicleOption{ parameter = XRoute.VehicleParameter.LENGTH, value = "2000"}
};
CallerContext context = new CallerContext()
{
wrappedProperties = new CallerContextProperty[]
{
new CallerContextProperty() { key = "CoordFormat", value = "PTV_SMARTUNITS" },
new CallerContextProperty() { key = "ResponseGeometry", value = "WKB" }
}
};
// Step 2.2: Set both way-points into the specific array WaypointDesc[]
WaypointDesc[] waypointDescs = new WaypointDesc[] { wpdStart, wpdDest };
var route = xRouteClient.calculateExtendedRoute(waypointDescs, null, null, resultOptions, countryInfoVehicleOptions, context);
return route;
}
}
class Program
{
static void Main(string[] args)
{
PtvRoute routing = new PtvRoute();
try
{
var route = routing.CalcRoute(new Location(42.708501, 23.332621), new Location(42.658578, 23.316328));
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}