Example #1
0
        public async Task <bool> ShowFriendsOnMap(ToggleSwitch toggleSwitch, Pushpin userPin)
        {
            const int myEntityId         = 1;
            var       furthestAwayFriend = 1.0;

            var switchName = toggleSwitch.Content.ToString();
            var layerName  = toggleSwitch.LayerName();

            toggleSwitch.Opacity = 0.8;

            var tagLayer = this.FindOrAddLayerByName(layerName);

            var results = await Discovr.Classes.Core.LocationEntity.GetFriends(myEntityId, switchName);

            if (results != null)
            {
                ClearLayer(tagLayer);

                var pointToCompareTo = userPin != null && userPin.GeoCoordinate != null ? userPin.GeoCoordinate : MainMap.Center;
                var newColor         = Extensions.GetRandomColor();
                toggleSwitch.SwitchForeground = newColor;

                foreach (var result in results)
                {
                    var distanceToFriend = Map.AddOrUpdatePinToLayer(pointToCompareTo, new GeoCoordinate(result.Latitude, result.Longitude), result.EntityLabel, result.AvatarUrl, tagLayer, true, null, newColor, new AppSettings().ShowMetric);
                    furthestAwayFriend = distanceToFriend > furthestAwayFriend ? distanceToFriend : furthestAwayFriend;
                }

                MainMap.ZoomLevel = GeoConverter.GetZoomLevelFromMeters(furthestAwayFriend);

                toggleSwitch.Opacity = 1;
            }

            return(true);
        }
Example #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;
            }
        }