From e9f30fc1f8f43ddbd025807e398eb741596322bf Mon Sep 17 00:00:00 2001 From: "jh5.cho" Date: Wed, 15 Mar 2017 20:42:39 +0900 Subject: Add Implementation of VisibleRegion - Implementation of 'VisibleRegion' property - update 'MoveToRegion()' to support Latitude/Longitude Degree(VisibleRegion) Change-Id: Ia88657d9ddba25a1c766a16905dab8d1fca68100 --- Xamarin.Forms.Maps.Tizen/MapRenderer.cs | 53 +++++++++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/Xamarin.Forms.Maps.Tizen/MapRenderer.cs b/Xamarin.Forms.Maps.Tizen/MapRenderer.cs index cf7ddf7f..752b021f 100755 --- a/Xamarin.Forms.Maps.Tizen/MapRenderer.cs +++ b/Xamarin.Forms.Maps.Tizen/MapRenderer.cs @@ -14,6 +14,7 @@ namespace Xamarin.Forms.Maps.Tizen public class MapRenderer : ViewRenderer { const string MoveMessageName = "MapMoveToRegion"; + const int BaseZoomLevel = 2; bool _disposed; Marker _marker; @@ -40,6 +41,9 @@ namespace Xamarin.Forms.Maps.Tizen if (Control == null) { var mapControl = new MapView(Platform.Tizen.Forms.Context.MainWindow, FormsMaps.MapService); + + mapControl.RenderPost += OnVisibleRegionChanged; + SetNativeControl(mapControl); } @@ -63,6 +67,7 @@ namespace Xamarin.Forms.Maps.Tizen UpdateHasScrollEnabled(); UpdateHasZoomEnabled(); UpdateIsShowingUser(); + UpdateVisibleRegion(); } base.OnElementChanged(e); } @@ -83,6 +88,7 @@ namespace Xamarin.Forms.Maps.Tizen { ((ObservableCollection)Element.Pins).CollectionChanged -= OnCollectionChanged; } + Control.RenderPost -= OnVisibleRegionChanged; Control.Unrealize(); } base.Dispose(disposing); @@ -104,8 +110,51 @@ namespace Xamarin.Forms.Maps.Tizen void OnMoveToRegion(Map map, MapSpan span) { - var newCenter = new Geocoordinates(span.Center.Latitude, span.Center.Longitude); - Control.Center = newCenter; + UpdateVisibleRegion(); + + int latitudeZoomFactor = GetZoomFactor(span.LatitudeDegrees, 90.0); + int longitudeZoomFactor = GetZoomFactor(span.LongitudeDegrees, 180.0); + + Control.Center = new Geocoordinates(span.Center.Latitude, span.Center.Longitude); ; + Control.ZoomLevel = BaseZoomLevel + Math.Min(latitudeZoomFactor, longitudeZoomFactor); + UpdateVisibleRegion(); + } + + int GetZoomFactor(double degree, double targetDegree) + { + int factor = 0; + double tempDegree = degree; + while (true) + { + tempDegree = tempDegree * 2; + if (tempDegree > targetDegree) + break; + factor++; + } + return factor; + } + + void OnVisibleRegionChanged(object sender, EventArgs e) + { + UpdateVisibleRegion(); + } + + void UpdateVisibleRegion() + { + int width = Control.Geometry.Width; + int height = Control.Geometry.Height; + int x = Control.Geometry.X; + int y = Control.Geometry.Y; + + Geocoordinates ul = Control.ScreenToGeolocation(new ElmSharp.Point { X = x, Y = y}); + Geocoordinates ur = Control.ScreenToGeolocation(new ElmSharp.Point { X = x + width, Y = y}); + Geocoordinates ll = Control.ScreenToGeolocation(new ElmSharp.Point { X = x, Y = y + height }); + Geocoordinates lr = Control.ScreenToGeolocation(new ElmSharp.Point { X = x + width, Y = y + height }); + + double dlat = Math.Max(Math.Abs(ul.Latitude - lr.Latitude), Math.Abs(ur.Latitude - ll.Latitude)); + double dlong = Math.Max(Math.Abs(ul.Longitude - lr.Longitude), Math.Abs(ur.Longitude - ll.Longitude)); + + Element.VisibleRegion = new MapSpan(new Position(Control.Center.Latitude, Control.Center.Longitude), dlat, dlong); } -- cgit v1.2.3