summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Maps.UWP
diff options
context:
space:
mode:
authorEthan Dennis <erdennis13@gmail.com>2017-03-20 15:40:04 (GMT)
committerJason Smith <jason.smith@xamarin.com>2017-03-20 15:40:04 (GMT)
commit5ca7259959022253c6a71c5290854929291caec5 (patch)
treeaae699f20b2f4b20e9418114504f825513002c60 /Xamarin.Forms.Maps.UWP
parentf98e1525c15ca77ec9840c2b5c8020b1b320b141 (diff)
downloadxamarin-forms-5ca7259959022253c6a71c5290854929291caec5.zip
xamarin-forms-5ca7259959022253c6a71c5290854929291caec5.tar.gz
xamarin-forms-5ca7259959022253c6a71c5290854929291caec5.tar.bz2
UWP MapRenderer NRE and Location fixes (#811)
* Issues: 1. If UWP MapRenderer is navigated away from, before the user location is found, an uncaught NRE will be thrown. 2. UWP MapRenderer updates are not enforced to be on UI thread 3. Order of operations in MapRenderer would cause user location to never be shown. Fixes: 1. Added null checks in UpdateUserIsShowing and LoadUserPosition methods. 2. Called map position updates on UI thread. 3. Changed order of operations in OnElementChanged to allow for user geocoordinates to be shown. * Fixed code formatting issues * Fix formatting * Deterministic null checks * Updated null checks
Diffstat (limited to 'Xamarin.Forms.Maps.UWP')
-rw-r--r--Xamarin.Forms.Maps.UWP/MapRenderer.cs28
1 files changed, 19 insertions, 9 deletions
diff --git a/Xamarin.Forms.Maps.UWP/MapRenderer.cs b/Xamarin.Forms.Maps.UWP/MapRenderer.cs
index 7b3bcd2..abecbe3 100644
--- a/Xamarin.Forms.Maps.UWP/MapRenderer.cs
+++ b/Xamarin.Forms.Maps.UWP/MapRenderer.cs
@@ -47,8 +47,8 @@ namespace Xamarin.Forms.Maps.WinRT
{
SetNativeControl(new MapControl());
Control.MapServiceToken = FormsMaps.AuthenticationToken;
- Control.ZoomLevelChanged += (s, a) => UpdateVisibleRegion();
- Control.CenterChanged += (s, a) => UpdateVisibleRegion();
+ Control.ZoomLevelChanged += async (s, a) => await UpdateVisibleRegion();
+ Control.CenterChanged += async (s, a) => await UpdateVisibleRegion();
}
MessagingCenter.Subscribe<Map, MapSpan>(this, "MapMoveToRegion", async (s, a) => await MoveToRegion(a), mapModel);
@@ -62,9 +62,11 @@ namespace Xamarin.Forms.Maps.WinRT
if (mapModel.Pins.Any())
LoadPins();
- await UpdateIsShowingUser();
+ if (Control == null) return;
+
await Control.Dispatcher.RunIdleAsync(async (i) => await MoveToRegion(mapModel.LastMoveToRegion, MapAnimationKind.None));
- }
+ await UpdateIsShowingUser();
+ }
}
protected override async void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
@@ -162,7 +164,8 @@ namespace Xamarin.Forms.Maps.WinRT
async Task UpdateIsShowingUser(bool moveToLocation = true)
{
-
+ if (Control == null || Element == null) return;
+
if (Element.IsShowingUser)
{
var myGeolocator = new Geolocator();
@@ -174,6 +177,8 @@ namespace Xamarin.Forms.Maps.WinRT
LoadUserPosition(userPosition.Coordinate, moveToLocation);
}
+ if (Control == null || Element == null) return;
+
if (_timer == null)
{
_timer = new DispatcherTimer();
@@ -186,7 +191,7 @@ namespace Xamarin.Forms.Maps.WinRT
}
else if (_userPositionCircle != null && Control.Children.Contains(_userPositionCircle))
{
- _timer?.Stop();
+ _timer.Stop();
Control.Children.Remove(_userPositionCircle);
}
}
@@ -207,7 +212,7 @@ namespace Xamarin.Forms.Maps.WinRT
await Control.TrySetViewBoundsAsync(boundingBox, null, animation);
}
- void UpdateVisibleRegion()
+ async Task UpdateVisibleRegion()
{
if (Control == null || Element == null)
return;
@@ -224,7 +229,10 @@ namespace Xamarin.Forms.Maps.WinRT
var center = new Position(boundingBox.Center.Latitude, boundingBox.Center.Longitude);
var latitudeDelta = Math.Abs(nw.Position.Latitude - se.Position.Latitude);
var longitudeDelta = Math.Abs(nw.Position.Longitude - se.Position.Longitude);
- Element.VisibleRegion = new MapSpan(center, latitudeDelta, longitudeDelta);
+ await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
+ {
+ Element.VisibleRegion = new MapSpan(center, latitudeDelta, longitudeDelta);
+ });
}
}
catch (Exception)
@@ -235,6 +243,8 @@ namespace Xamarin.Forms.Maps.WinRT
void LoadUserPosition(Geocoordinate userCoordinate, bool center)
{
+ if (Control == null || Element == null) return;
+
var userPosition = new BasicGeoposition
{
Latitude = userCoordinate.Point.Position.Latitude,
@@ -309,4 +319,4 @@ namespace Xamarin.Forms.Maps.WinRT
}
#endif
}
-} \ No newline at end of file
+}