public class MapRenderer : ViewRenderer<Map, MapView>
{
const string MoveMessageName = "MapMoveToRegion";
+ const int BaseZoomLevel = 2;
bool _disposed;
Marker _marker;
if (Control == null)
{
var mapControl = new MapView(Platform.Tizen.Forms.Context.MainWindow, FormsMaps.MapService);
+
+ mapControl.RenderPost += OnVisibleRegionChanged;
+
SetNativeControl(mapControl);
}
UpdateHasScrollEnabled();
UpdateHasZoomEnabled();
UpdateIsShowingUser();
+ UpdateVisibleRegion();
}
base.OnElementChanged(e);
}
{
((ObservableCollection<Pin>)Element.Pins).CollectionChanged -= OnCollectionChanged;
}
+ Control.RenderPost -= OnVisibleRegionChanged;
Control.Unrealize();
}
base.Dispose(disposing);
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);
}