// Creates Route polyline private void CreatePolyline(ISMDirections objDirections) { // create polyline IPolyline objLine = null; objLine = new PolylineClass(); // get points collection IPointCollection objPoints = null; objPoints = objLine as IPointCollection; // Adds Directions points to polyline AddPointsToPolyline(objDirections, ref objPoints); // Project points to Map projection IMxDocument objDoc = m_application.Document as IMxDocument; IMap objMap = objDoc.FocusMap; objLine.Project(objMap.SpatialReference); // create path graphics element IElement objElement = null; objElement = new LineElementClass(); objElement.Geometry = objLine; // Set line color width and style SetLineProperties(objElement); // get Graphic container IGraphicsContainer objCont = objMap as IGraphicsContainer; // Add line to map objCont.AddElement(objElement, 0); }
// Outputs result, zoom to route, fill directions private void ShowResult(ISMDirections objDirections) { // Fill directions m_dlgDirections.Init(objDirections); m_btnShowDirections.Enabled = true; try { // Zoom to extent Envelope objEnv = null; objEnv = new Envelope(); objEnv.PutCoords(objDirections.BoundBox.Left, objDirections.BoundBox.Bottom, objDirections.BoundBox.Right, objDirections.BoundBox.Top); objEnv.Expand(1.1, 1.1, true); // set spatial reference IGeometry objGeo = objEnv as IGeometry; objGeo.SpatialReference = m_objSpatialReference; ZoomToExtent(objEnv); // Create polyline CreatePolyline(objDirections); } catch (Exception ex) { MessageBox.Show(this, "Cannot show result.", "Routing Sample", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } finally { // Refresh map RefreshMap(); } }
// Fills text box public void Init(ISMDirections objDirections) { int nCount = objDirections.Count; string strText = null; // Totals strText = objDirections.TotalsText + System.Environment.NewLine + System.Environment.NewLine; // Add text for each Direction for (int i = 0; i < nCount; i++) { SMDirItem objItem = null; objItem = objDirections.get_Item(i); // Direction text strText = strText + objItem.Text + System.Environment.NewLine; // Drive text (length, time) if (objItem.DriveText.Length > 0) strText = strText + " " + objItem.DriveText + System.Environment.NewLine; strText = strText + System.Environment.NewLine; } // Set control text m_txtDirections.Text = strText; // deselect if was be selected m_txtDirections.Select(0, 0); }
// Find route and initialize Driving Directions page private void FindRoute() { if (m_objRouter == null) return; // Init DD m_btnShowDirections.Enabled = false; m_dlgDirections.Init(); System.Windows.Forms.Cursor Cursor = this.Cursor; try { this.Cursor = Cursors.WaitCursor; // set highways priority (0.0 - 100.0) SMRoadPreferences objPreferences = m_objRouter.Preferences; objPreferences.set_Item(esriSMRoadType.esriSMRoadTypeHighways, (short)m_trackUseRoad.Value); // set route type (Time/Length) if (m_rbtnQuickest.Checked) m_objRouter.NetAttributeName = "Time"; else m_objRouter.NetAttributeName = "Length"; // Set Length Units for Directions output SetDirectionsUnits(); // Geocode user data and adds Start and finish SMStopsCollection objStopsCol = new SMStopsCollectionClass(); ; AddStops(objStopsCol); // Add restrictions m_dlgRestrictions.SetupRouter(m_objRouter); // get driving directions ISMDirections objDirections = null; objDirections = m_objRouter.Solve(objStopsCol, null); // Output result, zoom to route, fill directions ShowResult(objDirections); } catch (Exception ex) { MessageBox.Show(this, "Cannot find route." + System.Environment.NewLine + ex.Message, "Routing Sample", MessageBoxButtons.OK, MessageBoxIcon.Error); // Init DD m_btnShowDirections.Enabled = false; m_dlgDirections.Init(); } finally { this.Cursor = Cursor; } }
// Adds Directions points to point collection private void AddPointsToPolyline(ISMDirections objDirections, ref IPointCollection objPoints) { Point objPoint = new PointClass(); // copy points from DD to line int nItemsCount = objDirections.Count; for (int i = 0; i < nItemsCount; i++) { // get shape from Direction ISMDirItem objItem = null; objItem = objDirections.get_Item(i) as ISMDirItem; ISMPointsCollection objShape = null; objShape = objItem.Shape; // Add point from Direction to received collection int nPointsCount = objShape.Count - 1; for (int j = 0; j <= nPointsCount; j++) { // get point from route SMRouterPoint objRouterPoint = objShape.get_Item(j); // Optimization: Not add point if last added point has similar coords bool bAddPoint = false; if (objPoint.IsEmpty) bAddPoint = true; else if ((objPoint.X != objRouterPoint.X) & (objPoint.Y != objRouterPoint.Y)) bAddPoint = true; if (bAddPoint) { // Add point if need objPoint.X = objRouterPoint.X; objPoint.Y = objRouterPoint.Y; objPoint.SpatialReference = m_objSpatialReference; object missing = System.Reflection.Missing.Value; objPoints.AddPoint(objPoint, ref missing, ref missing); } } } }