Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[UWP]Track user location when IsShowingUser enabled (#556)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Smith authored and rmarinho committed Nov 22, 2016
1 parent 55b8a43 commit 23fd0b6
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions Xamarin.Forms.Maps.UWP/MapRenderer.cs
Expand Up @@ -6,6 +6,7 @@
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;
Expand Down Expand Up @@ -85,6 +86,9 @@ protected override void Dispose(bool disposing)
{
_disposed = true;

_timer?.Stop();
_timer = null;

MessagingCenter.Unsubscribe<Map, MapSpan>(this, "MapMoveToRegion");

if (Element != null)
Expand All @@ -96,6 +100,7 @@ protected override void Dispose(bool disposing)
bool _disposed;
bool _firstZoomLevelChangeFired;
Ellipse _userPositionCircle;
DispatcherTimer _timer;

void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Expand Down Expand Up @@ -155,8 +160,9 @@ void LoadPin(Pin pin)
Control.Children.Add(new PushPin(pin));
}

async Task UpdateIsShowingUser()
async Task UpdateIsShowingUser(bool moveToLocation = true)
{

if (Element.IsShowingUser)
{
var myGeolocator = new Geolocator();
Expand All @@ -165,11 +171,24 @@ async Task UpdateIsShowingUser()
{
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)
Expand Down

0 comments on commit 23fd0b6

Please sign in to comment.