Ejemplo n.º 1
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);
            }
        }