microsoft / BingMapsRESTToolkit

This is a portable class library which makes it easy to access the Bing Maps REST services from .NET.

Home Page:https://github.com/Microsoft/BingMapsRESTToolkit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[RouteRequest] Truck sync version

knopa opened this issue · comments

Is there a way to get truck sync version api in sdk?

Seems by code i see only truck async implementation which is nice but for 2 points it takes 6s, when sync version took only 2s

The sync/async used to take about the same amount of time (async slightly longer) but the difference wasn't as much as you are noting.

Truck routing requests get big fast as there is a lot of extra parameters specific to truck routing. Even with two coordinates, you can easily exceed the max URL length of 2048 characters. Most production apps I have worked on ended up having to use async for truck routing because of this.

This library only uses async for truck routing as it will always work (this library does this for other services where async is an option).

Extending this library to support sync for truck routing is possible, but not something likely to make it into the nuget package anytime soon as that hasn't been updated in years. That said, you could create a single class in your app that extends from the BaseRestRequest class and create your own. You might however want to look at a simpler request class than the routing one as that has a ton of custom logic in it to support routes with any number of waypoints (you can literally request a route with a million waypoints).

I mean
Truck Route Example
vs
Truck Route Asynchronous Example

Async version made 3 request
Sync obviosly do 1 request - Regarding GET limitation as I see in documentation both way support POST type too

image

a little offtop
Also the limitation of GET - it's only browser one not API

which you mention it's Edge limitation which are not applies to microsoft servers I hope)
even in other kind of browsers limits are very different

Chrome able handle up to 2 097 152
Firefox 65 536 or even more (visual limitation)
Safari up to 80 000

Btw is it possible to merge if I do PR in that case?

The URL limit applies everywhere for this REST service, not just Edge browser.

Here is an example simple truck routing request class you can use in your code:

using System;
using System.Text;

namespace BingMapsRESTToolkit
{
    /// <summary>
    /// Simple truck routing request.
    /// </summary>
    public class SimpleTruckRouteRequest : BaseRestRequest
    {

        #region Public Properties

		/// <summary>
        /// Specifies two or more locations that define the route and that are in sequential order. 
        /// A route is defined by a set of waypoints and viaWaypoints (intermediate locations that the route must pass through). 
        /// The start and end points of the route cannot be viaWaypoints.
        public List<SimpleWaypoint> Waypoints { get; set; }
		
		/// <summary>
        /// Options to use when calculate route.
        /// </summary>
        public RouteOptions RouteOptions { get; set; }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets the request URL. 
        /// </summary>
        /// <returns></returns>
        public override string GetRequestUrl()
        {
			if (Waypoints == null)
            {
                throw new Exception("Waypoints not specified.");
            }
            else if (Waypoints.Count < 2)
            {
                throw new Exception("Not enough Waypoints specified.");
            }
            else if (Waypoints[0].IsViaPoint || Waypoints[Waypoints.Count - 1].IsViaPoint)
            {
                throw new Exception("Start and end waypoints must not be ViaWaypoints.");
            }

			var sb = new StringBuilder(this.Domain); 
            sb.Append("Routes/Truck");
			
			int wayCnt = 0, viaCnt = 0;
			
			for (int i = 0; i < Waypoints.Count; i++)
			{
				if (Waypoints[i].IsViaPoint)
				{
					sb.AppendFormat("&vwp.{0}=", i);
					viaCnt++;
				}
				else
				{
					sb.AppendFormat("&wp.{0}=", i);

					wayCnt++;
					viaCnt = 0;
				}

				if (Waypoints[i].Coordinate != null)
				{
					sb.AppendFormat(CultureInfo.InvariantCulture, "{0:0.#####},{1:0.#####}", Waypoints[i].Coordinate.Latitude, Waypoints[i].Coordinate.Longitude);
				}
				else
				{
					sb.AppendFormat("{0}", Uri.EscapeDataString(Waypoints[i].Address));
				}
			}
			
			if (RouteOptions != null)
			{
				sb.Append(RouteOptions.GetUrlParam(startIdx));
			}
			
			sb.Append(GetBaseRequestUrl());

            return sb.ToString();
        }

        #endregion
    }
}

You can then use this like this:

//Create a request.
var request = new SimpleTruckRouteRequest()
{
    	Waypoints = new List<SimpleWaypoint>(){
		new SimpleWaypoint("Seattle, WA"),
		new SimpleWaypoint("Bellevue, WA"),
		new SimpleWaypoint("Redmond, WA"),
		new SimpleWaypoint("Kirkland, WA")
	},
	RouteOptions = new RouteOptions()
	{
		Optimize = RouteOptimizationType.TimeWithTraffic
	},
    BingMapsKey = "YOUR_BING_MAPS_KEY"
};

//Process the request by using the ServiceManager.
var response = await request.Execute();

Merges of pull request sometimes happen. The NuGet however isn't being updated anymore. I handed this project off about 5 years ago (I'm not on the Bing Maps team) and I think one update was made after that and then it's been left as-is.

Thanks for reply, it makes clear how to extend now.

You already has a class to push into nuget)

Have a nice a day.