/// <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(); }
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)); }
/// <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); } }