百度地圖SDK-----百度地圖線上建議查詢,結合AutoCompleteTextView實現搜尋下拉列表。
這是百度地圖 POISearch的效果,
這是自己寫的效果
首先實現這個功能主要用到了兩個部分
第一個部分 AutoCompleteTextView
具體使用參考 http://blog.csdn.net/iamkila/article/details/7230160
第二個部分 百度地圖的線上搜尋建議功能。
http://developer.baidu.com/map/index.php?title=androidsdk/guide/retrieval
實現這個功能主要過程 可以分成幾步
1.sugAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line);
給AutoCompleteTextView建立一個adapter
(注意:這裡建立Adapter時沒有提供 字串資料來源, 而是動態的新增的)
2.keyWorldsView.setAdapter(sugAdapter);
3.AutoCompleteTextView 新增 onTextChangedListener 監聽器
複寫 三個方法
afterTextChanged
beforeTextChanged
onTextChanged(){
在該方法內部 呼叫 百度sdk提供的搜尋建議方法
/**
* 使用建議搜尋服務獲取建議列表,結果在onGetSuggestionResult()中更新
*/
mSuggestionSearch
.requestSuggestion((new SuggestionSearchOption())
.keyword(cs.toString()).city(city));
}
4.複寫 onGetSuggestionResult() 方法 複寫改方法有兩種方式
(1)OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
public void onGetSuggestionResult(SuggestionResult res) {
if (res == null || res.getAllSuggestions() == null) {
return;
//未找到相關結果
}
//獲取線上建議檢索結果
}
};
自己建立一個監聽器,複寫方法。
讓mSuggestionSearch 新增監聽
mSuggestionSearch.setOnGetSuggestionResultListener(listener);
(2) 讓 Activity 實現 介面 implements OnGetSuggestionResultListener
在Activity中 複寫onGetSuggestionResult 方法
public void onGetSuggestionResult(SuggestionResult res) {
if (res == null || res.getAllSuggestions() == null) {
return;
//未找到相關結果
}
//獲取線上建議檢索結果
}
};
5.
第 3 步中的 mSuggestionSearch
.requestSuggestion((new SuggestionSearchOption())
.keyword(cs.toString()).city(city));
方法呼叫之後就會回撥
第 4 步中的 onGetSuggestionResult 方法。 所以要在 onGetSuggestionResult 方法中給adapter提供字串資料來源
sugAdapter.clear();
每次提供資料來源的時候 都要清除上一次搜尋的結果
迴圈讀取結果 並且新增到 adapter中
for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) {
if (info.key != null)
sugAdapter.add(info.key);
}
sugAdapter.notifyDataSetChanged();提醒資料發生改變,這時 AutoCompleteTextView就會使用Adapter提供的
字串資料來源
然後給出 這幾個步驟對應的 截圖。
---------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------------------------
最後給出官方BaiduMapsApiDemo 中的 POISearch.java 中的程式碼
可以自己對照著比較
package baidumapsdk.demo; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.EditText; import android.widget.Toast; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.SupportMapFragment; import com.baidu.mapapi.overlayutil.PoiOverlay; import com.baidu.mapapi.search.core.CityInfo; import com.baidu.mapapi.search.core.PoiInfo; import com.baidu.mapapi.search.core.SearchResult; import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener; import com.baidu.mapapi.search.poi.PoiCitySearchOption; import com.baidu.mapapi.search.poi.PoiDetailResult; import com.baidu.mapapi.search.poi.PoiDetailSearchOption; import com.baidu.mapapi.search.poi.PoiResult; import com.baidu.mapapi.search.poi.PoiSearch; import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener; import com.baidu.mapapi.search.sug.SuggestionResult; import com.baidu.mapapi.search.sug.SuggestionSearch; import com.baidu.mapapi.search.sug.SuggestionSearchOption; /** * 演示poi搜尋功能 */ public class PoiSearchDemo extends FragmentActivity implements OnGetPoiSearchResultListener, OnGetSuggestionResultListener { private PoiSearch mPoiSearch = null; private SuggestionSearch mSuggestionSearch = null; private BaiduMap mBaiduMap = null; /** * 搜尋關鍵字輸入視窗 */ private AutoCompleteTextView keyWorldsView = null; private ArrayAdapter<String> sugAdapter = null; private int load_Index = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_poisearch); // 初始化搜尋模組,註冊搜尋事件監聽 mPoiSearch = PoiSearch.newInstance(); mPoiSearch.setOnGetPoiSearchResultListener(this); mSuggestionSearch = SuggestionSearch.newInstance(); mSuggestionSearch.setOnGetSuggestionResultListener(this); keyWorldsView = (AutoCompleteTextView) findViewById(R.id.searchkey); sugAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line); keyWorldsView.setAdapter(sugAdapter); mBaiduMap = ((SupportMapFragment) (getSupportFragmentManager() .findFragmentById(R.id.map))).getBaiduMap(); /** * 當輸入關鍵字變化時,動態更新建議列表 */ keyWorldsView.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable arg0) { } @Override public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { } @Override public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) { if (cs.length() <= 0) { return; } String city = ((EditText) findViewById(R.id.city)).getText() .toString(); /** * 使用建議搜尋服務獲取建議列表,結果在onSuggestionResult()中更新 */ mSuggestionSearch .requestSuggestion((new SuggestionSearchOption()) .keyword(cs.toString()).city(city)); } }); } @Override protected void onPause() { super.onPause(); } @Override protected void onResume() { super.onResume(); } @Override protected void onDestroy() { mPoiSearch.destroy(); mSuggestionSearch.destroy(); super.onDestroy(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); } /** * 影響搜尋按鈕點選事件 * * @param v */ public void searchButtonProcess(View v) { EditText editCity = (EditText) findViewById(R.id.city); EditText editSearchKey = (EditText) findViewById(R.id.searchkey); mPoiSearch.searchInCity((new PoiCitySearchOption()) .city(editCity.getText().toString()) .keyword(editSearchKey.getText().toString()) .pageNum(load_Index)); } public void goToNextPage(View v) { load_Index++; searchButtonProcess(null); } public void onGetPoiResult(PoiResult result) { if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) { Toast.makeText(PoiSearchDemo.this, "未找到結果", Toast.LENGTH_LONG) .show(); return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { mBaiduMap.clear(); PoiOverlay overlay = new MyPoiOverlay(mBaiduMap); mBaiduMap.setOnMarkerClickListener(overlay); overlay.setData(result); overlay.addToMap(); overlay.zoomToSpan(); return; } if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) { // 當輸入關鍵字在本市沒有找到,但在其他城市找到時,返回包含該關鍵字資訊的城市列表 String strInfo = "在"; for (CityInfo cityInfo : result.getSuggestCityList()) { strInfo += cityInfo.city; strInfo += ","; } strInfo += "找到結果"; Toast.makeText(PoiSearchDemo.this, strInfo, Toast.LENGTH_LONG) .show(); } } public void onGetPoiDetailResult(PoiDetailResult result) { if (result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(PoiSearchDemo.this, "抱歉,未找到結果", Toast.LENGTH_SHORT) .show(); } else { Toast.makeText(PoiSearchDemo.this, result.getName() + ": " + result.getAddress(), Toast.LENGTH_SHORT) .show(); } } @Override public void onGetSuggestionResult(SuggestionResult res) { if (res == null || res.getAllSuggestions() == null) { return; } sugAdapter.clear(); for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) { if (info.key != null) sugAdapter.add(info.key); } sugAdapter.notifyDataSetChanged(); } private class MyPoiOverlay extends PoiOverlay { public MyPoiOverlay(BaiduMap baiduMap) { super(baiduMap); } @Override public boolean onPoiClick(int index) { super.onPoiClick(index); PoiInfo poi = getPoiResult().getAllPoi().get(index); // if (poi.hasCaterDetails) { mPoiSearch.searchPoiDetail((new PoiDetailSearchOption()) .poiUid(poi.uid)); // } return true; } } }