public void View2DTestCreateFromXYDifference() { double x, y; var view = View2D.CreateFrom(0, 0, 400, 200, 1, false, false); // test result. Assert.AreEqual(view.LeftTop[0], -200); Assert.AreEqual(view.RightTop[0], 200); Assert.AreEqual(view.RightBottom[1], -100); Assert.AreEqual(view.RightTop[1], 100); Assert.IsTrue(view.Contains(0, 0)); var fromMatrix = view.CreateFromViewPort(2000, 1000); // var topLeft = view.FromViewPort(2000, 1000, 0, 0); fromMatrix.Apply(0, 0, out x, out y); Assert.AreEqual(x, -200); Assert.AreEqual(y, -100); // var bottomRight = view.FromViewPort(2000, 1000, 2000, 1000); fromMatrix.Apply(2000, 1000, out x, out y); Assert.AreEqual(x, 200); Assert.AreEqual(y, 100); var toMatrix = view.CreateToViewPort(2000, 1000); // var viewTopLeft = view.ToViewPort(2000, 1000, view.LeftTop[0], view.RightTop[1]); toMatrix.Apply(view.LeftTop[0], view.RightTop[1], out x, out y); Assert.AreEqual(0, x); Assert.AreEqual(1000, y); // var viewBottomRight = view.ToViewPort(2000, 1000, view.RightBottom[0], view.RightBottom[1]); toMatrix.Apply(view.RightBottom[0], view.RightBottom[1], out x, out y); Assert.AreEqual(2000, x); Assert.AreEqual(0, y); }
/// <summary> /// Raises the scroll event. /// </summary> /// <param name="e1">E1.</param> /// <param name="e2">E2.</param> /// <param name="distanceX">Distance x.</param> /// <param name="distanceY">Distance y.</param> public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // recreate the view. View2D view = View2D.CreateFrom(this.Center[0], this.Center[1], this.Width, this.Height, this.ZoomFactor, true, true); // calculate the new center in pixels. double centerXPixels = this.Width / 2.0f + distanceX; double centerYPixles = this.Height / 2.0f + distanceY; // calculate the new center from the view. double[] center = view.ToViewPort(this.Width, this.Height, (float)centerXPixels, (float)centerYPixles); this.Center[0] = (float)center[0]; this.Center[1] = (float)center[1]; // float factorX = this.View.Width / this.Width; // float factorY = this.View.Height / this.Height; // this.View = View2D.CreateFromCenterAndSize(this.View.Width, this.View.Height, // this.View.CenterX + (-distanceX) * factorX, // this.View.CenterY + (-distanceY) * factorY); this.Invalidate(); return(true); }
/// <summary> /// Creates a view. /// </summary> /// <returns></returns> public View2D CreateView() { try { if (this.Map != null && this.Map.Projection != null && this.MapCenter != null) { // only create a view when a map is set and a valid mapcenter is present. float height = this.SurfaceHeight / _bufferFactor; float width = this.SurfaceWidth / _bufferFactor; // calculate the center/zoom in scene coordinates. var sceneCenter = this.Map.Projection.ToPixel(this.MapCenter.Latitude, this.MapCenter.Longitude); var sceneZoomFactor = (float)this.Map.Projection.ToZoomFactor(this.MapZoom); // create the view for this control. return(View2D.CreateFrom((float)sceneCenter[0], (float)sceneCenter[1], width, height, sceneZoomFactor, _invertX, _invertY, this.MapTilt)); } } catch (Exception ex) { OsmSharp.Logging.Log.TraceEvent("MapViewSurface.CreateView()", TraceEventType.Critical, string.Format("An unhandled exception occured:{0}", ex.ToString())); } return(null); }
/// <summary> /// Creates the view. /// </summary> /// <returns>The view.</returns> public View2D CreateView() { double[] sceneCenter = this.Map.Projection.ToPixel(this.Center.Latitude, this.Center.Longitude); float sceneZoomFactor = (float)this.Map.Projection.ToZoomFactor(this.ZoomLevel); return(View2D.CreateFrom(sceneCenter[0], sceneCenter[1], this.Width, this.Height, sceneZoomFactor, this.Map.Projection.DirectionX, this.Map.Projection.DirectionY)); }
/// <summary> /// Holds the drawing rectangle. /// </summary> //private System.Drawing.RectangleF _rect; /// <summary> /// Creates the view. /// </summary> /// <returns>The view.</returns> public View2D CreateView(System.Drawing.RectangleF rect) { double[] sceneCenter = this.Map.Projection.ToPixel(this.MapCenter.Latitude, this.MapCenter.Longitude); float sceneZoomFactor = (float)this.Map.Projection.ToZoomFactor(this.MapZoom); return(View2D.CreateFrom(sceneCenter [0], sceneCenter [1], rect.Width, rect.Height, sceneZoomFactor, _invertX, _invertY, this.MapTilt)); }
public View2D CreateView(MapRenderingScene scene) { var zoomFactor = Map.Projection.ToZoomFactor(scene.Zoom); var sceneCenter = Map.Projection.ToPixel(scene.Center.Latitude, scene.Center.Longitude); var invertY = (true != !Map.Projection.DirectionY); return(View2D.CreateFrom(sceneCenter[0], sceneCenter[1], SceneSize.Width, SceneSize.Height, zoomFactor, false, invertY, scene.Tilt)); }
/// <summary> /// Creates the view. /// </summary> /// <returns>The view.</returns> public View2D CreateView(System.Drawing.RectangleF rect) { _rect = rect; double[] sceneCenter = this.Map.Projection.ToPixel(this.MapCenter.Latitude, this.MapCenter.Longitude); float sceneZoomFactor = (float)this.Map.Projection.ToZoomFactor(this.MapZoomLevel); return(View2D.CreateFrom(sceneCenter [0], sceneCenter [1], rect.Width, rect.Height, sceneZoomFactor, this.Map.Projection.DirectionX, this.Map.Projection.DirectionY)); }
/// <summary> /// Creates a view. /// </summary> /// <param name="map"></param> /// <param name="zoomFactor"></param> /// <param name="center"></param> /// <returns></returns> public View2D CreateView() { // calculate the center/zoom in scene coordinates. double[] sceneCenter = this.Map.Projection.ToPixel(this.Center.Latitude, this.Center.Longitude); float sceneZoomFactor = (float)this.Map.Projection.ToZoomFactor(this.ZoomLevel); // create the view for this control. return(View2D.CreateFrom((float)sceneCenter[0], (float)sceneCenter[1], this.Width, this.Height, sceneZoomFactor, this.Map.Projection.DirectionX, this.Map.Projection.DirectionY)); }
/// <summary> /// Creates a view. /// </summary> /// <param name="map"></param> /// <param name="zoomFactor"></param> /// <param name="center"></param> /// <returns></returns> public View2D CreateView() { float height = this.LayoutParameters.Height; float width = this.LayoutParameters.Width; // calculate the center/zoom in scene coordinates. double[] sceneCenter = this.Map.Projection.ToPixel(this.MapCenter.Latitude, this.MapCenter.Longitude); float sceneZoomFactor = (float)this.Map.Projection.ToZoomFactor(this.MapZoom); // create the view for this control. return(View2D.CreateFrom((float)sceneCenter[0], (float)sceneCenter[1], this.Width, this.Height, sceneZoomFactor, _invertX, _invertY, this.MapTilt)); }
/// <summary> /// Creates a view. /// </summary> /// <param name="map"></param> /// <param name="zoomFactor"></param> /// <param name="center"></param> /// <returns></returns> public View2D CreateView() { // calculate the center/zoom in scene coordinates. double[] sceneCenter = this.Map.Projection.ToPixel(this.MapCenter.Latitude, this.MapCenter.Longitude); float sceneZoomFactor = (float)this.Map.Projection.ToZoomFactor(this.MapZoomLevel); // return _cacheRenderer.Create (this.Width, this.Height, // this.Map, (float)this.Map.Projection.ToZoomFactor (this.MapZoomLevel), // this.MapCenter, _invertX, _invertY); // create the view for this control. return(View2D.CreateFrom((float)sceneCenter[0], (float)sceneCenter[1], this.Width, this.Height, sceneZoomFactor, _invertX, _invertY)); }
/// <summary> /// Raises the draw event. /// </summary> /// <param name="canvas">Canvas.</param> protected override void OnDraw(global::Android.Graphics.Canvas canvas) { base.OnDraw(canvas); // recreate the view. View2D view = View2D.CreateFrom(this.Center[0], this.Center[1], canvas.Width, canvas.Height, this.ZoomFactor, true, true); // call the canvas renderer. CanvasRenderer2D renderer = new CanvasRenderer2D(); renderer.Render(canvas, this.Scene, view); }
public void View2DTestCreateFromWithDirection() { double delta = 0.000001; // create a new view that is tilted. View2D view = View2D.CreateFrom(0, 0, System.Math.Sqrt(2), System.Math.Sqrt(2), 1, false, false, 45); Assert.AreEqual(-1, view.LeftBottom[0], delta); Assert.AreEqual(0, view.LeftBottom[1], delta); Assert.AreEqual(0, view.RightBottom[0], delta); Assert.AreEqual(-1, view.RightBottom[1], delta); Assert.AreEqual(0, view.LeftTop[0], delta); Assert.AreEqual(1, view.LeftTop[1], delta); Assert.AreEqual(1, view.RightTop[0], delta); Assert.AreEqual(0, view.RightTop[1], delta); Assert.IsTrue(view.Contains(0, 0)); double[] bottomLeft = view.FromViewPort(1000, 1000, 0, 0); Assert.AreEqual(bottomLeft[0], view.LeftBottom[0], delta); Assert.AreEqual(bottomLeft[1], view.LeftBottom[1], delta); double[] topRight = view.FromViewPort(1000, 1000, 1000, 1000); Assert.AreEqual(topRight[0], view.RightTop[0], delta); Assert.AreEqual(topRight[1], view.RightTop[1], delta); double[] topLeft = view.FromViewPort(1000, 1000, 0, 1000); Assert.AreEqual(topLeft[0], view.LeftTop[0], delta); Assert.AreEqual(topLeft[1], view.LeftTop[1], delta); double[] bottomRight = view.FromViewPort(1000, 1000, 1000, 0); Assert.AreEqual(bottomRight[0], view.RightBottom[0], delta); Assert.AreEqual(bottomRight[1], view.RightBottom[1], delta); double[] viewBottomLeft = view.ToViewPort(1000, 1000, view.LeftBottom[0], view.LeftBottom[1]); Assert.AreEqual(view.LeftBottom[0], view.FromViewPort(1000, 1000, viewBottomLeft[0], viewBottomLeft[1])[0], delta); Assert.AreEqual(view.LeftBottom[1], view.FromViewPort(1000, 1000, viewBottomLeft[0], viewBottomLeft[1])[1], delta); double[] viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]); Assert.AreEqual(view.RightBottom[0], view.FromViewPort(1000, 1000, viewBottomRight[0], viewBottomRight[1])[0], delta); Assert.AreEqual(view.RightBottom[1], view.FromViewPort(1000, 1000, viewBottomRight[0], viewBottomRight[1])[1], delta); double[] viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.LeftTop[1]); Assert.AreEqual(view.LeftTop[0], view.FromViewPort(1000, 1000, viewTopLeft[0], viewTopLeft[1])[0], delta); Assert.AreEqual(view.LeftTop[1], view.FromViewPort(1000, 1000, viewTopLeft[0], viewTopLeft[1])[1], delta); double[] viewTopRight = view.ToViewPort(1000, 1000, view.RightTop[0], view.RightTop[1]); Assert.AreEqual(view.RightTop[0], view.FromViewPort(1000, 1000, viewTopRight[0], viewTopRight[1])[0], delta); Assert.AreEqual(view.RightTop[1], view.FromViewPort(1000, 1000, viewTopRight[0], viewTopRight[1])[1], delta); }
/// <summary> /// Creates a view. /// </summary> /// <param name="height"></param> /// <param name="map"></param> /// <param name="zoomFactor"></param> /// <param name="center"></param> /// <param name="width"></param> /// <param name="angle"></param> /// <param name="xInverted">True when the x-axis on the target is inverted (right->left).</param> /// <param name="yInverted">True when the y-axis on the target is inverted (top->bottom).</param> /// <returns></returns> public View2D Create(float width, float height, Map map, float zoomFactor, GeoCoordinate center, bool xInverted, bool yInverted, Degree angle) { // get the projection. IProjection projection = map.Projection; // calculate the center/zoom in scene coordinates. double[] sceneCenter = projection.ToPixel(center.Latitude, center.Longitude); float sceneZoomFactor = zoomFactor; // inversion flags for view: only invert when different. bool invertX = xInverted && (xInverted != !projection.DirectionX); bool invertY = yInverted && (yInverted != !projection.DirectionY); // create the view for this control. return(View2D.CreateFrom((float)sceneCenter[0], (float)sceneCenter[1], width, height, sceneZoomFactor, invertX, invertY, angle)); }
/// <summary> /// /// </summary> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> private View2D CreateCurrentView(float width, float height) { return(View2D.CreateFrom(this.Center[0], this.Center[1], width, height, this.ZoomFactor, true, true)); }
public void View2DTestCreateFromWithDirection() { double delta = 0.000001; double x, y, xFrom, yFrom; // create a new view that is tilted. var view = View2D.CreateFrom(0, 0, System.Math.Sqrt(2), System.Math.Sqrt(2), 1, false, false, 45); var fromMatrix = view.CreateFromViewPort(1000, 1000); var toMatrix = view.CreateToViewPort(1000, 1000); Assert.AreEqual(-1, view.LeftBottom[0], delta); Assert.AreEqual(0, view.LeftBottom[1], delta); Assert.AreEqual(0, view.RightBottom[0], delta); Assert.AreEqual(-1, view.RightBottom[1], delta); Assert.AreEqual(0, view.LeftTop[0], delta); Assert.AreEqual(1, view.LeftTop[1], delta); Assert.AreEqual(1, view.RightTop[0], delta); Assert.AreEqual(0, view.RightTop[1], delta); Assert.IsTrue(view.Contains(0, 0)); // double[] bottomLeft = view.FromViewPort(1000, 1000, 0, 0); fromMatrix.Apply(0, 0, out x, out y); Assert.AreEqual(x, view.LeftBottom[0], delta); Assert.AreEqual(y, view.LeftBottom[1], delta); // double[] topRight = view.FromViewPort(1000, 1000, 1000, 1000); fromMatrix.Apply(1000, 1000, out x, out y); Assert.AreEqual(x, view.RightTop[0], delta); Assert.AreEqual(y, view.RightTop[1], delta); // double[] topLeft = view.FromViewPort(1000, 1000, 0, 1000); fromMatrix.Apply(0, 1000, out x, out y); Assert.AreEqual(x, view.LeftTop[0], delta); Assert.AreEqual(y, view.LeftTop[1], delta); // double[] bottomRight = view.FromViewPort(1000, 1000, 1000, 0); fromMatrix.Apply(1000, 0, out x, out y); Assert.AreEqual(x, view.RightBottom[0], delta); Assert.AreEqual(y, view.RightBottom[1], delta); // double[] viewBottomLeft = view.ToViewPort(1000, 1000, view.LeftBottom[0], view.LeftBottom[1]); toMatrix.Apply(view.LeftBottom[0], view.LeftBottom[1], out x, out y); fromMatrix.Apply(x, y, out xFrom, out yFrom); Assert.AreEqual(view.LeftBottom[0], xFrom, delta); Assert.AreEqual(view.LeftBottom[1], yFrom, delta); // double[] viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]); toMatrix.Apply(view.RightBottom[0], view.RightBottom[1], out x, out y); fromMatrix.Apply(x, y, out xFrom, out yFrom); Assert.AreEqual(view.RightBottom[0], xFrom, delta); Assert.AreEqual(view.RightBottom[1], yFrom, delta); // double[] viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.LeftTop[1]); toMatrix.Apply(view.LeftTop[0], view.LeftTop[1], out x, out y); fromMatrix.Apply(x, y, out xFrom, out yFrom); Assert.AreEqual(view.LeftTop[0], xFrom, delta); Assert.AreEqual(view.LeftTop[1], yFrom, delta); // double[] viewTopRight = view.ToViewPort(1000, 1000, view.RightTop[0], view.RightTop[1]); toMatrix.Apply(view.RightTop[0], view.RightTop[1], out x, out y); fromMatrix.Apply(x, y, out xFrom, out yFrom); Assert.AreEqual(view.RightTop[0], xFrom, delta); Assert.AreEqual(view.RightTop[1], yFrom, delta); }
public void View2DTestCreateFrom() { View2D view = View2D.CreateFrom(0, 0, 200, 200, 1, false, false); // test result. Assert.AreEqual(view.LeftTop[0], -100); Assert.AreEqual(view.RightTop[0], 100); Assert.AreEqual(view.RightBottom[1], -100); Assert.AreEqual(view.RightTop[1], 100); Assert.IsTrue(view.Contains(0, 0)); double[] topLeft = view.FromViewPort(1000, 1000, 0, 0); Assert.AreEqual(topLeft[0], -100); Assert.AreEqual(topLeft[1], -100); double[] bottomRight = view.FromViewPort(1000, 1000, 1000, 1000); Assert.AreEqual(bottomRight[0], 100); Assert.AreEqual(bottomRight[1], 100); double[] viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.RightTop[1]); Assert.AreEqual(0, viewTopLeft[0]); Assert.AreEqual(1000, viewTopLeft[1]); double[] viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]); Assert.AreEqual(1000, viewBottomRight[0]); Assert.AreEqual(0, viewBottomRight[1]); view = View2D.CreateFrom(0, 0, 200, 200, 1, true, false); // test result. Assert.AreEqual(view.LeftTop[0], 100); Assert.AreEqual(view.RightTop[0], -100); Assert.AreEqual(view.RightBottom[1], -100); Assert.AreEqual(view.RightTop[1], 100); Assert.IsTrue(view.Contains(0, 0)); topLeft = view.FromViewPort(1000, 1000, 0, 0); Assert.AreEqual(topLeft[0], 100); Assert.AreEqual(topLeft[1], -100); bottomRight = view.FromViewPort(1000, 1000, 1000, 1000); Assert.AreEqual(bottomRight[0], -100); Assert.AreEqual(bottomRight[1], 100); viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.LeftTop[1]); Assert.AreEqual(0, viewTopLeft[0]); Assert.AreEqual(1000, viewTopLeft[1]); viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]); Assert.AreEqual(1000, viewBottomRight[0]); Assert.AreEqual(0, viewBottomRight[1]); view = View2D.CreateFrom(0, 0, 200, 200, 1, false, true); // test result. Assert.AreEqual(view.LeftTop[0], -100); Assert.AreEqual(view.RightTop[0], 100); Assert.AreEqual(view.RightBottom[1], 100); Assert.AreEqual(view.RightTop[1], -100); Assert.IsTrue(view.Contains(0, 0)); topLeft = view.FromViewPort(1000, 1000, 0, 0); Assert.AreEqual(topLeft[0], -100); Assert.AreEqual(topLeft[1], 100); bottomRight = view.FromViewPort(1000, 1000, 1000, 1000); Assert.AreEqual(bottomRight[0], 100); Assert.AreEqual(bottomRight[1], -100); viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.LeftTop[1]); Assert.AreEqual(0, viewTopLeft[0]); Assert.AreEqual(1000, viewTopLeft[1]); viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]); Assert.AreEqual(1000, viewBottomRight[0]); Assert.AreEqual(0, viewBottomRight[1]); view = View2D.CreateFrom(0, 0, 200, 200, 1, true, true); // test result. Assert.AreEqual(view.LeftTop[0], 100); Assert.AreEqual(view.RightTop[0], -100); Assert.AreEqual(view.RightBottom[1], 100); Assert.AreEqual(view.RightTop[1], -100); Assert.IsTrue(view.Contains(0, 0)); topLeft = view.FromViewPort(1000, 1000, 0, 0); Assert.AreEqual(topLeft[0], 100); Assert.AreEqual(topLeft[1], 100); bottomRight = view.FromViewPort(1000, 1000, 1000, 1000); Assert.AreEqual(bottomRight[0], -100); Assert.AreEqual(bottomRight[1], -100); viewTopLeft = view.ToViewPort(1000, 1000, view.LeftTop[0], view.LeftTop[1]); Assert.AreEqual(0, viewTopLeft[0]); Assert.AreEqual(1000, viewTopLeft[1]); viewBottomRight = view.ToViewPort(1000, 1000, view.RightBottom[0], view.RightBottom[1]); Assert.AreEqual(1000, viewBottomRight[0]); Assert.AreEqual(0, viewBottomRight[1]); view = View2D.CreateFromBounds(100, -100, -100, 100); // test result. Assert.AreEqual(view.LeftTop[0], -100); Assert.AreEqual(view.RightTop[0], 100); Assert.AreEqual(view.RightBottom[1], -100); Assert.AreEqual(view.RightTop[1], 100); Assert.IsTrue(view.Contains(0, 0)); view = View2D.CreateFromBounds(-100, 100, 100, -100); // test result. Assert.AreEqual(view.LeftTop[0], 100); Assert.AreEqual(view.RightTop[0], -100); Assert.AreEqual(view.RightBottom[1], 100); Assert.AreEqual(view.RightTop[1], -100); Assert.IsTrue(view.Contains(0, 0)); }
/// <summary> /// Renders the current complete scene. /// </summary> private void Render() { if (_cacheRenderer.IsRunning) { _cacheRenderer.CancelAndWait(); } // make sure only on thread at the same time is using the renderer. lock (_cacheRenderer) { this.Map.ViewChangedCancel(); // build the layers list. var layers = new List <Layer> (); for (int layerIdx = 0; layerIdx < this.Map.LayerCount; layerIdx++) { // get the layer. layers.Add(this.Map[layerIdx]); } // add the internal layers. layers.Add(_makerLayer); // get old image if available. global::Android.Graphics.Bitmap image = null; if (_offScreenBuffer != null) { image = _offScreenBuffer.Tag as global::Android.Graphics.Bitmap; } if (this.SurfaceHeight == 0) { return; } // resize image if needed. float size = System.Math.Max(this.SurfaceHeight, this.SurfaceWidth); if (image == null || image.Width != (int)(size * _extra) || image.Height != (int)(size * _extra)) { // create a bitmap and render there. image = global::Android.Graphics.Bitmap.CreateBitmap((int)(size * _extra), (int)(size * _extra), global::Android.Graphics.Bitmap.Config.Argb8888); } // create and reset the canvas. global::Android.Graphics.Canvas canvas = new global::Android.Graphics.Canvas(image); canvas.DrawColor(new global::Android.Graphics.Color( SimpleColor.FromKnownColor(KnownColor.White).Value)); // create the view. double[] sceneCenter = this.Map.Projection.ToPixel(this.MapCenter.Latitude, this.MapCenter.Longitude); float mapZoom = this.MapZoom; float sceneZoomFactor = (float)this.Map.Projection.ToZoomFactor(this.MapZoom); // create the view for this control. View2D view = View2D.CreateFrom((float)sceneCenter[0], (float)sceneCenter[1], size * _extra, size * _extra, sceneZoomFactor, _invertX, _invertY, this.MapTilt); long before = DateTime.Now.Ticks; OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", TraceEventType.Information, "Rendering Start"); // notify the map that the view has changed. this.Map.ViewChanged((float)this.Map.Projection.ToZoomFactor(this.MapZoom), this.MapCenter, view); long afterViewChanged = DateTime.Now.Ticks; OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", TraceEventType.Information, "View change took: {0}ms @ zoom level {1}", (new TimeSpan(afterViewChanged - before).TotalMilliseconds), this.MapZoom); // does the rendering. bool complete = _cacheRenderer.Render(canvas, layers, view, (float)this.Map.Projection.ToZoomFactor(this.MapZoom)); long afterRendering = DateTime.Now.Ticks; OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", TraceEventType.Information, "Rendering took: {0}ms @ zoom level {1}", (new TimeSpan(afterRendering - afterViewChanged).TotalMilliseconds), this.MapZoom); if (complete) { // there was no cancellation, the rendering completely finished. // add the result to the scene cache. // add the newly rendered image again. _offScreenBuffer = new ImageTilted2D(view.Rectangle, new byte[0], float.MinValue, float.MaxValue); _offScreenBuffer.Tag = image; var temp = _onScreenBuffer; _onScreenBuffer = _offScreenBuffer; _offScreenBuffer = temp; } // notify the the current surface of the new rendering. this.PostInvalidate(); long after = DateTime.Now.Ticks; if (complete) { // report a successfull render to listener. _listener.NotifyRenderSuccess(view, mapZoom, (int)new TimeSpan(after - before).TotalMilliseconds); } } }
/// <summary> /// Renders the current complete scene. /// </summary> void Render() { if (_cacheRenderer.IsRunning) { _cacheRenderer.CancelAndWait(); } // make sure only on thread at the same time is using the renderer. lock (_cacheRenderer) { double extra = 1.25; // build the layers list. var layers = new List <ILayer> (); for (int layerIdx = 0; layerIdx < this.Map.LayerCount; layerIdx++) { // get the layer. layers.Add(this.Map[layerIdx]); } // add the internal layers. layers.Add(_makerLayer); // create a new cache if size has changed. if (_canvasBitmap == null || _canvasBitmap.Width != (int)(this.Width * extra) || _canvasBitmap.Height != (int)(this.Height * extra)) { // create a bitmap and render there. _canvasBitmap = global::Android.Graphics.Bitmap.CreateBitmap((int)(this.Width * extra), (int)(this.Height * extra), global::Android.Graphics.Bitmap.Config.Argb8888); } else { // clear the cache??? } // create and reset the canvas. global::Android.Graphics.Canvas canvas = new global::Android.Graphics.Canvas(_canvasBitmap); canvas.DrawColor(new global::Android.Graphics.Color( SimpleColor.FromKnownColor(KnownColor.Transparent).Value)); // create the view. double[] sceneCenter = this.Map.Projection.ToPixel(this.MapCenter.Latitude, this.MapCenter.Longitude); float sceneZoomFactor = (float)this.Map.Projection.ToZoomFactor(this.MapZoom); // create the view for this control. View2D view = View2D.CreateFrom((float)sceneCenter[0], (float)sceneCenter[1], this.Width * extra, this.Height * extra, sceneZoomFactor, _invertX, _invertY, this.MapTilt); long before = DateTime.Now.Ticks; OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information, "Rendering Start"); // notify the map that the view has changed. this.Map.ViewChanged((float)this.Map.Projection.ToZoomFactor(this.MapZoom), this.MapCenter, view); long afterViewChanged = DateTime.Now.Ticks; OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information, "View change took: {0}ms @ zoom level {1}", (new TimeSpan(afterViewChanged - before).TotalMilliseconds), this.MapZoom); // add the current canvas to the scene. _scene.AddImage(-1, float.MaxValue, float.MinValue, view.Rectangle, new byte[0], _canvasBitmap); // does the rendering. bool complete = _cacheRenderer.Render(canvas, layers, view); long afterRendering = DateTime.Now.Ticks; OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", System.Diagnostics.TraceEventType.Information, "Rendering took: {0}ms @ zoom level {1}", (new TimeSpan(afterRendering - afterViewChanged).TotalMilliseconds), this.MapZoom); if (complete) { // there was no cancellation, the rendering completely finished. // add the result to the scene cache. lock (_scene) { // add the newly rendered image again. _scene.Clear(); _scene.AddImage(0, float.MinValue, float.MaxValue, view.Rectangle, new byte[0], _canvasBitmap); // switch cache and canvas to prevent re-allocation of bitmaps. global::Android.Graphics.Bitmap newCanvas = _cache; _cache = _canvasBitmap; _canvasBitmap = newCanvas; } } this.PostInvalidate(); long after = DateTime.Now.Ticks; } }
/// <summary> /// Renders the current complete scene. /// </summary> private void Render() { try { if (_renderingSuspended) { // no rendering when rendering is suspended. return; } if (_cacheRenderer.IsRunning) { // cancel previous render. _cacheRenderer.CancelAndWait(); } // make sure only on thread at the same time is using the renderer. lock (_cacheRenderer) { this.Map.ViewChangedCancel(); // build the layers list. var layers = new List <Layer>(); for (int layerIdx = 0; layerIdx < this.Map.LayerCount; layerIdx++) { // get the layer. if (this.Map[layerIdx].IsVisible) { layers.Add(this.Map[layerIdx]); } } // add the internal layers. layers.Add(_makerLayer); if (this.SurfaceHeight == 0) { // the surface has no height yet. Impossible to render like this. return; } // get old image if available. NativeImage image = null; if (_offScreenBuffer != null) { // get the native image from the off-screen buffer. image = _offScreenBuffer.NativeImage as NativeImage; } // resize image if needed. float sizeX = this.SurfaceWidth; float sizeY = this.SurfaceHeight; //if(this.MapAllowTilt) //{ // when rotation is allowed make sure a square is rendered. // sizeX = System.Math.Max(this.SurfaceWidth, this.SurfaceHeight); // sizeY = System.Math.Max(this.SurfaceWidth, this.SurfaceHeight); //} // float size = System.Math.Max(this.SurfaceHeight, this.SurfaceWidth); if (image == null || image.Image.Width != (int)(sizeX * _extra) || image.Image.Height != (int)(sizeY * _extra)) { // create a bitmap and render there. if (image != null) { // make sure to dispose the old image. image.Dispose(); } image = new NativeImage(global::Android.Graphics.Bitmap.CreateBitmap((int)(sizeX * _extra), (int)(sizeY * _extra), global::Android.Graphics.Bitmap.Config.Argb8888)); } // create and reset the canvas. using (var canvas = new global::Android.Graphics.Canvas(image.Image)) { canvas.DrawColor(new global::Android.Graphics.Color( SimpleColor.FromKnownColor(KnownColor.White).Value)); // create the view. double[] sceneCenter = this.Map.Projection.ToPixel(this.MapCenter.Latitude, this.MapCenter.Longitude); float mapZoom = this.MapZoom; float sceneZoomFactor = (float)this.Map.Projection.ToZoomFactor(this.MapZoom); // create the view for this control. float scaledNormalWidth = image.Image.Width / _bufferFactor; float scaledNormalHeight = image.Image.Height / _bufferFactor; var view = View2D.CreateFrom((float)sceneCenter[0], (float)sceneCenter[1], scaledNormalWidth * _extra, scaledNormalHeight * _extra, sceneZoomFactor, _invertX, _invertY, this.MapTilt); long before = DateTime.Now.Ticks; OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", TraceEventType.Information, "Rendering Start"); // notify the map that the view has changed. if (_previouslyChangedView == null || !_previouslyChangedView.Equals(view)) { // report change once! var normalView = View2D.CreateFrom((float)sceneCenter[0], (float)sceneCenter[1], scaledNormalWidth, scaledNormalHeight, sceneZoomFactor, _invertX, _invertY, this.MapTilt); this.Map.ViewChanged((float)this.Map.Projection.ToZoomFactor(this.MapZoom), this.MapCenter, normalView, view); _previouslyChangedView = view; long afterViewChanged = DateTime.Now.Ticks; OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", TraceEventType.Information, "View change took: {0}ms @ zoom level {1}", (new TimeSpan(afterViewChanged - before).TotalMilliseconds), this.MapZoom); } // does the rendering. bool complete = _cacheRenderer.Render(canvas, layers, view, (float)this.Map.Projection.ToZoomFactor(this.MapZoom)); long afterRendering = DateTime.Now.Ticks; OsmSharp.Logging.Log.TraceEvent("OsmSharp.Android.UI.MapView", TraceEventType.Information, "Rendering took: {0}ms @ zoom level {1} and {2}", (new TimeSpan(afterRendering - before).TotalMilliseconds), this.MapZoom, this.MapCenter); if (complete) { // there was no cancellation, the rendering completely finished. // add the result to the scene cache. // add the newly rendered image again. if (_offScreenBuffer == null) { // create the offscreen buffer first. _offScreenBuffer = new ImageTilted2D(view.Rectangle, image, float.MinValue, float.MaxValue); } else { // augment the previous buffer. _offScreenBuffer.Bounds = view.Rectangle; _offScreenBuffer.NativeImage = image; } var temp = _onScreenBuffer; _onScreenBuffer = _offScreenBuffer; _offScreenBuffer = temp; } long after = DateTime.Now.Ticks; if (complete) { // report a successful render to listener. _listener.NotifyRenderSuccess(view, mapZoom, (int)new TimeSpan(after - before).TotalMilliseconds); } } } // notify the the current surface of the new rendering. this.PostInvalidate(); } catch (Exception ex) { // exceptions can be thrown when the mapview is disposed while rendering. // don't worry too much about these, the mapview is garbage anyway. OsmSharp.Logging.Log.TraceEvent("MapViewSurface", TraceEventType.Critical, string.Format("An unhandled exception occured:{0}", ex.ToString())); } }