private void searchTB_Click(object sender, EventArgs e) { winformsMap1.CurrentExtent = new RectangleShape(46.7292, 24.6783, 46.7390, 24.6719); GC.Collect(); LayerOverlay staticOverlay = new LayerOverlay(); staticOverlay.Name = "staticOverlays"; LayerOverlay dynamicOverlay = new LayerOverlay(); dynamicOverlay.Name = "dynamicOverlays"; dynamicOverlay.Layers.Clear(); InMemoryMarkerOverlay markerOverlay = new InMemoryMarkerOverlay(); markerOverlay.IsVisible = false; if (Street1TB.Text != "" && Street2TB.Text != "") { object ob = winformsMap1.Overlays[0]; if (winformsMap1.Overlays.Count > 2) { winformsMap1.Overlays.RemoveAt(winformsMap1.Overlays.Count - 1); } FeatureLayer streetLayer = winformsMap1.FindFeatureLayer("StreetLayer"); //Gets the features according to the street name. streetLayer.Open(); Collection features1 = streetLayer.FeatureSource.GetFeaturesByColumnValue("ST_NAME", Street1TB.Text); Collection features2 = streetLayer.FeatureSource.GetFeaturesByColumnValue("ST_NAME", Street2TB.Text); streetLayer.Close(); intersectionInMemoryFeatureLayer.InternalFeatures.Clear(); //intersectionInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel11.DefaultLineStyle = LineStyles.CreateSimpleLineStyle(GeoColor.StandardColors.LightBlue, 3, true); intersectionInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel15.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; intersectionInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel15.DefaultPointStyle = new PointStyle(new GeoImage(@"D:\Call_Center\MapData\marker_blue.gif")); try { //Adds the street candidates feature and the intersection point feature to the InMemoryFeatureLayer intersectionInMemoryFeatureLayer.Open(); intersectionInMemoryFeatureLayer.EditTools.BeginTransaction(); Collection multiLineShapes1 = new Collection(); Collection multiLineShapes2 = new Collection(); foreach (Feature feature1 in features1) { MultilineShape multiLineShape = feature1.GetShape() as MultilineShape; multiLineShapes1.Add(multiLineShape); intersectionInMemoryFeatureLayer.EditTools.Add(new Feature(multiLineShape)); } foreach (Feature feature2 in features2) { MultilineShape multiLineShape = feature2.GetShape() as MultilineShape; multiLineShapes2.Add(multiLineShape); intersectionInMemoryFeatureLayer.EditTools.Add(new Feature(multiLineShape)); } //Gets the intersection point (or points if this is the case) Feature centerPoint = new Feature(GetIntersectionPoints(multiLineShapes1, multiLineShapes2, 0, DistanceUnit.Meter, GeographyUnit.DecimalDegree)); intersectionInMemoryFeatureLayer.EditTools.Add(centerPoint); intersectionInMemoryFeatureLayer.EditTools.CommitTransaction(); intersectionInMemoryFeatureLayer.Close(); dynamicOverlay.Layers.Add("Intersection", intersectionInMemoryFeatureLayer); winformsMap1.Overlays.Add(dynamicOverlay); intersectionInMemoryFeatureLayer.Open(); winformsMap1.CenterAt(centerPoint); intersectionInMemoryFeatureLayer.Close(); winformsMap1.Refresh(); } catch (Exception exp) { MessageBox.Show(" لا يوجد تقاطع بين " + Street2TB.Text + " و " + Street1TB.Text + " او الشارع المدخل غير موجود ", "نتيجة بحثك", MessageBoxButtons.OK); } } else if (Street1TB.Text != "" && POITB.Text == "") { object ob = winformsMap1.Overlays[0]; if (winformsMap1.Overlays.Count > 2) { winformsMap1.Overlays.RemoveAt(winformsMap1.Overlays.Count - 1); } try { FeatureLayer street1Layer = winformsMap1.FindFeatureLayer("StreetLayer");//Najm_DTMS street1Layer.Open(); SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=Jeddah;Persist Security Info=True;User ID=sa;Password=sa"); SqlCommand comm3 = new SqlCommand("select top 1 col003 from " + CityDropMenu.SelectedValue.ToString() + "_streets where col002 ='" + Street1TB.Text + "'", conn); conn.Open(); SqlDataReader rdr = null; rdr = comm3.ExecuteReader(); if (rdr.HasRows == false) { MessageBox.Show("لم يتم ايجاد شارع " + Street1TB.Text + " او الشارع خطأ", "نتيجة بحثك", MessageBoxButtons.OK); } else { Feature features = new Feature(); while (rdr.Read()) { string rec = Convert.ToString(rdr["col003"]); features = street1Layer.FeatureSource.GetFeatureById(rec, ReturningColumnsType.AllColumns); } intersectionInMemoryFeatureLayer.InternalFeatures.Clear(); intersectionInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel15.DefaultPointStyle = new PointStyle(new GeoImage(@"D:\Call_Center\MapData\marker_blue.gif")); intersectionInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel15.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; intersectionInMemoryFeatureLayer.Open(); intersectionInMemoryFeatureLayer.EditTools.BeginTransaction(); intersectionInMemoryFeatureLayer.EditTools.Add(features); intersectionInMemoryFeatureLayer.EditTools.CommitTransaction(); intersectionInMemoryFeatureLayer.Close(); dynamicOverlay.Layers.Add("Intersection", intersectionInMemoryFeatureLayer); winformsMap1.Overlays.Add(dynamicOverlay); winformsMap1.CenterAt(features); rdr.Close(); street1Layer.Close(); conn.Close(); winformsMap1.ZoomIn(60); winformsMap1.ZoomIn(60); winformsMap1.ZoomIn(60); winformsMap1.ZoomIn(60); winformsMap1.Refresh(); } } catch (Exception exp) { MessageBox.Show(" لم يتم ايجاد شارع " + Street1TB.Text + " او الشارع خطأ", "نتيجة بحثك", MessageBoxButtons.OK); } } else if (Street1TB.Text == "" && Street2TB.Text == "" && POITB.Text != "") { object ob = winformsMap1.Overlays[0]; if (winformsMap1.Overlays.Count > 2) { winformsMap1.Overlays.RemoveAt(winformsMap1.Overlays.Count - 1); } try { FeatureLayer POILayer = winformsMap1.FindFeatureLayer("AllPOILayer"); POILayer.Open(); SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=Najm_DTMS;Persist Security Info=True;User ID=sa;Password=sa"); SqlCommand comm2 = new SqlCommand("Select Top 1 col003 from " + CityDropMenu.SelectedValue.ToString() + "_POI where col001 = '" + POITB.Text + "'", conn); conn.Open(); SqlDataReader rdr = null; rdr = comm2.ExecuteReader(); if (rdr.HasRows == false) { MessageBox.Show(" لم يتم ايجاد المعلم", "نتيجة بحثك", MessageBoxButtons.OK); } else { Feature features = new Feature(); while (rdr.Read()) { string rec = Convert.ToString(rdr["col003"]); features = POILayer.FeatureSource.GetFeatureById(rec, ReturningColumnsType.AllColumns); } intersectionInMemoryFeatureLayer.InternalFeatures.Clear(); intersectionInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel11.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; intersectionInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel11.DefaultPointStyle = new PointStyle(new GeoImage(@"D:\Call_Center\MapData\marker_blue.gif")); intersectionInMemoryFeatureLayer.Open(); intersectionInMemoryFeatureLayer.EditTools.BeginTransaction(); intersectionInMemoryFeatureLayer.EditTools.Add(features); intersectionInMemoryFeatureLayer.EditTools.CommitTransaction(); intersectionInMemoryFeatureLayer.Close(); dynamicOverlay.Layers.Add("Intersection", intersectionInMemoryFeatureLayer); winformsMap1.Overlays.Add(dynamicOverlay); winformsMap1.CenterAt(features); rdr.Close(); POILayer.Close(); conn.Close(); winformsMap1.ZoomIn(60); winformsMap1.ZoomIn(60); winformsMap1.ZoomIn(60); winformsMap1.ZoomIn(60); winformsMap1.Refresh(); } } catch (Exception exp) { MessageBox.Show("لم يتم ايجاد المعلم", "نتيجة بحثك", MessageBoxButtons.OK); } } else if (Street1TB.Text != "" || Street2TB.Text != "" && POITB.Text != "") { object ob = winformsMap1.Overlays[0]; if (winformsMap1.Overlays.Count > 2) { winformsMap1.Overlays.RemoveAt(winformsMap1.Overlays.Count - 1); } try { FeatureLayer POILayer = winformsMap1.FindFeatureLayer("AllPOILayer"); POILayer.Open(); SqlConnection conn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=Najm_DTMS;Persist Security Info=True;User ID=sa;Password=sa"); SqlCommand comm = new SqlCommand("Select Top 1 col003 from " + CityDropMenu.SelectedValue.ToString() + "_POI where col001 = '" + POITB.Text + "' and col002 = '" + Street1TB.Text + "'", conn); conn.Open(); SqlDataReader rdr = null; rdr = comm.ExecuteReader(); if (rdr.HasRows == false) { MessageBox.Show(" لا يوجد تقاطع بين " + POITB.Text + " و " + Street1TB.Text, "نتيجة بحثك", MessageBoxButtons.OK); } else { Feature features = new Feature(); while (rdr.Read()) { string rec = Convert.ToString(rdr["col003"]); features = POILayer.FeatureSource.GetFeatureById(rec, ReturningColumnsType.AllColumns); } intersectionInMemoryFeatureLayer.InternalFeatures.Clear(); intersectionInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel11.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; intersectionInMemoryFeatureLayer.ZoomLevelSet.ZoomLevel11.DefaultPointStyle = new PointStyle(new GeoImage(@"D:\Call_Center\MapData\marker_blue.gif")); intersectionInMemoryFeatureLayer.Open(); intersectionInMemoryFeatureLayer.EditTools.BeginTransaction(); intersectionInMemoryFeatureLayer.EditTools.Add(features); intersectionInMemoryFeatureLayer.EditTools.CommitTransaction(); intersectionInMemoryFeatureLayer.Close(); dynamicOverlay.Layers.Add("Intersection", intersectionInMemoryFeatureLayer); winformsMap1.Overlays.Add(dynamicOverlay); winformsMap1.CenterAt(features); rdr.Close(); POILayer.Close(); conn.Close(); winformsMap1.ZoomIn(60); winformsMap1.ZoomIn(60); winformsMap1.ZoomIn(60); winformsMap1.ZoomIn(60); winformsMap1.Refresh(); } } catch (Exception exp) { MessageBox.Show(" لا يوجد تقاطع بين " + POITB.Text + " و " + Street1TB.Text, "نتيجة بحثك", MessageBoxButtons.OK); } } else { MessageBox.Show("الرجاء ادخال شارع واحد على الاقل", "نتيجة بحثك", MessageBoxButtons.OK); } } private MultipointShape GetIntersectionPoints(Collection multiLineShapes1, Collection multiLineShapes2, double toleranceDistance, DistanceUnit toleranceUnit, GeographyUnit mapUnit) { MultipointShape intersectionMultiPointShape = new MultipointShape(); MultipointShape multiPointShape1 = GetEndPoints(multiLineShapes1); MultipointShape multiPointShape2 = GetEndPoints(multiLineShapes2); foreach (PointShape pointShape1 in multiPointShape1.Points) { foreach (PointShape pointShape2 in multiPointShape2.Points) { if (pointShape1.GetDistanceTo(pointShape2, mapUnit, toleranceUnit) <= toleranceDistance) { intersectionMultiPointShape.Points.Add(pointShape1); } } } return intersectionMultiPointShape; } private MultipointShape GetEndPoints(Collection multilineShapes) { MultipointShape multipointShape = new MultipointShape(); foreach (MultilineShape multilineShape in multilineShapes) { foreach (LineShape lineShape in multilineShape.Lines) { multipointShape.Points.Add(new PointShape(lineShape.Vertices[0])); multipointShape.Points.Add(new PointShape(lineShape.Vertices[lineShape.Vertices.Count - 1])); } } return multipointShape; }