using System; using System.Collections.ObjectModel; using System.Globalization; using System.Web.UI; using ThinkGeo.MapSuite.Core; using ThinkGeo.MapSuite.WebEdition; namespace CSSamples.Samples.Querying_Vector_Layers { public partial class FindFeaturesWithinDistance : 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(-125, 72, 50, -46); Map1.MapUnit = GeographyUnit.DecimalDegree; WorldMapKitWmsWebOverlay worldMapKitOverlay = new WorldMapKitWmsWebOverlay(); Map1.CustomOverlays.Add(worldMapKitOverlay); ShapeFileFeatureLayer worldLayer = new ShapeFileFeatureLayer(MapPath("~/SampleData/world/cntry02.shp")); worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.SimpleColors.Transparent, GeoColor.FromArgb(100, GeoColor.SimpleColors.Green)); worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; LayerOverlay staticOverlay = new LayerOverlay("StaticOverlay"); staticOverlay.IsBaseOverlay = false; staticOverlay.Layers.Add("WorldLayer", worldLayer); Map1.CustomOverlays.Add(staticOverlay); InMemoryFeatureLayer highlightLayer = new InMemoryFeatureLayer(); highlightLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.Capital2; highlightLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.SimpleColors.Transparent, GeoColor.SimpleColors.Green); highlightLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; InMemoryFeatureLayer selectedFeatureLayer = new InMemoryFeatureLayer(); selectedFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.Evergreen1; selectedFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; LayerOverlay dynamicOverlay = new LayerOverlay("DynamicOverlay"); dynamicOverlay.IsBaseOverlay = false; dynamicOverlay.TileType = TileType.SingleTile; dynamicOverlay.Layers.Add("HighlightLayer", highlightLayer); dynamicOverlay.Layers.Add("selectedFeatureLayer", selectedFeatureLayer); Map1.CustomOverlays.Add(dynamicOverlay); } } protected void DistanceDropDownList_SelectedIndexChanged(object sender, EventArgs e) { if (ViewState["CenterPoint"] != null) { FindWithinDistanceFeatures(); } } protected void Map1_Click(object sender, MapClickedEventArgs e) { ViewState["CenterPoint"] = e.Position; FindWithinDistanceFeatures(); } private void FindWithinDistanceFeatures() { PointShape pointShape = (PointShape)ViewState["CenterPoint"]; double distance = Convert.ToDouble(DistanceDropDownList.SelectedItem.ToString(), CultureInfo.InvariantCulture); double xInDegree = DecimalDegreesHelper.GetLongitudeDifferenceFromDistance(distance, DistanceUnit.Kilometer, pointShape.Y); double yInDegree = DecimalDegreesHelper.GetLatitudeDifferenceFromDistance(distance, DistanceUnit.Kilometer); RectangleShape rect = new RectangleShape(pointShape.X - xInDegree, pointShape.Y + yInDegree, pointShape.X + xInDegree, pointShape.Y - yInDegree); LayerOverlay staticOverlay = (LayerOverlay)Map1.CustomOverlays["StaticOverlay"]; LayerOverlay dynamicOverlay = (LayerOverlay)Map1.CustomOverlays["DynamicOverlay"]; FeatureLayer worldLayer = (FeatureLayer)staticOverlay.Layers["WorldLayer"]; InMemoryFeatureLayer highlightLayer = (InMemoryFeatureLayer)dynamicOverlay.Layers["HighlightLayer"]; InMemoryFeatureLayer selectedFeatureLayer = (InMemoryFeatureLayer)dynamicOverlay.Layers["selectedFeatureLayer"]; // Find the countries within special distance. worldLayer.Open(); Collection selectedFeatures = worldLayer.QueryTools.GetFeaturesWithin(rect, ReturningColumnsType.NoColumns); worldLayer.Close(); highlightLayer.InternalFeatures.Clear(); selectedFeatureLayer.InternalFeatures.Clear(); highlightLayer.InternalFeatures.Add("Point", new Feature(pointShape)); highlightLayer.InternalFeatures.Add("Rect", new Feature(rect)); foreach (Feature feature in selectedFeatures) { selectedFeatureLayer.InternalFeatures.Add(feature.Id, feature); } dynamicOverlay.Redraw(); } } }