Exemple #1
0
        /// <summary>
        /// Creates a new PushPin, adds it to the layer then returns the distance to the friend.
        /// </summary>
        public static double AddOrUpdatePinToLayer(GeoCoordinate pointToCompareTo, GeoCoordinate friendLocation, string friendLabel, string avatarUrl, MapLayer mapLayer, bool isFriend, Pushpin existingPin, Brush pinColor, bool showMetric)
        {
            var newPin = existingPin ?? new Pushpin();

            newPin.Name = isFriend ? "Pin_" + friendLabel : UserPinName;
            if (pinColor != null)
            {
                newPin.Background = pinColor;
            }

            // This will hold our text and image
            var pinPanel = new StackPanel();

            newPin.GeoCoordinate = friendLocation;
            var distanceToFriend = newPin.GeoCoordinate.GetDistanceTo(pointToCompareTo);

            // Define the image to use as the pushpin icon.
            var pinImage = new Image();

            pinImage.Source = !String.IsNullOrEmpty(avatarUrl) ? new BitmapImage(new Uri(avatarUrl, UriKind.Absolute)) : new BitmapImage(new Uri("https://lh3.googleusercontent.com/-2cAEJfwgJU0/AAAAAAAAAAI/AAAAAAAAAAA/2qq4qKqgmlA/s80-c-k-no/photo.jpg", UriKind.Absolute));
            pinImage.HorizontalAlignment = HorizontalAlignment.Left;
            pinImage.VerticalAlignment   = VerticalAlignment.Top;
            pinImage.Height = 56;
            pinImage.Width  = 56;

            pinPanel.Children.Add(pinImage);

            var textContent = new TextBlock();

            textContent.HorizontalAlignment = HorizontalAlignment.Left;
            textContent.VerticalAlignment   = VerticalAlignment.Top;
            textContent.Margin = new Thickness(65, -56, 5, 0);

            if (isFriend)
            {
                textContent.Text = String.Format("{0} is here!", friendLabel) + Environment.NewLine + GeoConverter.MetersToReadableString(distanceToFriend, showMetric) + " from you";
            }
            else
            {
                textContent.Text = friendLabel;
            }

            pinPanel.Children.Add(textContent);

            newPin.Content = pinPanel;

            if (existingPin == null)
            {
                var pinOverlay = new MapOverlay();
                pinOverlay.Content        = newPin;
                pinOverlay.GeoCoordinate  = friendLocation;
                pinOverlay.PositionOrigin = new Point(0, 1);

                mapLayer.Add(pinOverlay);
            }

            return(distanceToFriend);
        }
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;
            }
        }