Exemplo n.º 1
0
        /// <summary>
        /// This method has a lot of logic that is specific to the map type.
        /// To process a request you can easily just call the Execute method on the request.
        /// This will build much of the SimioMapRoute object.
        /// </summary>
        /// <param name="request"></param>
        public bool GetRoute(string mapsKey, string originAddress, string destinationAddress,
                             out SimioMapRoute mapRoute,
                             out string requestUrl, out string explanation)
        {
            explanation = "";
            requestUrl  = "";
            mapRoute    = null;
            try
            {
                mapRoute = new SimioMapRoute(originAddress, destinationAddress);
                // Build a list of our two waypoints (from and to)
                List <SimpleWaypoint> wpList = new List <SimpleWaypoint>();
                wpList.Add(new SimpleWaypoint(originAddress));      //e.g. "Pittsburgh, PA"));
                wpList.Add(new SimpleWaypoint(destinationAddress)); // e.g. "Sewickley, PA"));

                List <RouteAttributeType> routeAttributes = new List <RouteAttributeType>();
                routeAttributes.Add(RouteAttributeType.RoutePath);

                // Construct the request and attributes
                var request = new RouteRequest()
                {
                    BingMapsKey = mapsKey,
                    Waypoints   = wpList
                };

                request.RouteOptions = new RouteOptions();
                request.RouteOptions.RouteAttributes = routeAttributes;

                requestUrl = request.ToString();

                var start = DateTime.Now;

                // Async. Execute the request.
                var task = Task <Response> .Run(async() =>
                {
                    return(await request.Execute());
                });

                Response r2 = task.Result;

                // Check if we got a good response
                if (r2 != null && r2.ResourceSets != null &&
                    r2.ResourceSets.Length > 0 &&
                    r2.ResourceSets[0].Resources != null &&
                    r2.ResourceSets[0].Resources.Length > 0)
                {
                    ResourceSet     rs          = (ResourceSet)r2.ResourceSets[0];
                    Route           route       = (Route)rs.Resources[0];
                    RouteLeg[]      legs        = route.RouteLegs;
                    ItineraryItem[] itineraries = legs[0].ItineraryItems;
                    ItineraryItem   itinItem    = itineraries[2];
                    string          bb          = route.BoundingBox.ToString();

                    mapRoute.SegmentList.Clear();

                    // We could make the bounding box from the one that Bing Maps sends us,
                    // but we're going to do our own to match the usa 'map'.
                    ////PointF ptLoc = new PointF((float)route.BoundingBox[1], (float)route.BoundingBox[0]);
                    ////float width = (float)(route.BoundingBox[3] - route.BoundingBox[1]);
                    ////float height = (float)(route.BoundingBox[2] - route.BoundingBox[0]);

                    ////// We're going to bound according to the contiguous USA, which is appox.
                    ////// lat 20 to 50, and lon -60 to -130
                    ////PointF ptLoc = transform.LonLatBoundingBox.Location; // new PointF( -130f, 20f);
                    ////float width = lonlatBox.Width; // 70f;
                    ////float height = lonlatBox.Height; // 30f;

                    ////// Turning the thing on its side, since we want latitude to be 'Y'
                    ////mapData.LonLatBoundingBox = new RectangleF(ptLoc.X, ptLoc.Y, width, height);
                    ////mapData.Origin = new PointF(ptLoc.X + width / 2f, ptLoc.Y + height / 2f);

                    ////mapData.SimioScaling = simioScaling;

                    // Build something for the form's 'result textbox
                    StringBuilder sb = new StringBuilder();

                    // Create segments from the itineraries, and pick up the indices
                    // that reference the RoutePath array of lat,lon coordinates.
                    // We are assuming a single itinerary. See Bing Maps for for info.
                    for (var ii = 0; ii < itineraries.Length; ii++)
                    {
                        ItineraryItem item = itineraries[ii];

                        if (route.RoutePath != null)
                        {
                            int idxStart = item.Details[0].StartPathIndices[0];
                            int idxEnd   = item.Details[0].EndPathIndices[0];

                            double        lat     = route.RoutePath.Line.Coordinates[idxStart][0];
                            double        lon     = route.RoutePath.Line.Coordinates[idxStart][1];
                            MapCoordinate mcStart = new MapCoordinate(lat, lon);

                            lat = route.RoutePath.Line.Coordinates[idxEnd][0];
                            lon = route.RoutePath.Line.Coordinates[idxEnd][1];

                            MapCoordinate mcEnd   = new MapCoordinate(lat, lon);
                            MapSegment    segment = null;
                            if (ii == 0)
                            {
                                segment = mapRoute.AddFirstSegment(mcStart, mcEnd);
                            }
                            else
                            {
                                segment = mapRoute.AppendSegment(mcEnd);
                            }

                            // Now add Bing-specific info
                            segment.Distance = item.TravelDistance;
                            segment.Duration = item.TravelDuration;
                        }
                        sb.AppendLine($"Compass={item.CompassDirection} Distance={item.TravelDistance} >> {item.Instruction.Text}");
                    } // for each itinerary

                    explanation = sb.ToString();
                    return(true);
                }
                else
                {
                    explanation = "No results found.";
                    return(false);
                }
            }
            catch (Exception ex)
            {
                explanation = $"Err={ex.Message}";
                return(false);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// An asynchronous fetch of a map route (untested)
        /// </summary>
        /// <param name="mapData"></param>
        /// <param name="googleMapsKey"></param>
        /// <param name="StartStopList"></param>
        public bool GetRoute(string googleMapsKey, string originAddress, string destinationAddress,
                             out SimioMapRoute mapRoute,
                             out string requestUrl, out string explanation)
        {
            requestUrl  = "";
            explanation = "";
            mapRoute    = null;
            try
            {
                mapRoute = new SimioMapRoute(originAddress, destinationAddress);

                DirectionsRequest request = new DirectionsRequest
                {
                    Key = googleMapsKey,

                    Origin      = new Location(originAddress),
                    Destination = new Location(destinationAddress)
                };

                requestUrl = request.ToString();
                var response = GoogleApi.GoogleMaps.Directions.Query(request);

                mapRoute.SegmentList.Clear();

                // .. wait for response ..
                // Response has Routes, and Routes have legs. Typically 1-2 routes
                foreach (Route route in response.Routes)
                {
                    // Route has an overview summary and boundingbox
                    // We could make the bounding box from the one that Bing Maps sends us,
                    //but we're going to do our own to match the usa 'map'.
                    ////PointF ptLoc = new PointF((float)route.Bounds.NorthEast.Latitude, (float)route.Bounds.NorthEast.Longitude);
                    ////float width = (float)(route.Bounds.NorthEast.Latitude - route.Bounds.SouthWest.Latitude);
                    ////float height = (float)(route.Bounds.NorthEast.Longitude - route.Bounds.SouthWest.Longitude);

                    ////// We're going to bound according to the contiguous USA, which is appox.
                    ////// lat 20 to 50, and lon -60 to -130
                    ////PointF ptLoc = lonlatBox.Location; // new PointF( -130f, 20f);
                    ////float width = lonlatBox.Width; // 70f;
                    ////float height = lonlatBox.Height; // 30f;

                    ////// Turning the thing on its side, since we want latitude to be 'Y'
                    ////mapData.LonLatBoundingBox = new RectangleF(ptLoc.X, ptLoc.Y, width, height);
                    ////mapData.Origin = new PointF(ptLoc.X + width / 2f, ptLoc.Y + height / 2f);

                    ////mapData.SimioScaling = simioScaling;

                    int           ii = 0;
                    StringBuilder sb = new StringBuilder();
                    foreach (Leg leg in route.Legs)
                    {
                        // Legs have distance and duration
                        // and StartLocation and address, and EndLocation and address
                        foreach (Step step in leg.Steps)
                        {
                            double        lat     = step.StartLocation.Latitude;
                            double        lon     = step.StartLocation.Longitude;
                            MapCoordinate mcStart = new MapCoordinate(lat, lon);

                            lat = step.EndLocation.Latitude;
                            lon = step.EndLocation.Longitude;

                            MapCoordinate mcEnd   = new MapCoordinate(lat, lon);
                            MapSegment    segment = null;

                            if (ii == 0)
                            {
                                segment = mapRoute.AddFirstSegment(mcStart, mcEnd);
                            }
                            else
                            {
                                segment = mapRoute.AppendSegment(mcEnd);
                            }

                            // Now add Google-specific information
                            segment.Distance = step.Distance.Value;
                            segment.Duration = step.Duration.Value;

                            ii++;
                            sb.AppendLine($"Instructions={step.HtmlInstructions} Distance={step.Distance.Text} >> {step.Duration.Text}");
                        } // foreach step
                    }     // foreach leg
                    explanation = sb.ToString();
                }
                return(true);
            }
            catch (Exception ex)
            {
                explanation = $"From={originAddress} To={destinationAddress} Err={ex}";
                return(false);
            }
        }