첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ MainForm.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;

using DevExpress.XtraEditors;
using DevExpress.XtraMap;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : XtraForm
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 빙 맵 키
        /// </summary>
        private string bingKey = "INPUT YOUR BING KEY";

        /// <summary>
        /// 빙맵 데이터 공급자
        /// </summary>
        private BingMapDataProvider bingMapDataProvider;

        /// <summary>
        /// 빙 경로 데이터 공급자
        /// </summary>
        private BingRouteDataProvider bingRouteDataProvider;

        /// <summary>
        /// 경로점 리스트
        /// </summary>
        private List<RouteWaypoint> waypointList = new List<RouteWaypoint>();

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            #region 빙맵 데이터 공급자를 설정한다.

            this.bingMapDataProvider = new BingMapDataProvider();

            this.bingMapDataProvider.BingKey = bingKey;

            this.imageLayer.DataProvider = this.bingMapDataProvider;

            #endregion
            #region 빙 경로 데이터 공급자를 설정한다.

            this.bingRouteDataProvider = new BingRouteDataProvider();

            this.bingRouteDataProvider.BingKey                        = this.bingKey;
            this.bingRouteDataProvider.RouteOptions.Mode              = BingTravelMode.Driving;
            this.bingRouteDataProvider.RouteOptions.RouteOptimization = BingRouteOptimization.MinimizeTime;

            this.informationLayer.DataProvider = this.bingRouteDataProvider;

            #endregion
            #region 경로점 리스트를 설정한다.

            this.waypointList.Add(new RouteWaypoint("NY"       , new GeoPoint(41.145556, -73.995    )));
            this.waypointList.Add(new RouteWaypoint("Oklahoma" , new GeoPoint(36.131389, -95.937222 )));
            this.waypointList.Add(new RouteWaypoint("Las Vegas", new GeoPoint(36.175   , -115.136389)));

            #endregion
            #region 경로점 리스트 박스 컨트롤을 설정한다.

            this.waypointListBoxControl.DisplayMember = "Description";
            this.waypointListBoxControl.DataSource    = this.waypointList;

            #endregion
            #region 이벤트를 설정한다.

            this.bingRouteDataProvider.RouteCalculated      += bingRouteDataProvider_RouteCalculated;
            this.bingRouteDataProvider.LayerItemsGenerating += bingRouteDataProvider_LayerItemsGenerating;
            this.informationLayer.DataRequestCompleted      += informationLayer_DataRequestCompleted;
            this.addButton.Click                            += addButton_Click;
            this.clearButton.Click                          += clearButton_Click;
            this.calculateButton.Click                      += calculateButton_Click;

            #endregion
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 빙 경로 데이터 공급자 경로 계산시 처리하기 - bingRouteDataProvider_RouteCalculated(sender, e)

        /// <summary>
        /// 빙 경로 데이터 공급자 경로 계산시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void bingRouteDataProvider_RouteCalculated(object sender, BingRouteCalculatedEventArgs e)
        {
            RouteCalculationResult result = e.CalculationResult;

            if((result.RouteResults == null) || (result.ResultCode == RequestResultCode.BadRequest))
            {
                this.resultMemoEdit.Text = "이 장소에 대해 빙 경로 서비스가 작업을 할 수 없습니다.";

                return;
            }

            StringBuilder resultStringBuilder = new StringBuilder();

            if(result.IntermediatePoints != null)
            {
                resultStringBuilder.AppendLine("------------------------------");

                for(int i = 0; i < e.CalculationResult.IntermediatePoints.Count; i++)
                {
                    resultStringBuilder.AppendLine
                    (
                        string.Format
                        (
                            "시작점 {0} : {1} ({2})",
                            i + 1,
                            e.CalculationResult.IntermediatePoints[i].Description,
                            e.CalculationResult.IntermediatePoints[i].Location
                        )
                    );
                }
            }

            if((result.RouteResults != null) & (result.ResultCode == RequestResultCode.Success))
            {
                for(int i = 0; i < e.CalculationResult.RouteResults.Count; i++)
                {
                    resultStringBuilder.AppendLine("------------------------------");
                    resultStringBuilder.AppendFormat("경로 {0} :", i + 1);
                    resultStringBuilder.AppendLine();
                    resultStringBuilder.AppendFormat("거리 : {0}", e.CalculationResult.RouteResults[i].Distance);
                    resultStringBuilder.AppendLine();
                    resultStringBuilder.AppendFormat("시간 : {0}", e.CalculationResult.RouteResults[0].Time);

                    if(e.CalculationResult.RouteResults[i].Legs != null)
                    {
                        int j = 1;

                        foreach(BingRouteLeg leg in e.CalculationResult.RouteResults[i].Legs)
                        {
                            resultStringBuilder.AppendFormat("  레그 {0} :", j++);
                            resultStringBuilder.AppendLine();
                            resultStringBuilder.AppendFormat("  거리 : {0}", leg.Distance);
                            resultStringBuilder.AppendLine();
                            resultStringBuilder.AppendFormat("  시간 : {0}", leg.Time);
                            resultStringBuilder.AppendLine();

                            if(leg.Itinerary != null)
                            {
                                foreach(BingItineraryItem itineraryItem in leg.Itinerary)
                                {
                                    resultStringBuilder.AppendFormat("    {0}", itineraryItem.Maneuver);
                                    resultStringBuilder.AppendLine();
                                    resultStringBuilder.AppendFormat("    위치 : {0}", itineraryItem.Location);
                                    resultStringBuilder.AppendLine();
                                }
                            }
                        }
                    }
                }
            }

            this.resultMemoEdit.Text = resultStringBuilder.ToString();
        }

        #endregion
        #region 빙 경로 데이터 공급자 레이어 항목 생성시 처리하기 - bingRouteDataProvider_LayerItemsGenerating(sender, e)

        /// <summary>
        /// 빙 경로 데이터 공급자 레이어 항목 생성시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void bingRouteDataProvider_LayerItemsGenerating(object sender, LayerItemsGeneratingEventArgs e)
        {
            foreach(MapItem item in e.Items)
            {
                MapPolyline polyline = item as MapPolyline;

                if(polyline != null)
                {
                    polyline.Stroke = Color.FromArgb(0xff, 0x00, 0x72, 0xc6);

                    polyline.StrokeWidth = 4;
                }
            }
        }

        #endregion
        #region 정보 레이어 데이터 요청 완료시 처리하기 - informationLayer_DataRequestCompleted(sender, e)

        /// <summary>
        /// 정보 레이어 데이터 요청 완료시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void informationLayer_DataRequestCompleted(object sender, RequestCompletedEventArgs e)
        {
            this.mapControl.ZoomToFitLayerItems(0.4);
        }

        #endregion
        #region 추가 버튼 클릭시 처리하기 - addButton_Click(sender, e)

        /// <summary>
        /// 추가 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void addButton_Click(object sender, EventArgs e)
        {
            string name      = this.nameTextEdit.Text.Trim();
            double latitude  = Convert.ToDouble(this.latitudeTextEdit.Text);
            double longitude = Convert.ToDouble(this.longitudeTextEdit.Text);

            RouteWaypoint point = new RouteWaypoint(name, new GeoPoint(latitude, longitude));

            this.waypointList.Add(point);

            this.waypointListBoxControl.Refresh();
        }

        #endregion
        #region 지우기 버튼 클릭시 처리하기 - clearButton_Click(sender, e)

        /// <summary>
        /// 지우기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void clearButton_Click(object sender, EventArgs e)
        {
            this.waypointList.Clear();

            this.waypointListBoxControl.Refresh();
        }

        #endregion
        #region 경로 계산하기 버튼 클릭시 처리하기 - calculateButton_Click(sender, e)

        /// <summary>
        /// 경로 계산하기 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void calculateButton_Click(object sender, EventArgs e)
        {
            this.bingRouteDataProvider.CalculateRoute(this.waypointList);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요