コード例 #1
0
        //(CDLTLL) RefreshMap() with real data loading from services/mock is called only if VisibleRegion or Map-Zoom has changed
        private async void ReloadMapInfo()
        {
            if (IsBusy)
            {
                return;
            }

            IsBusy = true;

            if (_firstLocation == null)
            {
                _firstLocation = await GetLocation();

                if (_firstLocation != null)
                {
                    VisibleRegion = MapSpan.FromCenterAndRadius(new Position(_firstLocation.Latitude, _firstLocation.Longitude), VisibleRegion.Radius);
                    IsBusy        = false;
                    return;
                }
            }

            try
            {
                var distanceRadians =
                    DegreesToRadians(Math.Max(VisibleRegion.LatitudeDegrees, VisibleRegion.LongitudeDegrees) / 2);

                var center            = VisibleRegion.Center;
                var halfheightDegrees = VisibleRegion.LatitudeDegrees / 2;
                var halfwidthDegrees  = VisibleRegion.LongitudeDegrees / 2;

                var topLatitude    = center.Latitude + halfheightDegrees;
                var leftLongitude  = center.Longitude - halfwidthDegrees;
                var bottomLatitude = center.Latitude - halfheightDegrees;
                var rightLongitude = center.Longitude + halfwidthDegrees;

                // Adjust for Internation Date Line (+/- 180 degrees longitude)
                if (leftLongitude < -180)
                {
                    leftLongitude = 180 + (180 + leftLongitude);
                }

                if (rightLongitude > 180)
                {
                    rightLongitude = (rightLongitude - 180) - 180;
                }

                // (CDLTLL) Query and obtain data from a real service or from the mock service depending on the injected implementation
                var vehiclesInCurrentMapArea = await _vehiclesService.GetVehiclesInArea(this.UrlPrefix,
                                                                                        CurrentTenantId,
                                                                                        topLatitude,
                                                                                        leftLongitude,
                                                                                        bottomLatitude,
                                                                                        rightLongitude);

                //(CDLTLL - Check this, not being used)
                Vehicles = new ObservableCollection <Vehicle>(vehiclesInCurrentMapArea);

                var pinsList = new List <ILocationViewModel>();
                foreach (Vehicle vehicle in vehiclesInCurrentMapArea)
                {
                    Vehicle localVehicle = vehicle;
                    pinsList.Add(new LocationViewModel
                    {
                        Key         = vehicle.Id.ToString(),
                        Title       = vehicle.Make + " " + vehicle.Model,
                        Latitude    = vehicle.Latitude,
                        Longitude   = vehicle.Longitude,
                        Description = "LicensePlate: " + vehicle.LicensePlate,
                        Command     = new Command(() => DoneCommand.Execute(localVehicle))
                    });
                }

                //UpdatePins(pins);
                if (Pins != null)
                {
                    Pins.Clear();
                }
                Pins = new ObservableCollection <ILocationViewModel>(pinsList);
            }
            finally
            {
                IsBusy = false;
            }
        }