From 23fd0b63950864c30e01465b0e1adfc2fd35c700 Mon Sep 17 00:00:00 2001 From: Jason Smith Date: Tue, 22 Nov 2016 05:02:12 -0800 Subject: [PATCH] [UWP]Track user location when IsShowingUser enabled (#556) --- Xamarin.Forms.Maps.UWP/MapRenderer.cs | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/Xamarin.Forms.Maps.UWP/MapRenderer.cs b/Xamarin.Forms.Maps.UWP/MapRenderer.cs index f8a4c8741..842864b9d 100644 --- a/Xamarin.Forms.Maps.UWP/MapRenderer.cs +++ b/Xamarin.Forms.Maps.UWP/MapRenderer.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Threading.Tasks; using Windows.Devices.Geolocation; using Windows.UI; +using Windows.UI.Xaml; using Windows.UI.Xaml.Controls.Maps; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Shapes; @@ -85,6 +86,9 @@ namespace Xamarin.Forms.Maps.WinRT { _disposed = true; + _timer?.Stop(); + _timer = null; + MessagingCenter.Unsubscribe(this, "MapMoveToRegion"); if (Element != null) @@ -96,6 +100,7 @@ namespace Xamarin.Forms.Maps.WinRT bool _disposed; bool _firstZoomLevelChangeFired; Ellipse _userPositionCircle; + DispatcherTimer _timer; void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { @@ -155,8 +160,9 @@ namespace Xamarin.Forms.Maps.WinRT Control.Children.Add(new PushPin(pin)); } - async Task UpdateIsShowingUser() + async Task UpdateIsShowingUser(bool moveToLocation = true) { + if (Element.IsShowingUser) { var myGeolocator = new Geolocator(); @@ -165,11 +171,24 @@ namespace Xamarin.Forms.Maps.WinRT { var userPosition = await myGeolocator.GetGeopositionAsync(); if (userPosition?.Coordinate != null) - LoadUserPosition(userPosition.Coordinate, true); + LoadUserPosition(userPosition.Coordinate, moveToLocation); } + + if (_timer == null) + { + _timer = new DispatcherTimer(); + _timer.Tick += async (s, o) => await UpdateIsShowingUser(moveToLocation: false); + _timer.Interval = TimeSpan.FromSeconds(15); + } + + if (!_timer.IsEnabled) + _timer.Start(); } else if (_userPositionCircle != null && Control.Children.Contains(_userPositionCircle)) + { + _timer?.Stop(); Control.Children.Remove(_userPositionCircle); + } } async Task MoveToRegion(MapSpan span, MapAnimationKind animation = MapAnimationKind.Bow)