Add Implementation of VisibleRegion
authorjh5.cho <jh5.cho@samsung.com>
Wed, 15 Mar 2017 11:42:39 +0000 (20:42 +0900)
committerKangho Hur <kangho.hur@samsung.com>
Mon, 24 Apr 2017 04:36:55 +0000 (13:36 +0900)
    - Implementation of 'VisibleRegion' property
    - update 'MoveToRegion()' to support Latitude/Longitude Degree(VisibleRegion)

Change-Id: Ia88657d9ddba25a1c766a16905dab8d1fca68100

Xamarin.Forms.Maps.Tizen/MapRenderer.cs

index cf7ddf7..752b021 100755 (executable)
@@ -14,6 +14,7 @@ namespace Xamarin.Forms.Maps.Tizen
        public class MapRenderer : ViewRenderer<Map, MapView>
        {
                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<Pin>)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);
                }