Exemple #1
0
        /// <summary>
        /// Agent that runs a scheduled task
        /// </summary>
        /// <param name="task">
        /// The invoked task
        /// </param>
        /// <remarks>
        /// This method is called when a periodic or resource intensive task is invoked
        /// </remarks>
        protected async override void OnInvoke(ScheduledTask task)
        {
            var geolocator = new Geolocator();

            var maximumAge = TimeSpan.FromMinutes(1);
            var timeOut    = TimeSpan.FromSeconds(30);

            try
            {
                var geoposition = await geolocator.GetGeopositionAsync(maximumAge, timeOut);

                if (geoposition != PreviousPosition)
                {
                    var userLocation = GeoConverter.ConvertGeocoordinate(geoposition.Coordinate);
                    PreviousPosition = geoposition;

                    // TODO: Send update to cloud server
                    //var toast = new ShellToast
                    //{
                    //    Title = "Location DiscovR'd",
                    //    Content = string.Format("{0}:{1} {2} accuracy", userLocation.Latitude, userLocation.Longitude, GeoConverter.MetersToReadableString(geoposition.Coordinate.Accuracy, true))
                    //};
                    //toast.Show();
                }
                else
                {
                    //var toast = new ShellToast { Title = "DiscovR", Content = string.Format("You haven't moved!") };
                    //toast.Show();
                }
            }
            catch (Exception ex)
            {
                if ((uint)ex.HResult == 0x80004004)
                {
                    var toast = new ShellToast {
                        Title = "DiscovR", Content = "Location is disabled on this device :("
                    };
                    toast.Show();
                }
            }


            NotifyComplete();
        }
Exemple #2
0
        private async void ShowLocationOnMap()
        {
            if (_currentlySearching)
            {
                return;
            }

            if (AppSettings.LocationConsent == false)
            {
                // The user has opted out of Location.
                LoadingBar.Opacity = 0;
                return;
            }

            _currentlySearching = true;

            var maximumAge = TimeSpan.FromSeconds(10);
            var timeOut    = TimeSpan.FromSeconds(5);

            Geolocator.DesiredAccuracyInMeters = 4000;

            // If we aleady have a pin, let's see if we can be more accurate
            if (Map.UserLayer != null)
            {
                Geolocator.DesiredAccuracy = PositionAccuracy.High;
                timeOut = TimeSpan.FromSeconds(20);
            }

            try
            {
                var geoposition = await Geolocator.GetGeopositionAsync(maximumAge, timeOut);

                var userLocation = GeoConverter.ConvertGeocoordinate(geoposition.Coordinate);

                MainMap.Center = userLocation;
                if (!double.IsNaN(userLocation.Course))
                {
                    MainMap.Heading = userLocation.Course;
                }

                MainMap.Pitch = GeoConverter.GetPitchFromSpeed(userLocation.Speed);

                Pushpin pin = null;

                if (Map.UserLayer != null && Map.UserLayer.Any())
                {
                    var overlay = Map.UserLayer[0];
                    if (overlay != null)
                    {
                        pin = (Pushpin)overlay.Content;
                        overlay.GeoCoordinate = userLocation;
                    }
                }
                else
                {
                    Map.UserLayer = new MapLayer();
                    MainMap.Layers.Add(Map.UserLayer);
                }

                var extraInfo = !double.IsNaN(userLocation.Speed) ? GeoConverter.MetersPerSecondToReadableString(userLocation.Speed, AppSettings.ShowMetric) :
                                string.Format("{0} accuracy", GeoConverter.MetersToReadableString(geoposition.Coordinate.Accuracy, AppSettings.ShowMetric));

                Map.AddOrUpdatePinToLayer(userLocation, userLocation, string.Format("You are here!{0}{1}", Environment.NewLine, extraInfo), UserAvatar, Map.UserLayer, false, pin, (Brush)Resources["PhoneAccentBrush"], AppSettings.ShowMetric);

                if (_friendLocation != null)
                {
                    var friendCoordinate = new GeoCoordinate(_friendLocation.Latitude, _friendLocation.Longitude);
                    var distanceToFriend = Map.AddOrUpdatePinToLayer(userLocation, friendCoordinate, _friendLocation.Label, string.Empty, Map.UserLayer, true, null, null, AppSettings.ShowMetric);

                    // Center the map on the friend and zoom out enough to see both
                    if (_resetZoom)
                    {
                        MainMap.Center    = friendCoordinate;
                        MainMap.ZoomLevel = GeoConverter.GetZoomLevelFromMeters(distanceToFriend);
                        _resetZoom        = false;
                    }
                }
                else if (_resetZoom)
                {
                    MainMap.ZoomLevel = GeoConverter.GetZoomLevelFromMeters(geoposition.Coordinate.Accuracy);
                }

                AppSettings.LastKnownLatitude  = geoposition.Coordinate.Latitude;
                AppSettings.LastKnownLongitude = geoposition.Coordinate.Longitude;

                LoadingBar.Opacity  = 0;
                _currentlySearching = false;
            }
            catch (Exception ex)
            {
                if ((uint)ex.HResult == 0x80004004)
                {
                    // the application does not have the right capability or the location master switch is off
                    MessageBox.Show("Location is disabled on this device. Please enable Location in your phone settings so we can find where you are.", "Location Disabled", MessageBoxButton.OKCancel);
                }

                LoadingBar.Opacity  = 0;
                _currentlySearching = false;
            }
        }