实现效果图 如下
这是百度地图 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(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 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(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;
}
}
}