Example #1
0
        /// <summary>
        /// Copies the desired portions of the deserialized model data to the view model collection of locations
        /// </summary>
        /// <param name="model">Deserialized result from API call</param>
        /// <param name="maxResults">Maximum number of results to assign to view model (0 = assign all results)</param>
        /// <returns>Indicator of whether items were left out of the view model due to max size restrictions</returns>
        public static Boolean PopulateViewModel(Response model, ObservableCollection <BingMapsLocationViewModel> viewModel, Int32 maxResults = 0)
        {
            // filter criteria
            String[] countryList = { "United States", "Canada" };

            // set up a staging list for applying any filters/max # of items returned, etc.
            var stagingList = new List <BingMapsLocationViewModel>();

            // clear the view model first
            viewModel.Clear();

            // loop through resource sets (there should only be one)
            foreach (var resourceSet in model.ResourceSets)
            {
                // Note: Changed ResourceSet to hold Location[] vs Resources[] in BingMapsRESTServices.cs

                // loop through resources in resource set
                foreach (var location in resourceSet.Locations.Where((r) => countryList.Contains(r.Address.CountryRegion)))
                {
                    // add location to staging list list
                    stagingList.Add(new BingMapsLocationViewModel()
                    {
                        Address  = GetFormattedAddress(location),
                        City     = GetFormattedCity(location),
                        State    = GetFormattedState(location),
                        Position = new LatLong((location.BoundingBox[0] + location.BoundingBox[2]) / 2, (location.BoundingBox[1] + location.BoundingBox[3]) / 2),
                        Extent   = Math.Max(
                            MapUtilities.HaversineDistance(new LatLong(location.BoundingBox[0], location.BoundingBox[1]), new LatLong(location.BoundingBox[2], location.BoundingBox[1])),
                            MapUtilities.HaversineDistance(new LatLong(location.BoundingBox[0], location.BoundingBox[1]), new LatLong(location.BoundingBox[0], location.BoundingBox[3]))
                            )
                    });
                }
            }

            // only show results that appear unique
            var uniqueStagingList = stagingList.Distinct();

            // apply max count if provided
            var maxResultsExceeded = (maxResults > 0) && (uniqueStagingList.Count() > maxResults);

            foreach (var s in uniqueStagingList.Take(maxResultsExceeded ? maxResults : uniqueStagingList.Count()))
            {
                viewModel.Add(s);
            }

            return(maxResultsExceeded);
        }
Example #2
0
        /// <summary>
        /// Copy the desired portions of the deserialized model data to the view model collection of cameras
        /// </summary>
        /// <param name="model">Deserializeed result from API call</param>
        /// <param name="viewModel">Collection of view model items</param>
        /// <param name="centerLatitude">Latitude of center point of current map view</param>
        /// <param name="centerLongitude">Longitude of center point of current map view</param>
        /// <param name="maxResults">Maximum number of results to assign to view model (0 = assign all results)</param>
        /// <returns>Indicator of whether items were left out of the view model due to max size restrictions</returns>
        public static Boolean PopulateViewModel(cameras model, ObservableCollection <TomTomCameraViewModel> viewModel, LatLong centerPoint, Int32 maxResults = 0)
        {
            Int32 sequence = 0;

            // set up a staging list for applying any filters/max # of items returned, etc.
            var stagingList = new List <TomTomCameraViewModel>();

            // clear the view model first
            viewModel.Clear();

            // pull desired fields from model and insert into view model
            if (model.CameraList != null)
            {
                foreach (var camera in
                         (from c in model.CameraList
                          select new TomTomCameraViewModel()
                {
                    CameraId = c.cameraId,
                    Name = c.cameraName,
                    Orientation = c.orientation.Replace("Traffic closest to camera is t", "T"),
                    RefreshRate = c.refreshRate / 1000d,
                    Position = new LatLong(c.latitude, c.longitude),
                    DistanceFromCenter = MapUtilities.HaversineDistance(centerPoint, new LatLong(c.latitude, c.longitude)),
                    TimeLapse = new ObservableCollection <TimeLapseImage>()
                }))
                {
                    stagingList.Add(camera);
                }
            }

            // apply max count if provided
            var resultsWereTruncated = (maxResults > 0) && (stagingList.Count > maxResults);

            foreach (var s in stagingList
                     .OrderBy((c) => c.DistanceFromCenter)
                     .Take(resultsWereTruncated ? maxResults : stagingList.Count))
            {
                s.Sequence = ++sequence;
                viewModel.Add(s);
            }

            return(resultsWereTruncated);
        }