Exemple #1
0
        public override void Draw(BoundingBox boundingBox, sbyte zoomLevel, ICanvas canvas, Point topLeftPoint)
        {
            lock (this)
            {
                if (this.latLong == null || this.bitmap == null)
                {
                    return;
                }

                long   mapSize = MercatorProjection.GetMapSize(zoomLevel, this.displayModel.TileSize);
                double pixelX  = MercatorProjection.LongitudeToPixelX(this.latLong.Longitude, mapSize);
                double pixelY  = MercatorProjection.LatitudeToPixelY(this.latLong.Latitude, mapSize);

                int halfBitmapWidth  = this.bitmap.Width / 2;
                int halfBitmapHeight = this.bitmap.Height / 2;

                int left   = (int)(pixelX - topLeftPoint.X - halfBitmapWidth + this.horizontalOffset);
                int top    = (int)(pixelY - topLeftPoint.Y - halfBitmapHeight + this.verticalOffset);
                int right  = left + this.bitmap.Width;
                int bottom = top + this.bitmap.Height;

                Rectangle bitmapRectangle = new Rectangle(left, top, right, bottom);
                Rectangle canvasRectangle = new Rectangle(0, 0, canvas.Width, canvas.Height);
                if (!canvasRectangle.Intersects(bitmapRectangle))
                {
                    return;
                }

                canvas.DrawBitmap(this.bitmap, left, top);
            }
        }
        /// <summary>
        /// Converts geographic coordinates to view x/y coordinates in the map view.
        /// </summary>
        /// <param name="in">
        ///            the geographic coordinates </param>
        /// <returns> x/y view coordinates for the given location </returns>
        public virtual Point ToPixels(LatLong @in)
        {
            if (@in == null || this.mapView.Width <= 0 || this.mapView.Height <= 0)
            {
                return(null);
            }

            MapPosition mapPosition = this.mapView.Model.mapViewPosition.MapPosition;

            // this means somehow the mapview is not yet properly set up, see issue #308.
            if (mapPosition == null)
            {
                return(null);
            }

            // calculate the pixel coordinates of the top left corner
            LatLong latLong = mapPosition.LatLong;
            long    mapSize = MercatorProjection.GetMapSize(mapPosition.ZoomLevel, this.mapView.Model.displayModel.TileSize);
            double  pixelX  = MercatorProjection.LongitudeToPixelX(latLong.Longitude, mapSize);
            double  pixelY  = MercatorProjection.LatitudeToPixelY(latLong.Latitude, mapSize);

            pixelX -= this.mapView.Width >> 1;
            pixelY -= this.mapView.Height >> 1;

            // create a new point and return it
            return(new Point((int)(MercatorProjection.LongitudeToPixelX(@in.Longitude, mapSize) - pixelX), (int)(MercatorProjection.LatitudeToPixelY(@in.Latitude, mapSize) - pixelY)));
        }
Exemple #3
0
        public static Point GetTopLeftPoint(MapPosition mapPosition, Dimension canvasDimension, int tileSize)
        {
            LatLong centerPoint = mapPosition.LatLong;

            int halfCanvasWidth  = canvasDimension.Width / 2;
            int halfCanvasHeight = canvasDimension.Height / 2;

            long   mapSize = MercatorProjection.GetMapSize(mapPosition.ZoomLevel, tileSize);
            double pixelX  = Math.Round(MercatorProjection.LongitudeToPixelX(centerPoint.Longitude, mapSize));
            double pixelY  = Math.Round(MercatorProjection.LatitudeToPixelY(centerPoint.Latitude, mapSize));

            return(new Point((int)pixelX - halfCanvasWidth, (int)pixelY - halfCanvasHeight));
        }
Exemple #4
0
        public override void Draw(BoundingBox boundingBox, sbyte zoomLevel, ICanvas canvas, Point topLeftPoint)
        {
            lock (this)
            {
                if (this.latLongs.Count < 2 || (this.paintStroke == null && this.paintFill == null))
                {
                    return;
                }

                IEnumerator <LatLong> iterator = this.latLongs.GetEnumerator();

                IPath   path    = this.graphicFactory.CreatePath();
                LatLong latLong = iterator.Current;
                long    mapSize = MercatorProjection.GetMapSize(zoomLevel, displayModel.TileSize);
                float   x       = (float)(MercatorProjection.LongitudeToPixelX(latLong.Longitude, mapSize) - topLeftPoint.X);
                float   y       = (float)(MercatorProjection.LatitudeToPixelY(latLong.Latitude, mapSize) - topLeftPoint.Y);
                path.MoveTo(x, y);

                while (iterator.MoveNext())
                {
                    latLong = iterator.Current;
                    x       = (float)(MercatorProjection.LongitudeToPixelX(latLong.Longitude, mapSize) - topLeftPoint.X);
                    y       = (float)(MercatorProjection.LatitudeToPixelY(latLong.Latitude, mapSize) - topLeftPoint.Y);
                    path.LineTo(x, y);
                }

                if (this.paintStroke != null)
                {
                    if (this.keepAligned)
                    {
                        this.paintStroke.SetBitmapShaderShift = topLeftPoint;
                    }
                    canvas.DrawPath(path, this.paintStroke);
                }

                if (this.paintFill != null)
                {
                    if (this.keepAligned)
                    {
                        this.paintFill.SetBitmapShaderShift = topLeftPoint;
                    }

                    canvas.DrawPath(path, this.paintFill);
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// Moves the center position of the map by the given amount of pixels.
        /// </summary>
        /// <param name="moveHorizontal">
        ///            the amount of pixels to move this MapViewPosition horizontally. </param>
        /// <param name="moveVertical">
        ///            the amount of pixels to move this MapViewPosition vertically. </param>
        /// <param name="zoomLevelDiff">
        ///            the difference in desired zoom level. </param>
        /// <param name="animated">
        ///            whether the move should be animated. </param>
        public virtual void MoveCenterAndZoom(double moveHorizontal, double moveVertical, sbyte zoomLevelDiff, bool animated)
        {
            lock (this)
            {
                long   mapSize = MercatorProjection.GetMapSize(this.zoomLevel, this.displayModel.TileSize);
                double pixelX  = MercatorProjection.LongitudeToPixelX(this.longitude, mapSize) - moveHorizontal;
                double pixelY  = MercatorProjection.LatitudeToPixelY(this.latitude, mapSize) - moveVertical;

                pixelX = Math.Min(Math.Max(0, pixelX), mapSize);
                pixelY = Math.Min(Math.Max(0, pixelY), mapSize);

                double newLatitude  = MercatorProjection.PixelYToLatitude(pixelY, mapSize);
                double newLongitude = MercatorProjection.PixelXToLongitude(pixelX, mapSize);
                setCenterInternal(newLatitude, newLongitude);
                setZoomLevelInternal(this.zoomLevel + zoomLevelDiff, animated);
            }
            NotifyObservers();
        }
Exemple #6
0
        public static BoundingBox GetBoundingBox(MapPosition mapPosition, Dimension canvasDimension, int tileSize)
        {
            long   mapSize = MercatorProjection.GetMapSize(mapPosition.ZoomLevel, tileSize);
            double pixelX  = MercatorProjection.LongitudeToPixelX(mapPosition.LatLong.Longitude, mapSize);
            double pixelY  = MercatorProjection.LatitudeToPixelY(mapPosition.LatLong.Latitude, mapSize);

            int halfCanvasWidth  = canvasDimension.Width / 2;
            int halfCanvasHeight = canvasDimension.Height / 2;

            double pixelXMin = Math.Max(0, pixelX - halfCanvasWidth);
            double pixelYMin = Math.Max(0, pixelY - halfCanvasHeight);
            double pixelXMax = Math.Min(mapSize, pixelX + halfCanvasWidth);
            double pixelYMax = Math.Min(mapSize, pixelY + halfCanvasHeight);

            double minLatitude  = MercatorProjection.PixelYToLatitude(pixelYMax, mapSize);
            double minLongitude = MercatorProjection.PixelXToLongitude(pixelXMin, mapSize);
            double maxLatitude  = MercatorProjection.PixelYToLatitude(pixelYMin, mapSize);
            double maxLongitude = MercatorProjection.PixelXToLongitude(pixelXMax, mapSize);

            return(new BoundingBox(minLatitude, minLongitude, maxLatitude, maxLongitude));
        }
Exemple #7
0
        public override void Draw(BoundingBox boundingBox, sbyte zoomLevel, ICanvas canvas, Point topLeftPoint)
        {
            lock (this)
            {
                if (this.latLong == null || (this.paintStroke == null && this.paintFill == null))
                {
                    return;
                }

                double latitude      = this.latLong.Latitude;
                double longitude     = this.latLong.Longitude;
                long   mapSize       = MercatorProjection.GetMapSize(zoomLevel, displayModel.TileSize);
                int    pixelX        = (int)(MercatorProjection.LongitudeToPixelX(longitude, mapSize) - topLeftPoint.X);
                int    pixelY        = (int)(MercatorProjection.LatitudeToPixelY(latitude, mapSize) - topLeftPoint.Y);
                int    radiusInPixel = GetRadiusInPixels(latitude, zoomLevel);

                Rectangle canvasRectangle = new Rectangle(0, 0, canvas.Width, canvas.Height);
                if (!canvasRectangle.IntersectsCircle(pixelX, pixelY, radiusInPixel))
                {
                    return;
                }

                if (this.paintStroke != null)
                {
                    if (this.keepAligned)
                    {
                        this.paintStroke.SetBitmapShaderShift = topLeftPoint;
                    }
                    canvas.DrawCircle(pixelX, pixelY, radiusInPixel, this.paintStroke);
                }
                if (this.paintFill != null)
                {
                    if (this.keepAligned)
                    {
                        this.paintFill.SetBitmapShaderShift = topLeftPoint;
                    }
                    canvas.DrawCircle(pixelX, pixelY, radiusInPixel, this.paintFill);
                }
            }
        }
        /// <summary>
        /// Computes the geographic coordinates of a screen point.
        /// </summary>
        /// <returns> the coordinates of the x/y point </returns>
        public virtual LatLong FromPixels(double x, double y)
        {
            if (this.mapView.Width <= 0 || this.mapView.Height <= 0)
            {
                return(null);
            }

            // this uses the framebuffer position, the mapview position can be out of sync with
            // what the user sees on the screen if an animation is in progress
            MapPosition mapPosition = this.mapView.Model.frameBufferModel.MapPosition;

            // this means somehow the mapview is not yet properly set up, see issue #308.
            if (mapPosition == null)
            {
                return(null);
            }

            // calculate the pixel coordinates of the top left corner
            LatLong latLong = mapPosition.LatLong;
            long    mapSize = MercatorProjection.GetMapSize(mapPosition.ZoomLevel, this.mapView.Model.displayModel.TileSize);
            double  pixelX  = MercatorProjection.LongitudeToPixelX(latLong.Longitude, mapSize);
            double  pixelY  = MercatorProjection.LatitudeToPixelY(latLong.Latitude, mapSize);

            pixelX -= this.mapView.Width >> 1;
            pixelY -= this.mapView.Height >> 1;

            // catch outer map limits
            try
            {
                // convert the pixel coordinates to a LatLong and return it
                return(new LatLong(MercatorProjection.PixelYToLatitude(pixelY + y, mapSize), MercatorProjection.PixelXToLongitude(pixelX + x, mapSize)));
            }
            catch (Exception)
            {
                return(null);
            }
        }
Exemple #9
0
        /// <summary>
        /// Start animating the map towards the given point.
        /// </summary>
        public virtual void AnimateTo(LatLong pos)
        {
            new Task(async() =>
            {
                int totalSteps = 25;           // Define the Step Number
                int signX      = 1;            // Define the Sign for Horizontal Movement
                int signY      = 1;            // Define the Sign for Vertical Movement
                long mapSize   = MercatorProjection.GetMapSize(ZoomLevel, displayModel.TileSize);

                double targetPixelX = MercatorProjection.LongitudeToPixelX(pos.Longitude, mapSize);
                double targetPixelY = MercatorProjection.LatitudeToPixelY(pos.Latitude, mapSize);

                double currentPixelX = MercatorProjection.LongitudeToPixelX(longitude, mapSize);
                double currentPixelY = MercatorProjection.LatitudeToPixelY(latitude, mapSize);

                double stepSizeX = Math.Abs(targetPixelX - currentPixelX) / totalSteps;
                double stepSizeY = Math.Abs(targetPixelY - currentPixelY) / totalSteps;

                /* Check the Signs */
                if (currentPixelX < targetPixelX)
                {
                    signX = -1;
                }

                if (currentPixelY < targetPixelY)
                {
                    signY = -1;
                }

                /* Compute Scroll */
                for (int i = 0; i < totalSteps; i++)
                {
                    MoveCenter(stepSizeX * signX, stepSizeY * signY);
                    await Task.Delay(10);
                }
            }).Start();
        }
Exemple #10
0
        public override void Draw(BoundingBox boundingBox, sbyte zoomLevel, ICanvas canvas, Point topLeftPoint)
        {
            if (spacingConfig.ContainsKey(zoomLevel))
            {
                double spacing = spacingConfig[zoomLevel].Value;

                double minLongitude = spacing * (Math.Floor(boundingBox.MinLongitude / spacing));
                double maxLongitude = spacing * (Math.Ceiling(boundingBox.MaxLongitude / spacing));
                double minLatitude  = spacing * (Math.Floor(boundingBox.MinLatitude / spacing));
                double maxLatitude  = spacing * (Math.Ceiling(boundingBox.MaxLatitude / spacing));

                long mapSize = MercatorProjection.GetMapSize(zoomLevel, this.displayModel.TileSize);

                int bottom = (int)(MercatorProjection.LatitudeToPixelY(minLatitude, mapSize) - topLeftPoint.Y);
                int top    = (int)(MercatorProjection.LatitudeToPixelY(maxLatitude, mapSize) - topLeftPoint.Y);
                int left   = (int)(MercatorProjection.LongitudeToPixelX(minLongitude, mapSize) - topLeftPoint.X);
                int right  = (int)(MercatorProjection.LongitudeToPixelX(maxLongitude, mapSize) - topLeftPoint.X);

                for (double latitude = minLatitude; latitude <= maxLatitude; latitude += spacing)
                {
                    int pixelY = (int)(MercatorProjection.LatitudeToPixelY(latitude, mapSize) - topLeftPoint.Y);
                    canvas.DrawLine(left, pixelY, right, pixelY, this.lineBack);
                }

                for (double longitude = minLongitude; longitude <= maxLongitude; longitude += spacing)
                {
                    int pixelX = (int)(MercatorProjection.LongitudeToPixelX(longitude, mapSize) - topLeftPoint.X);
                    canvas.DrawLine(pixelX, bottom, pixelX, top, this.lineBack);
                }

                for (double latitude = minLatitude; latitude <= maxLatitude; latitude += spacing)
                {
                    int pixelY = (int)(MercatorProjection.LatitudeToPixelY(latitude, mapSize) - topLeftPoint.Y);
                    canvas.DrawLine(left, pixelY, right, pixelY, this.lineFront);
                }

                for (double longitude = minLongitude; longitude <= maxLongitude; longitude += spacing)
                {
                    int pixelX = (int)(MercatorProjection.LongitudeToPixelX(longitude, mapSize) - topLeftPoint.X);
                    canvas.DrawLine(pixelX, bottom, pixelX, top, this.lineFront);
                }

                for (double latitude = minLatitude; latitude <= maxLatitude; latitude += spacing)
                {
                    string text   = ConvertCoordinate(latitude);
                    int    pixelX = (canvas.Width - this.textFront.GetTextWidth(text)) / 2;
                    int    pixelY = (int)(MercatorProjection.LatitudeToPixelY(latitude, mapSize) - topLeftPoint.Y) + this.textFront.GetTextHeight(text) / 2;
                    canvas.DrawText(text, pixelX, pixelY, this.textBack);
                    canvas.DrawText(text, pixelX, pixelY, this.textFront);
                }

                for (double longitude = minLongitude; longitude <= maxLongitude; longitude += spacing)
                {
                    string text   = ConvertCoordinate(longitude);
                    int    pixelX = (int)(MercatorProjection.LongitudeToPixelX(longitude, mapSize) - topLeftPoint.X) - this.textFront.GetTextWidth(text) / 2;
                    int    pixelY = (canvas.Height + this.textFront.GetTextHeight(text)) / 2;
                    canvas.DrawText(text, pixelX, pixelY, this.textBack);
                    canvas.DrawText(text, pixelX, pixelY, this.textFront);
                }
            }
        }