翻譯|使用教程|編輯:龔雪|2022-09-20 09:59:43.987|閱讀 216 次
概述:本文將為大家介紹使用DevExpress WinForms控件開發(fā)地圖應(yīng)用時如何實(shí)現(xiàn)搜索功能,歡迎下載相關(guān)產(chǎn)品體驗(yàn)!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
DevExpress WinForm擁有180+組件和UI庫,能為Windows Forms平臺創(chuàng)建具有影響力的業(yè)務(wù)解決方案。DevExpress WinForms能完美構(gòu)建流暢、美觀且易于使用的應(yīng)用程序,無論是Office風(fēng)格的界面,還是分析處理大批量的業(yè)務(wù)數(shù)據(jù),它都能輕松勝任!
DevExpress Map控件支持Microsoft的Bing搜索和OpenStreetMap搜索服務(wù),允許您在應(yīng)用程序中嵌入搜索功能。 啟用此功能后,您可以在Search Panel(或使用自定義UI)中輸入搜索條件,實(shí)現(xiàn)請求,并在地圖和搜索面板中查看結(jié)果。
和 類代表提供搜索功能的 Bing Search 和 OpenStreetMap Search 數(shù)據(jù)提供者,在本文中解釋了如何在地圖控件中使用搜索數(shù)據(jù)提供程序。
重要提示:由于Bing于 2017 年 7 月 30 日取消 SOAP 服務(wù),地圖控件的Bing搜索提供程序在 16.1 版和早期版本中無法正常工作。
執(zhí)行以下操作以在Map控件中啟用搜索:
下面的代碼片段顯示了如何執(zhí)行此操作。
C#
private void Form1_Load(object sender, System.EventArgs e) { // ... InformationLayer infoLayer = new InformationLayer(); map.Layers.Add(infoLayer); BingSearchDataProvider searchProvider = new BingSearchDataProvider(); infoLayer.DataProvider = searchProvider; searchProvider.BingKey = yourBingKey; }
VB.NET
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load ' .... Dim infoLayer As New InformationLayer() map.Layers.Add(infoLayer) Dim searchProvider As New BingSearchDataProvider() infoLayer.DataProvider = searchProvider searchProvider.BingKey = yourBingKey End Sub
您還可以自定義搜索結(jié)果計數(shù):
C#
private void Form1_Load(object sender, System.EventArgs e) { // ... searchProvider.SearchOptions.ResultsCount = 5; }
VB.NET
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load ' .... searchProvider.SearchOptions.ResultsCount = 5 End Sub
當(dāng)Map控件包含提供搜索數(shù)據(jù)的信息層時,Map控件會自動調(diào)用其內(nèi)置的搜索面板(默認(rèn)設(shè)置為 true)。
Map控件提供帶有附加參數(shù)的搜索功能,例如國家地區(qū)或郵政編碼。 使用這種方法,您可以構(gòu)建自定義搜索面板以從搜索服務(wù)中獲取額外的搜索結(jié)果。
注意:將屬性設(shè)置為 false 以在使用此方法時禁用默認(rèn)搜索面板。
要開始搜索位置,請調(diào)用或 方法。
例如,應(yīng)用程序的 UI 包含一個名為“tbKeywords”的文本框和一個名為“btnSearch”的按鈕。 要開始搜索,請單擊調(diào)用以下 Search 方法重載的 Search 按鈕:
C#
private void OnClick(object sender, EventArgs e) { searchProvider.Search(tbKeywords.Text); }
VB.NET
Private Sub OnClick(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click searchProvider.Search(tbKeywords.Text) End Sub
要獲取搜索結(jié)果,請?zhí)幚砘蚴录?
SearchCompleted 事件處理程序參數(shù)的提供后代類實(shí)例來存儲搜索結(jié)果。
結(jié)果包含與搜索位置關(guān)聯(lián)的顯示名稱、地址和地理坐標(biāo)(緯度和經(jīng)度)。
Form1.cs
void OnSearchCompleted(object sender, BingSearchCompletedEventArgs e) { if(e.Cancelled) return; if(e.RequestResult.ResultCode != RequestResultCode.Success) { meResult.Text = "The Bing Search service does not work for this location."; return; } StringBuilder resultList = new StringBuilder(""); int resCounter = 1; foreach(BingLocationInformation resultInfo in e.RequestResult.SearchResults) { resultList.Append(String.Format("Result {0}: \r\n", resCounter)); resultList.Append(String.Format("Name: {0}\r\n", resultInfo.DisplayName)); resultList.Append(String.Format("Address: {0}\r\n", resultInfo.Address.FormattedAddress)); resultList.Append(String.Format("Confidence level: {0}\r\n", resultInfo.Confidence)); resultList.Append(String.Format("Geographic coordinates: {0}\r\n", resultInfo.Location)); resultList.Append(String.Format("Match code: {0}\r\n", resultInfo.MatchCode)); resultList.Append(String.Format("______________________________\r\n")); resCounter++; } meResult.Text = resultList.ToString(); }
Form1.vb
Private Sub OnSearchCompleted(ByVal sender As Object, ByVal e As BingSearchCompletedEventArgs) If e.Cancelled Then Return End If If e.RequestResult.ResultCode <> RequestResultCode.Success Then meResult.Text = "The Bing Search service does not work for this location." Return End If Dim resultList As New StringBuilder("") Dim resCounter As Integer = 1 For Each resultInfo As BingLocationInformation In e.RequestResult.SearchResults resultList.Append(String.Format("Result {0}: " & ControlChars.CrLf, resCounter)) resultList.Append(String.Format("Name: {0}" & ControlChars.CrLf, resultInfo.DisplayName)) resultList.Append(String.Format("Address: {0}" & ControlChars.CrLf, resultInfo.Address.FormattedAddress)) resultList.Append(String.Format("Confidence level: {0}" & ControlChars.CrLf, resultInfo.Confidence)) resultList.Append(String.Format("Geographic coordinates: {0}" & ControlChars.CrLf, resultInfo.Location)) resultList.Append(String.Format("Match code: {0}" & ControlChars.CrLf, resultInfo.MatchCode)) resultList.Append(String.Format("______________________________" & ControlChars.CrLf)) resCounter += 1 Next resultInfo meResult.Text = resultList.ToString() End Sub
“New York”關(guān)鍵字的搜索結(jié)果如下圖所示。
DevExpress技術(shù)交流群6:600715373 歡迎一起進(jìn)群討論
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)