Esempio n. 1
0
        //aarnold 4/9

        //asynchronous method that will cause the data to be sent and receive asyn. 
        //tsage 4/2 PDT I.B.1.h.(4)
        private async Task<List<Maneuver>> GetDataAsync(bool shortestTime, List<DirectionsRequest.Avoids> toAvoid, StaticMap_Request_Classes.StaticMap.MapType typeOfMap) {
            //aarnold 4/2 Project Dev Tree - II.E.1 and 2 
            Loading();
            UserManeuvers.Clear();
            Narrative.Clear();

            List<Locations> Loc = new List<Locations>();
            List<Locations> TempLoc = new List<Locations>();
            foreach (AddressLine items in Addresses.ItemContainerGenerator.Items)
            {
                if (items.AddressLine1.Text == "Address Line 1") items.AddressLine1.Text = "";
                if (items.City.Text == "City") items.City.Text = "";
                if (items.ZIP.Text == "Zip Code") items.ZIP.Text = "";
                Address tempAddress = new Address(items.AddressLine1.Text, items.City.Text, items.State.Text, items.ZIP.Text);
                Task<List<Locations>> TLoc = tempAddress.getLocations();
                TempLoc = await TLoc;
                if (TempLoc.Count > 0)
                {
                    Loc.Add(TempLoc[0]);
                }                
            }

            //tsage end 4/2
            Directions UserInput = new Directions();

            if(Loc.Count != 0)
                if (shortestTime == true)
                {
                    //aarnold 4/16
                    UserInput = await Task.Run(() =>DirectionsRequest.getDirections(Loc, typeOfMap, DirectionsRequest.RouteType.Fastest, DirectionsRequest.DrivingStyle.Normal, toAvoid));
                }
                else
                {
                    //aarnold 4/16
                    UserInput = await Task.Run(() =>DirectionsRequest.getDirections(Loc, typeOfMap, DirectionsRequest.RouteType.Shortest, DirectionsRequest.DrivingStyle.Normal, toAvoid));
                }

            //end aarnold 4/2
            if (UserInput.legs.Count != 0)
            {
                try
                {
                    MainMap = UserInput.mainMap;
                    MemoryStream ms = new MemoryStream();
                    await Task.Run(() => MainMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png));
                    ms.Position = 0;
                    BitmapImage bi = new BitmapImage();
                    bi.BeginInit();
                    bi.StreamSource = ms;
                    bi.EndInit();
                    image.Source = bi;
                }
                catch (System.Net.WebException e)
                {
                    
                }
            } 


            List<Maneuver> TempManeuvers = new List<Maneuver>();
            List<Maneuver> Temp = new List<Maneuver>();
            foreach (Leg items in UserInput.legs)
            {
                if (items.maneuvers.Count == 0) return TempManeuvers;
                Temp = items.maneuvers;
                foreach (var item in Temp)
                {
                    TempManeuvers.Add(item);
                }
            }
            if (UserInput.legs.Count != 0)
            {
                Distance = UserInput.distance;
                Time = UserInput.formattedTime;
            }
            

            DistancePlace.Text = Convert.ToString(Distance);

            TotalTime.Text = Time;

            if (Loc.Count == 0 || Loc.Count == 1)
                TempManeuvers.Clear();

            NotLoading();
            return TempManeuvers;

            //foreach (var item in TempManeuvers)
            //    UserManeuvers.Add(item);

            //Jyoder 3/12
            //List<Locations> Loc = Locations.getLocations()
            //List<Locations> Loc1 = Locations.getLocations(AddressLine1.Text, City1.Text, State1.Text, ZIP1.Text);
            //List<Locations> Loc2 = Locations.getLocations(AddressLineOne.Text, City2.Text, State2.Text, ZIP2.Text);
            //Directions UserInput = Directions.getDirections(Loc1[0], Loc2[0]);
            //string test = UserInput.legs[0].maneuvers[3].getMap();
            //List<Maneuver> TempManeuvers = UserInput.legs[0].maneuvers;

            //string var = TempManeuvers[0].iconUrl;

            //foreach (var item in TempManeuvers)
            //    UserManeuvers.Add(item);

            //foreach (var items in UserManeuvers)
            //    Narrative.Add(items.narrative);
            //////////////////////////////////
           
        }
 public DirectionsRootObject()
 {
     route = new Directions();
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="locations"></param>
 /// <param name="routeType"></param>
 /// <param name="drivingStyle"></param>
 /// <param name="avoids"></param>
 /// <param name="enhancedNarrative"></param>
 /// <param name="metric"></param>
 /// <param name="fuelEfficiency"></param>
 /// <returns></returns>
 public static Directions getDirections(List<Locations> locations, StaticMap.MapType mapType, RouteType routeType = RouteType.Fastest, DrivingStyle drivingStyle = DrivingStyle.Normal, List<Avoids> avoids = null, bool enhancedNarrative = false, bool metric = false, int fuelEfficiency = 21)
 {
     if (locations.Count == 0)
         return new Directions();
     if (locations.Count == 1)
     {
         //Front end request an empty direction with the map of the location
         Directions d = new Directions();
         d.mainMap = locations[0].map;
         d.mapDetails = locations[0].mapDetails;
         d.mapDetails.mapType = mapType;
         d.legs.Add(new Leg());
         d.legs[0].maneuvers.Add(new Maneuver());
         return d;
     }
     WebClient w = new WebClient();
     string url = ConfigurationManager.AppSettings["MapQuestDirectionsURL"] + ConfigurationManager.AppSettings["MapQuestAPIKey"];
     if (avoids != null)
     {
         foreach (Avoids a in avoids)
             switch (a)
             {
                 case Avoids.CountryBorderCrossing:
                     url += "&avoids=Country border";
                     break;
                 case Avoids.Ferry:
                     url += "&avoids=Ferry";
                     break;
                 case Avoids.LimitedAccess:
                     url += "&avoids=Limited Access";
                     break;
                 case Avoids.SeasonalClosure:
                     url += "&avoids=Approximate Seasonal Closure";
                     break;
                 case Avoids.TollRoad:
                     url += "&avoids=Toll road";
                     break;
                 case Avoids.Unpaved:
                     url += "&avoids=Unpaved";
                     break;
                 default:
                     break;
             }
     }
     url += "&outFormat=json";
     switch (routeType)
     {
         case RouteType.Fastest:
             url += "&routeType=fastest";
             break;
         case RouteType.Shortest:
             url += "&routeType=shortest";
             break;
         case RouteType.Pedestrian:
             url += "&routeType=pedestrian";
             break;
         case RouteType.Bicycle:
             url += "&routeType=bicycle";
             break;
         case RouteType.Multimodal:
             url += "&routeType=multimodal";
             break;
         default:
             url += "&routeType=fastest";
             break;
     }
     url += "&timeType=1";
     url += (enhancedNarrative ? "&enhancedNarrative=true" : "&enhancedNarrative=false");
     url += "&shapeFormat=raw&generalize=0";
     url += ConfigurationManager.AppSettings["MapQuestDirectionsLocale"];
     url += (metric ? "&unit=k" : "&unit=m");
     for (int i = 1; i < locations.Count; ++i)
     {
         url += ("&from=" + locations[i - 1].latLng.lat + ',' + locations[i - 1].latLng.lng
             + "&to=" + locations[i].latLng.lat + ',' + locations[i].latLng.lng);
     }
     url += "&drivingStyle=";
     switch (drivingStyle)
     {
         case DrivingStyle.Aggressive:
             url += "3";
             break;
         case DrivingStyle.Cautious:
             url += "1";
             break;
         case DrivingStyle.Normal:
             url += "2";
             break;
         default:
             url += "2";
             break;
     }
     url += "&highwayEfficiency=" + fuelEfficiency;
     JsonSerializerSettings s = new JsonSerializerSettings();
     s.NullValueHandling = NullValueHandling.Ignore;
     s.ObjectCreationHandling = ObjectCreationHandling.Replace;
     Directions result = JsonConvert.DeserializeObject<DirectionsRootObject>(w.DownloadString(url), s).route;
     result.mapDetails.mapType = mapType;
     return result;
 }