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

Commit

Permalink
revert uwp changes
Browse files Browse the repository at this point in the history
  • Loading branch information
roubachof committed Oct 30, 2019
1 parent 28d7ca3 commit e1d06fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 279 deletions.
287 changes: 9 additions & 278 deletions Xamarin.Forms.Maps.UWP/MapRenderer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
Expand All @@ -25,8 +24,7 @@ protected override async void OnElementChanged(ElementChangedEventArgs<Map> e)
{
var mapModel = e.OldElement;
MessagingCenter.Unsubscribe<Map, MapSpan>(this, "MapMoveToRegion");
((ObservableCollection<Pin>)mapModel.Pins).CollectionChanged -= OnPinCollectionChanged;
((ObservableCollection<MapElement>)mapModel.MapElements).CollectionChanged -= OnMapElementCollectionChanged;
((ObservableCollection<Pin>)mapModel.Pins).CollectionChanged -= OnCollectionChanged;
}

if (e.NewElement != null)
Expand All @@ -39,9 +37,6 @@ protected override async void OnElementChanged(ElementChangedEventArgs<Map> e)
Control.MapServiceToken = FormsMaps.AuthenticationToken;
Control.ZoomLevelChanged += async (s, a) => await UpdateVisibleRegion();
Control.CenterChanged += async (s, a) => await UpdateVisibleRegion();
Control.ActualCameraChanging += async (s, a) => await UpdateCamera(a.Camera.Location.Position);
Control.ActualCameraChanged += async (s, a) => await UpdateCamera(a.Camera.Location.Position);
Control.SizeChanged += async (s, a) => await OnSizeChanged(s, a);
Control.MapTapped += OnMapTapped;
}

Expand All @@ -51,16 +46,12 @@ protected override async void OnElementChanged(ElementChangedEventArgs<Map> e)
UpdateHasScrollEnabled();
UpdateHasZoomEnabled();

((ObservableCollection<Pin>)mapModel.Pins).CollectionChanged += OnPinCollectionChanged;
((ObservableCollection<Pin>)mapModel.Pins).CollectionChanged += OnCollectionChanged;

if (mapModel.Pins.Any())
LoadPins();

((ObservableCollection<MapElement>)mapModel.MapElements).CollectionChanged += OnMapElementCollectionChanged;
if (mapModel.MapElements.Any())
LoadMapElements(mapModel.MapElements);

if (Control == null)
return;
if (Control == null) return;

await Control.Dispatcher.RunIdleAsync(async (i) => await MoveToRegion(mapModel.LastMoveToRegion, MapAnimationKind.None));
await UpdateIsShowingUser();
Expand Down Expand Up @@ -93,20 +84,16 @@ protected override void Dispose(bool disposing)
MessagingCenter.Unsubscribe<Map, MapSpan>(this, "MapMoveToRegion");

if (Element != null)
{
((ObservableCollection<Pin>)Element.Pins).CollectionChanged -= OnPinCollectionChanged;
((ObservableCollection<MapElement>)Element.MapElements).CollectionChanged -= OnMapElementCollectionChanged;
}
((ObservableCollection<Pin>)Element.Pins).CollectionChanged -= OnCollectionChanged;
}

base.Dispose(disposing);
}

bool _disposed;
Ellipse _userPositionCircle;
DispatcherTimer _timer;

void OnPinCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
Expand Down Expand Up @@ -164,175 +151,9 @@ void LoadPin(Pin pin)
Control.Children.Add(new PushPin(pin));
}

void OnMapElementCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
LoadMapElements(e.NewItems.Cast<MapElement>());
break;
case NotifyCollectionChangedAction.Remove:
RemoveMapElements(e.OldItems.Cast<MapElement>());
break;
case NotifyCollectionChangedAction.Replace:
RemoveMapElements(e.OldItems.Cast<MapElement>());
LoadMapElements(e.NewItems.Cast<MapElement>());
break;
case NotifyCollectionChangedAction.Reset:
Control.MapElements.Clear();
LoadMapElements(Element.MapElements);
break;
}
}

void LoadMapElements(IEnumerable<MapElement> mapElements)
{
foreach (var formsMapElement in mapElements)
{
Windows.UI.Xaml.Controls.Maps.MapElement nativeMapElement = null;

switch (formsMapElement)
{
case Polyline polyline:
nativeMapElement = LoadPolyline(polyline);
break;
case Polygon polygon:
nativeMapElement = LoadPolygon(polygon);
break;
}

Control.MapElements.Add(nativeMapElement);

formsMapElement.PropertyChanged += MapElementPropertyChanged;
formsMapElement.MapElementId = nativeMapElement;
}
}

void RemoveMapElements(IEnumerable<MapElement> mapElements)
{
foreach (var mapElement in mapElements)
{
mapElement.PropertyChanged -= MapElementPropertyChanged;
Control.MapElements.Remove((Windows.UI.Xaml.Controls.Maps.MapElement)mapElement.MapElementId);
}
}

void MapElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
switch (sender)
{
case Polyline polyline:
OnPolylinePropertyChanged(polyline, e);
break;
case Polygon polygon:
OnPolygonPropertyChanged(polygon, e);
break;
}
}

protected Geopath PositionsToGeopath(IList<Position> positions)
{
// Geopath constructor throws an exception on an empty list
if (positions.Any())
{
return new Geopath(positions.Select(p => new BasicGeoposition
{
Latitude = p.Latitude,
Longitude = p.Longitude
}));
}
else
{
return new Geopath(new[]
{
new BasicGeoposition(),
});
}
}

#region Polylines

protected virtual MapPolyline LoadPolyline(Polyline polyline)
{
return new MapPolyline()
{
Path = PositionsToGeopath(polyline.Geopath),
StrokeColor = polyline.StrokeColor.IsDefault ? Colors.Black : polyline.StrokeColor.ToWindowsColor(),
StrokeThickness = polyline.StrokeWidth
};
}

void OnPolylinePropertyChanged(Polyline polyline, PropertyChangedEventArgs e)
{
var mapPolyline = (MapPolyline)polyline.MapElementId;

if (mapPolyline == null)
{
return;
}

if (e.PropertyName == Polyline.StrokeColorProperty.PropertyName)
{
mapPolyline.StrokeColor = polyline.StrokeColor.IsDefault ? Colors.Black : polyline.StrokeColor.ToWindowsColor();
}
else if (e.PropertyName == Polyline.StrokeWidthProperty.PropertyName)
{
mapPolyline.StrokeThickness = polyline.StrokeWidth;
}
else if (e.PropertyName == nameof(Polyline.Geopath))
{
mapPolyline.Path = PositionsToGeopath(polyline.Geopath);
}
}

#endregion

#region Polygons

protected virtual MapPolygon LoadPolygon(Polygon polygon)
{
return new MapPolygon()
{
Path = PositionsToGeopath(polygon.Geopath),
StrokeColor = polygon.StrokeColor.IsDefault ? Colors.Black : polygon.StrokeColor.ToWindowsColor(),
StrokeThickness = polygon.StrokeWidth,
FillColor = polygon.FillColor.ToWindowsColor()
};
}

void OnPolygonPropertyChanged(Polygon polygon, PropertyChangedEventArgs e)
{
var mapPolygon = (MapPolygon)polygon.MapElementId;

if (mapPolygon == null)
{
return;
}

if (e.PropertyName == MapElement.StrokeColorProperty.PropertyName)
{
mapPolygon.StrokeColor = polygon.StrokeColor.IsDefault ? Colors.Black : polygon.StrokeColor.ToWindowsColor();
}
else if (e.PropertyName == MapElement.StrokeWidthProperty.PropertyName)
{
mapPolygon.StrokeThickness = polygon.StrokeWidth;
}
else if (e.PropertyName == Polygon.FillColorProperty.PropertyName)
{
mapPolygon.FillColor = polygon.FillColor.ToWindowsColor();
}
else if (e.PropertyName == nameof(Polygon.Geopath))
{
mapPolygon.Path = PositionsToGeopath(polygon.Geopath);
}
}

#endregion

async Task UpdateIsShowingUser(bool moveToLocation = true)
{
if (Control == null || Element == null)
return;
if (Control == null || Element == null) return;

if (Element.IsShowingUser)
{
Expand All @@ -345,8 +166,7 @@ async Task UpdateIsShowingUser(bool moveToLocation = true)
LoadUserPosition(userPosition.Coordinate, moveToLocation);
}

if (Control == null || Element == null)
return;
if (Control == null || Element == null) return;

if (_timer == null)
{
Expand Down Expand Up @@ -409,58 +229,9 @@ async Task UpdateVisibleRegion()
}
}

Task UpdateCamera(BasicGeoposition position, bool raiseCameraChanged = true)
{
if (Control.ActualWidth == 0 || Control.ActualHeight == 0)
{
return Task.CompletedTask;
}

return Task.Run(async () =>
{
try
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
{
double zoomLevel = GetCurrentZoom(Control);
if (!DidCameraPositionAndZoomChange(position, zoomLevel))
{
return;
}
Element.SetCamera(
new Camera(new Position(position.Latitude, position.Longitude), zoomLevel),
raiseCameraChanged);
});
}
catch (Exception exc)
{
System.Diagnostics.Trace.TraceWarning($"UpdateCamera exception: {exc}");
}
});
}

bool DidCameraPositionAndZoomChange(BasicGeoposition position, double zoomLevel)
{
if (Element.Camera != null)
{
var currentPosition = Element.Camera.Position;
var currentZoom = Element.Camera.Zoom;

if (currentPosition.Latitude == position.Latitude && currentPosition.Longitude == position.Longitude
&& currentZoom == zoomLevel)
{
return false;
}
}

return true;
}

void LoadUserPosition(Geocoordinate userCoordinate, bool center)
{
if (Control == null || Element == null)
return;
if (Control == null || Element == null) return;

var userPosition = new BasicGeoposition
{
Expand Down Expand Up @@ -526,49 +297,9 @@ void UpdateHasScrollEnabled()
Control.PanInteractionMode = Element.HasScrollEnabled ? MapPanInteractionMode.Auto : MapPanInteractionMode.Disabled;
}

async Task OnSizeChanged(object sender, SizeChangedEventArgs e)
{
bool isLayoutInit = e.PreviousSize.Width == 0 && e.NewSize.Width > 0;

await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
await UpdateCamera(Control.ActualCamera.Location.Position, raiseCameraChanged: !isLayoutInit));

if (!isLayoutInit)
{
return;
}

// If move to region is called before the layout pass has been made, it won't work as expected.
try
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async () =>
await MoveToRegion(Element.LastMoveToRegion, MapAnimationKind.None));
}
catch (Exception exc)
{
System.Diagnostics.Trace.TraceWarning($"MoveToRegion exception: {exc}");
}
}

void OnMapTapped(MapControl sender, MapInputEventArgs args)
{
Element?.SendMapClicked(new Position(args.Location.Position.Latitude, args.Location.Position.Longitude));
}

static double GetCurrentZoom(MapControl mapView)
{
// The bing maps zoom level is not computed the same way as the google maps one:
// we normalize it to have consistent values
const int tileSize = 256;

mapView.GetLocationFromOffset(new Windows.Foundation.Point(0, 0), out Geopoint nw);
mapView.GetLocationFromOffset(new Windows.Foundation.Point(mapView.ActualWidth, mapView.ActualHeight), out Geopoint se);

double longitudeDelta = Math.Abs(nw.Position.Longitude - se.Position.Longitude);
double width = mapView.ActualWidth;

var zoom = Math.Log(360 * width / (longitudeDelta * tileSize), 2);
return zoom;
}
}
}
2 changes: 1 addition & 1 deletion Xamarin.Forms.Maps.UWP/Xamarin.Forms.Maps.UWP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<AssemblyName>Xamarin.Forms.Maps.UWP</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion>10.0.18362.0</TargetPlatformVersion>
<TargetPlatformVersion>10.0.16299.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.16299.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
Expand Down

0 comments on commit e1d06fa

Please sign in to comment.