Esempio n. 1
0
        /// <summary>
        /// Tries to geocode a simple waypoint.
        /// </summary>
        /// <param name="waypoint">The simple waypoint to geocode.</param>
        /// <param name="baseRequest">A base request that has the information need to perform a geocode, primarily a Bing Maps key.</param>
        /// <returns>A Task in which the simple waypoint will be geocoded.</returns>
        public static async Task TryGeocode(SimpleWaypoint waypoint, BaseRestRequest baseRequest)
        {
            if (waypoint != null && waypoint.Coordinate == null && !string.IsNullOrEmpty(waypoint.Address))
            {
                var request = new GeocodeRequest()
                {
                    Query        = waypoint.Address,
                    MaxResults   = 1,
                    BingMapsKey  = baseRequest.BingMapsKey,
                    Culture      = baseRequest.Culture,
                    Domain       = baseRequest.Domain,
                    UserIp       = baseRequest.UserIp,
                    UserLocation = baseRequest.UserLocation,
                    UserMapView  = baseRequest.UserMapView,
                    UserRegion   = baseRequest.UserRegion
                };

                try
                {
                    var r = await ServiceManager.GetResponseAsync(request).ConfigureAwait(false);

                    if (Response.HasResource(r))
                    {
                        var l = Response.GetFirstResource(r) as Location;

                        waypoint.Coordinate = new Coordinate(l.Point.Coordinates[0], l.Point.Coordinates[1]);
                    }
                }
                catch
                {
                    //Do nothing.
                }
            }
        }
        /// <summary>
        /// Tries to geocode a simple waypoint.
        /// </summary>
        /// <param name="waypoint">The simple waypoint to geocode.</param>
        /// <param name="bingMapsKey">The Bing Maps key to use when geocoding.</param>
        /// <returns>A Task in which the simple waypoint will be geocoded.</returns>
        public static async Task TryGeocode(SimpleWaypoint waypoint, string bingMapsKey)
        {
            var request = new GeocodeRequest()
            {
                BingMapsKey = bingMapsKey
            };

            await TryGeocode(waypoint, request).ConfigureAwait(false);
        }
Esempio n. 3
0
        /// <summary>
        /// Geocodes the origins and destinations.
        /// </summary>
        /// <returns>A task for geocoding the origins and destinations.</returns>
        public async Task GeocodeWaypoints()
        {
            //Ensure all the origins are geocoded.
            if (Origins != null)
            {
                await SimpleWaypoint.GeocodeWaypoints(Origins, this);
            }

            //Ensure all the destinations are geocoded.
            if (Destinations != null)
            {
                await SimpleWaypoint.GeocodeWaypoints(Destinations, this);
            }
        }
        /// <summary>
        /// Creates a NxN distance matrix with straight line distances.
        /// </summary>
        /// <param name="waypoints">The waypoints to generate a matrix for.</param>
        /// <param name="distanceUnits">The distance units to calculate the distances in.</param>
        /// <param name="bingMapsKey">A bing maps key that can be used to geocode waypoints, if needed.</param>
        /// <returns>A NxN distance matrix with straight line distances.</returns>
        public static async Task <DistanceMatrix> CreateStraightLineNxNMatrix(List <SimpleWaypoint> waypoints, DistanceUnitType distanceUnits, string bingMapsKey)
        {
            //Ensure all the waypoints are geocoded.
            if (waypoints == null || waypoints.Count < 2)
            {
                throw new Exception("Not enough Waypoints specified.");
            }

            if (!string.IsNullOrEmpty(bingMapsKey))
            {
                await SimpleWaypoint.TryGeocodeWaypoints(waypoints, bingMapsKey);
            }

            var numWaypoints = waypoints.Count;

            var cells = new DistanceMatrixCell[numWaypoints * numWaypoints];

            await Task.Run(() => Parallel.For(0, numWaypoints, i =>
            {
                for (var j = 0; j < numWaypoints; j++)
                {
                    double distance = -1;

                    if (i != j && waypoints[i].Coordinate != null && waypoints[j].Coordinate != null)
                    {
                        distance = SpatialTools.HaversineDistance(waypoints[i].Coordinate, waypoints[j].Coordinate, distanceUnits);
                    }

                    cells[i * numWaypoints + j] = new DistanceMatrixCell()
                    {
                        OriginIndex      = i,
                        DestinationIndex = j,
                        TravelDistance   = distance
                    };
                }
            }));

            return(new DistanceMatrix()
            {
                Origins = waypoints,
                Destinations = waypoints,
                Results = cells
            });
        }
Esempio n. 5
0
        /// <summary>
        /// Tries to geocode a simple waypoint.
        /// </summary>
        /// <param name="waypoint">The simple waypoint to geocode.</param>
        /// <param name="baseRequest">A base request that has the information need to perform a geocode, primarily a Bing Maps key.</param>
        /// <returns>A Task in which the simple waypoint will be geocoded.</returns>
        internal static Task TryGeocode(SimpleWaypoint waypoint, BaseRestRequest baseRequest)
        {
            return(new Task(async() =>
            {
                if (waypoint != null && waypoint.Coordinate == null && !string.IsNullOrEmpty(waypoint.Address))
                {
                    var request = new GeocodeRequest()
                    {
                        Query = waypoint.Address,
                        MaxResults = 1,
                        BingMapsKey = baseRequest.BingMapsKey,
                        Culture = baseRequest.Culture,
                        Domain = baseRequest.Domain,
                        UserIp = baseRequest.UserIp,
                        UserLocation = baseRequest.UserLocation,
                        UserMapView = baseRequest.UserMapView,
                        UserRegion = baseRequest.UserRegion
                    };

                    try
                    {
                        var r = await ServiceManager.GetResponseAsync(request);

                        if (r != null && r.ResourceSets != null &&
                            r.ResourceSets.Length > 0 &&
                            r.ResourceSets[0].Resources != null &&
                            r.ResourceSets[0].Resources.Length > 0)
                        {
                            var l = r.ResourceSets[0].Resources[0] as Location;

                            waypoint.Coordinate = new Coordinate(l.Point.Coordinates[0], l.Point.Coordinates[1]);
                        }
                    }
                    catch
                    {
                        //Do nothing.
                    }
                }
            }));
        }