■ BingRouteDataProvider 클래스에서 GIS 공급자를 사용해 경로 계산기를 구현하는 방법을 보여준다.
▶ RoutingHelper.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 |
using System.Collections.Generic; using System.Drawing; using DevExpress.XtraMap; namespace TestProject { /// <summary> /// 라우팅 헬퍼 /// </summary> public class RoutingHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 맵 푸시핀 리스트 /// </summary> private List<MapPushpin> pushpinList = new List<MapPushpin>(); /// <summary> /// 다각선 /// </summary> private MapPolyline polyline = new MapPolyline() { StrokeWidth = 4, SelectedStrokeWidth = 4, Stroke = Color.FromArgb(0xff, 0xfe, 0x72, 0xff) }; /// <summary> /// 경로점 리스트 /// </summary> private List<RouteWaypoint> waypointList = new List<RouteWaypoint>(); /// <summary> /// 현재 후자 /// </summary> private char currentLatter = 'A'; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 푸시핀 리스트 - PushpinList /// <summary> /// 푸시핀 리스트 /// </summary> public List<MapPushpin> PushpinList { get { return this.pushpinList; } } #endregion #region 경로점 리스트 - WaypointList /// <summary> /// 경로점 리스트 /// </summary> public List<RouteWaypoint> WaypointList { get { return this.waypointList; } } #endregion #region 다각선 - Polyline /// <summary> /// 다각선 /// </summary> public MapPolyline Polyline { get { return this.polyline; } set { this.polyline = value; } } #endregion #region 카운트 - Count /// <summary> /// 카운트 /// </summary> public int Count { get { return this.pushpinList.Count; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 경로 구축하기 - BuildRoute(pointEnumerable) /// <summary> /// 경로 구축하기 /// </summary> /// <param name="pointEnumerable">포인트 열거 가능 목록</param> public void BuildRoute(IEnumerable<GeoPoint> pointEnumerable) { this.polyline.Points.Clear(); foreach(GeoPoint point in pointEnumerable) { this.polyline.Points.Add(point); } } #endregion #region 항목 추가하기 - AddItem(pushpin) /// <summary> /// 항목 추가하기 /// </summary> /// <param name="pushpin">푸시핀</param> public void AddItem(MapPushpin pushpin) { this.pushpinList.Add(pushpin); this.waypointList.Add ( new RouteWaypoint ( ((LocationInformation)pushpin.Information).DisplayName, (GeoPoint)pushpin.Location ) ); pushpin.Text = (this.currentLatter++).ToString(); } #endregion #region 지우기 - Clear() /// <summary> /// 지우기 /// </summary> public void Clear() { this.polyline.Points.Clear(); this.pushpinList.Clear(); this.waypointList.Clear(); this.currentLatter = 'A'; } #endregion } } |
▶ 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 |
using System; using System.Collections.Generic; 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 RoutingHelper routingHelper = new RoutingHelper(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.bingMapDataProvider.BingKey = this.bingKey; this.bingGeocodeDataProvider.BingKey = this.bingKey; this.bingRouteDataProvider.BingKey = this.bingKey; this.bingSearchDataProvider.BingKey = this.bingKey; this.bingRouteDataProvider.RouteOptions.Mode = BingTravelMode.Driving; this.bingRouteDataProvider.RouteOptions.RouteOptimization = BingRouteOptimization.MinimizeTime; this.clearButton.Click += clearButton_Click; this.mapControl.MapItemClick += mapControl_MapItemClick; this.bingGeocodeDataProvider.LocationInformationReceived += bingGeocodeDataProvider_LocationInformationReceived; this.bingRouteDataProvider.RouteCalculated += bingRouteDataProvider_RouteCalculated; this.bingSearchDataProvider.SearchCompleted += bingSearchDataProvider_SearchCompleted; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region Clear 버튼 클릭시 처리하기 - clearButton_Click(sender, e) /// <summary> /// Clear 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void clearButton_Click(object sender, EventArgs e) { this.routingHelper.Clear(); UpdateStorage(); } #endregion #region 맵 컨트롤 맵 항목 클릭시 처리하기 - mapControl_MapItemClick(sender, e) /// <summary> /// 맵 컨트롤 맵 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void mapControl_MapItemClick(object sender, MapItemClickEventArgs e) { MapPushpin pushpin = e.Item as MapPushpin; if(pushpin == null) { return; } if(this.routingHelper.PushpinList.Contains(pushpin)) { return; } this.routingHelper.AddItem(pushpin); CalculateRoute(); e.Handled = true; } #endregion #region 빙 지오코드 데이터 공급자 위치 정보 수신시 처리하기 - bingGeocodeDataProvider_LocationInformationReceived(sender, e) /// <summary> /// 빙 지오코드 데이터 공급자 위치 정보 수신시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void bingGeocodeDataProvider_LocationInformationReceived(object sender, LocationInformationReceivedEventArgs e) { if(e.Cancelled) { return; } GenerateItem(e.Result.Locations); } #endregion #region 빙 경로 데이터 공급자 경로 계산시 처리하기 - bingRouteDataProvider_RouteCalculated(sender, e) /// <summary> /// 빙 경로 데이터 공급자 경로 계산시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void bingRouteDataProvider_RouteCalculated(object sender, BingRouteCalculatedEventArgs e) { if(e.Cancelled) { return; } if(e.CalculationResult.ResultCode == RequestResultCode.Success) { this.routingHelper.BuildRoute(e.CalculationResult.RouteResults[0].RoutePath); UpdateStorage(); } } #endregion #region 빙 검색 데이터 공급자 검색 완료시 처리하기 - bingSearchDataProvider_SearchCompleted(sender, e) /// <summary> /// 빙 검색 데이터 공급자 검색 완료시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void bingSearchDataProvider_SearchCompleted(object sender, BingSearchCompletedEventArgs e) { if(e.Cancelled || (e.RequestResult.ResultCode != RequestResultCode.Success)) { return; } if(e.RequestResult.SearchResults.Count != 0) { GenerateItem(e.RequestResult.SearchResults); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 경로 계산하기 - CalculateRoute() /// <summary> /// 경로 계산하기 /// </summary> private void CalculateRoute() { if(this.routingHelper.Count < 2) { return; } this.bingRouteDataProvider.CalculateRoute(this.routingHelper.WaypointList); } #endregion #region 저장소 업데이트하기 - UpdateStorage() /// <summary> /// 저장소 업데이트하기 /// </summary> private void UpdateStorage() { this.mapItemStorage.Items.Clear(); this.mapItemStorage.Items.Add(this.routingHelper.Polyline); foreach(MapPushpin pushpin in this.routingHelper.PushpinList) { this.mapItemStorage.Items.Add(pushpin); } } #endregion #region 항목 생성하기 - GenerateItem(informationEnumerable) /// <summary> /// 항목 생성하기 /// </summary> /// <param name="informationEnumerable">위치 정보 열거 가능 목록</param> private void GenerateItem(IEnumerable<LocationInformation> informationEnumerable) { UpdateStorage(); foreach(LocationInformation location in informationEnumerable) { MapPushpin pushpin = new MapPushpin() { Location = location.Location, Information = location }; this.mapItemStorage.Items.Add(pushpin); } } #endregion } } |