ThinkGeo.com    |     Documentation    |     Premium Support

Zoom Map to a Set of Points

Hi,


I have an application where we are displaying several points on the map, but would like to have the map only include some specific points. Can you provide a code example on how to implement this?



Steve,


Thanks for you post!


To zoom the map to a set of points you would want to use the Map1.ExtentHelper.GetBoundingBoxofItems() method. You can pass in a collection of InternalFeatures (from a Layer) and the Map will zoom the map to include just those features. 


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using ThinkGeo.MapSuite.Core;

namespace CSSamples.Samples.NavigateTheMap
{
    public partial class ZoomToSetOfFeatures : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Map1.MapBackground.BackgroundBrush = new GeoSolidBrush(GeoColor.FromHtml("#B3C6D4"));
                Map1.CurrentExtent = new RectangleShape(-140, 60, 140, -60);
                Map1.MapUnit = GeographyUnit.DecimalDegree;

                //Declare Layer
                ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(MapPath("~/SampleData/world/cntry02.shp"));
                worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(255, 243, 239, 228), GeoColor.FromArgb(255, 218, 193, 163), 1);
                worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;

                //Declare Layer that contains Internal Features
                InMemoryFeatureLayer highlightLayer = new InMemoryFeatureLayer();
                highlightLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(100, 60, 180, 60), GeoColor.GeographicColors.DeepOcean);
                highlightLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.Country1("CNTRY_NAME");
                highlightLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
                highlightLayer.DrawingMarginPercentage = 50;

                //Add Layers to Map
                Map1.StaticOverlay.Layers.Add("WorldLayer", worldLayer);
                Map1.DynamicOverlay.Layers.Add("HighlightLayer", highlightLayer);

                
                //Clear HighlightLayer
                highlightLayer.InternalFeatures.Clear();

                //Create list of IDs for the features we want to zoomin to
                //This data could reside in your database or the dbf of your
                //shapefile as another column next to your Lat/Long columns
                List<string> featureIds = new List<string>();
                featureIds.Add("15");
                featureIds.Add("24");
                featureIds.Add("60");

                //Query the dbf of your shapefile or database for the features that match the featureIds above
                worldLayer.Open();
                Collection<Feature> features = worldLayer.QueryTools.GetFeaturesByIds(featureIds, new string[] { "CNTRY_NAME" });
                worldLayer.Close();

                foreach (Feature feature in features)
                {
                    //Add each feature that matched the featureIds above to a HighLightLayer
                    highlightLayer.InternalFeatures.Add(feature.Id, feature);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            InMemoryFeatureLayer highlightLayer = (InMemoryFeatureLayer)Map1.DynamicOverlay.Layers["HighlightLayer"];

            if (highlightLayer.InternalFeatures.Count > 0)
            {
                //Zoom the map to the bounding box of the features contained in the HightLightLayer
                highlightLayer.Open();

                Map1.CurrentExtent = highlightLayer.GetBoundingBox();

                //Use the next three lines of code if you want to scale the Current Extent
                //a bit larger
                //RectangleShape rect = highlightLayer.GetBoundingBox();
                //rect.ScaleUp(30);
                //Map1.CurrentExtent = rect;

                highlightLayer.Close();
            }
        }
    }
}
 
Note you can use the also use:

Map1.CurrentExtent = ExtentHelper.GetBoundingBoxOfItems(Layer.InternalFeatures);