■ BingGeocodeDataProvider 클래스에서 빙 지오코드(Bing Geocode) 서비스를 사용해 위도/경도에 대한 정보를 구하는 방법을 보여준다.
▶ 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 |
using System; using System.Text; using System.Windows.Forms; 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 BingGeocodeDataProvider bingGeocodeDataProvider; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); Load += Form_Load; this.sendButton.Click += sendButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { this.bindMapDataProvider.BingKey = this.bingKey; this.bingGeocodeDataProvider = new BingGeocodeDataProvider(); this.bingGeocodeDataProvider.BingKey = this.bingKey; this.bingGeocodeDataProvider.MaxVisibleResultCount = 1; this.informationLayer.DataProvider = bingGeocodeDataProvider; this.bingGeocodeDataProvider.LocationInformationReceived += bingGeocodeDataProvider_LocationInformationReceived; this.bingGeocodeDataProvider.LayerItemsGenerating += bingGeocodeDataProvider_LayerItemsGenerating; } #endregion #region 전송 버튼 클릭시 처리하기 - sendButton_Click(sender, e) /// <summary> /// 전송 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void sendButton_Click(object sender, EventArgs e) { GeoPoint geoPoint; if(GetGeoPoint(this.latitudeTextEdit.Text, this.longitudeTextEdit.Text, out geoPoint)) { this.bingGeocodeDataProvider.RequestLocationInformation(geoPoint, 0); } } #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 == true) { return; } if(e.Result.ResultCode != RequestResultCode.Success) { this.resultMemoEdit.Text = "The Bing Geocode service does not work for this location."; return; } StringBuilder stringBuilder = new StringBuilder(); int count = 1; foreach(LocationInformation information in e.Result.Locations) { stringBuilder.AppendFormat("Request Result {0} :", count ); stringBuilder.AppendLine(); stringBuilder.AppendFormat("{0}" , information.EntityType ); stringBuilder.AppendLine(); stringBuilder.AppendFormat("{0}" , information.Address.FormattedAddress); stringBuilder.AppendLine(); stringBuilder.AppendFormat("Coordinates : {0}" , information.Location ); stringBuilder.AppendLine(); stringBuilder.AppendLine ("--------------------------------------------------"); count++; } this.resultMemoEdit.Text = stringBuilder.ToString(); } #endregion #region 빙 지오코드 데이터 공급자 레이어 항목 생성시 처리하기 - bingGeocodeDataProvider_LayerItemsGenerating(sender, e) /// <summary> /// 빙 지오코드 데이터 공급자 레이어 항목 생성시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void bingGeocodeDataProvider_LayerItemsGenerating(object sender, LayerItemsGeneratingEventArgs e) { this.mapControl.ZoomToFit(e.Items, 0.4); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 값 구하기 - GetValue(valueString, minimumValue, maximumValue, valueName, value) /// <summary> /// 값 구하기 /// </summary> /// <param name="valueString">값 문자열</param> /// <param name="minimumValue">최소 값</param> /// <param name="maximumValue">최대 값</param> /// <param name="valueName">값 명</param> /// <param name="value">값</param> /// <returns>처리 결과</returns> private bool GetValue(string valueString, double minimumValue, double maximumValue, string valueName, out double value) { double temporaryValue = string.IsNullOrEmpty(valueString) ? 0 : double.Parse(valueString); if((temporaryValue > maximumValue) || (temporaryValue < minimumValue)) { MessageBox.Show ( string.Format ( "The {0} must be less than or equal to {2} and greater than or equal to {1}. Correct the input value.", valueName, minimumValue, maximumValue ) ); value = 0; return false; } value = temporaryValue; return true; } #endregion #region 지리 위치 구하기 - GetGeoPoint(latitudeString, longitudeString, geoPoint) /// <summary> /// 지리 위치 구하기 /// </summary> /// <param name="latitudeString">위도 문자열</param> /// <param name="longitudeString">경도 문자열</param> /// <param name="geoPoint">지리 위치</param> /// <returns>처리 결과</returns> private bool GetGeoPoint(string latitudeString, string longitudeString, out GeoPoint geoPoint) { double latitude; double longitude; if ( GetValue(latitudeString , -90, 90, "Latitude" , out latitude ) && GetValue(longitudeString, -180, 180, "Longitude", out longitude) ) { geoPoint = new GeoPoint(latitude, longitude); return true; } geoPoint = null; return false; } #endregion } } |