public void GetDistanceMatrixResult()
        {
            var am  = new AzureMapsToolkit.AzureMapsServices(_KEY);
            var req = new RouteMatrixRequest();

            req.RouteType = RouteType.Fastest;

            var origins = new List <Coordinate> {
                new Coordinate {
                    Longitude = 4.85106f, Latitude = 52.36006f
                }
                , new Coordinate {
                    Longitude = 4.85056f, Latitude = 52.36187f
                }
            };


            var destinations = new List <Coordinate> {
                new Coordinate {
                    Longitude = 4.85003f, Latitude = 52.36241f
                }
                , new Coordinate {
                    Longitude = 13.42937f, Latitude = 52.50931f
                }
            };

            var result = am.GetRouteMatrix(req, origins, destinations).Result;

            var matrixResponse = am.GetRouteMatrixResult(result.ResultUrl).Result;

            Assert.Null(matrixResponse.Error);
            Assert.NotNull(matrixResponse.Result);
        }
Exemplo n.º 2
0
        public void GetDistanceMatrix()
        {
            var am  = new AzureMapsToolkit.AzureMapsServices(_KEY);
            var req = new RouteMatrixRequest();

            req.RouteType = RouteType.Fastest;

            var origins = new List <Coordinate> {
                new Coordinate {
                    Longitude = 4.85106f, Latitude = 52.36006f
                }
                , new Coordinate {
                    Longitude = 4.85056f, Latitude = 52.36187f
                }
            };

            var destinations = new List <Coordinate> {
                new Coordinate {
                    Longitude = 4.85003f, Latitude = 52.36241f
                }
                , new Coordinate {
                    Longitude = 13.42937f, Latitude = 52.50931f
                }
            };

            var result = am.GetRouteMatrix(req, origins, destinations).Result;

            Assert.Null(result.ex);

            Assert.NotNull(result.matrix);

            Assert.True(result.matrix.Matrix.Length > 0);

            Assert.Equal(495, result.matrix.Matrix[0][0].Response.RouteSummary.LengthInMeters);
        }
Exemplo n.º 3
0
        /// <summary>
        /// The Matrix Routing service allows calculation of a matrix of route summaries for a set of routes defined by origin and destination locations. For every given origin, this service calculates the cost of routing from that origin to every given destination. The set of origins and the set of destinations can be thought of as the column and row headers of a table and each cell in the table contains the costs of routing from the origin to the destination for that cell. For each route, the travel times and distances are calculated. You can use the computed costs to determine which routes to calculate using the Routing Directions API. If the computation takes longer than 20 seconds or forceAsyn parameter in the request is set to true, this API returns a 202 response code along a redirect URL in the Location field of the response header. This URL should be checked periodically until the response data or error information is available.
        /// The asynchronous responses are stored for 14 days. The redirect URL returns a 400 response if used after the expiration period.
        /// </summary>
        /// <param name="routeMatrixRequest"></param>
        /// <param name="coordinatesOrigins"></param>
        /// <param name="coordinatesDestinations"></param>
        /// <returns></returns>
        public virtual async Task <(RouteMatrixResponse matrix, Exception ex)> GetRouteMatrix(RouteMatrixRequest routeMatrixRequest, IEnumerable <Coordinate> coordinatesOrigins, IEnumerable <Coordinate> coordinatesDestinations)
        {
            try
            {
                var url = $"https://atlas.microsoft.com/route/matrix/json?subscription-key={Key}";
                url += GetQuery <RouteMatrixRequest>(routeMatrixRequest, true);

                var originPoints      = GetMultipPoint(coordinatesOrigins);
                var destinationsPoint = GetMultipPoint(coordinatesDestinations);

                var body = new
                {
                    origins      = originPoints,
                    destinations = destinationsPoint
                };

                string data = Newtonsoft.Json.JsonConvert.SerializeObject(body);

                using (var response = await GetHttpResponseMessage(url, data))
                {
                    using (var responseMessage = response.Content)
                    {
                        var responseData = await responseMessage.ReadAsStringAsync();

                        var matrix = Newtonsoft.Json.JsonConvert.DeserializeObject <RouteMatrixResponse>(responseData);
                        return(matrix, null);
                    }
                }
            }
            catch (Exception ex)
            {
                return(null, ex);
            }
        }