Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UWP]Track user location when IsShowingUser enabled #556

Merged
merged 1 commit into from Nov 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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