■ BingRouteDataProvider 클래스에서 빙 경로(Bing Route) 서비스를 사용해 경로를 계산하는 방법을 보여준다.
▶ MainForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
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 } } |