using System; using System.Drawing; using System.Web.Mvc; using ThinkGeo.MapSuite.Core; using ThinkGeo.MapSuite.MvcEdition; namespace DisplayASimpleMap.Controllers { public class DisplayASimpleMapController : Controller { public ActionResult DisplayASimpleMap() { Map map = new Map("Map1", new System.Web.UI.WebControls.Unit(1600, System.Web.UI.WebControls.UnitType.Pixel), new System.Web.UI.WebControls.Unit(1600, System.Web.UI.WebControls.UnitType.Pixel)); map.MapBackground = new BackgroundLayer(new GeoSolidBrush(GeoColor.FromHtml("#E5E3DF"))); map.MapUnit = GeographyUnit.Meter; RectangleShape extent = GetSnappedCurrentExtent(new RectangleShape(-14639183.0384644, 7524757.1810634, -7399085.82811318, 2358849.98232628), map); map.CurrentExtent = extent; GoogleMapsLayer gl = new GoogleMapsLayer(); gl.TileMode = GoogleMapsTileMode.SingleTile; LayerOverlay lo = new LayerOverlay(); lo.TileType = TileType.SingleTile; lo.Layers.Add(gl); lo.IsBaseOverlay = true; map.CustomOverlays.Add(lo); Bitmap bitmap = map.GetBitmap(1600, 1600); return View(map); } private RectangleShape GetSnappedCurrentExtent(RectangleShape value, Map map) { RectangleShape snappedShape = null; PointF currentCenter; PointShape centerPoint = value.GetCenterPoint(); currentCenter = new PointF((float)centerPoint.X, (float)centerPoint.Y); int zoomLevelIndex = GetSnappedZoomLevelIndex(value, map); snappedShape = GetRectangle(new PointShape(currentCenter.X, currentCenter.Y), map.ZoomLevelSet.GetZoomLevels()[zoomLevelIndex].Scale); return snappedShape; } private int GetSnappedZoomLevelIndex(RectangleShape targetExtent, Map map) { double screenWidth = 1600; double screenHeight = 1600; double resolution = Math.Max(targetExtent.Width / screenWidth, targetExtent.Height / screenHeight); double scale = resolution * (39.3701 * 96); PointShape center = targetExtent.GetCenterPoint(); int zoomLevel = -1; foreach (ZoomLevel item in map.ZoomLevelSet.GetZoomLevels()) { if (item.Scale >= scale || Math.Abs(item.Scale - scale) < 0.1) { zoomLevel++; } else { break; } } return zoomLevel == -1 ? 0 : zoomLevel; } private RectangleShape GetRectangle(PointShape center, double scale) { double resolution = scale / (39.3701 * 96); double left = center.X - resolution * 1600 * 0.5; double top = center.Y + resolution * 1600 * 0.5; double right = center.X + resolution * 1600 * 0.5; double bottom = center.Y - resolution * 1600 * 0.5; return new RectangleShape(left, top, right, bottom); } } }