6.7 模糊位置自定义搜索
//城市信息TextView
TextView _tv_city = null;
//搜寻的autoComplete的keyword (起点)
AutoCompleteTextView _autotv_startAddr = null;
//搜寻的autoComplete的keyword(终点)
AutoCompleteTextView _autotv_finalAddr = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_main);
//初始化mapView 容器
mMapView = (MapView) findViewById(R.id.map);
//启动mapView显示主地图
mMapView.onCreate(savedInstanceState);
//获取mMap操作对象
if (mMap == null) {
mMap = mMapView.getMap();
if (mUiSettings == null) {
mUiSettings = mMap.getUiSettings();
}
}
/* -------------- 初始化搜索布局 -------------- */
initSearchBarLayout();
/*------ 开启定位服务(以自我为中心) ------*/
doLocation();
//---------- 初始化地图布局-----------
initMapLayout();
}
// ============ 设置搜索栏布局 ============
public void initSearchBarLayout() {
//城市显示textView
_tv_city = (TextView)findViewById(R.id.tv_city);
//得到起始地址和最终地址搜索控件
_autotv_startAddr = (AutoCompleteTextView)findViewById(R.id.autotv_startAddr);
_autotv_finalAddr = (AutoCompleteTextView)findViewById(R.id.autotv_finalAddr);
//显示城市标记
getCity();
/*-------- 设置关键字模糊搜索 --------*/
setAutoCompleteAddress();
}
//为搜索TextView设置模糊搜索,并且显示
void setAutoCompleteAddress() {
//默认2个字才会开启匹配列表显示,这里改为1个字就开始显示
_autotv_startAddr.setThreshold(1);
_autotv_startAddr.addTextChangedListener(new TextWatcher() {
//控件内容 改变之前会触发此回调
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
//当ui控件的内容发生改变,会触发此回调
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//得到用户输入的 关键字
final String keyword = _autotv_startAddr.getText().toString().trim();
if (_tv_city.getText().toString().length() == 0) {
return;
}
if (keyword ==null || keyword.length() == 0) {
return;
}
Log.e("Amap", "用户当前输入的模糊关键字是 " + keyword);
/*
得到根据此模糊关键字搜索到的全部模糊匹配的地址 集合
此集合由高德地图提供,不需要我们自己写模糊算法
*/
// 得到查询条件
InputtipsQuery query = new InputtipsQuery(keyword, _tv_city.getText().toString());
// 得到查询点搜索句柄(实际上高德地图会根据 搜索关键字 来定位匹配的地址)
Inputtips search = new Inputtips(getApplicationContext(), query);
// 设置 定位监听回调
search.setInputtipsListener(new Inputtips.InputtipsListener() {
@Override
public void onGetInputtips(List<Tip> list, int rCode) {
// 高德地图会将全部定位到的 地址 放在list列表中,每个元素类型是Tip
// 下面就是将全部的地址信息,放在我们的_autotv_keyword控件中
// 1 首先将Tip list信息封装到一个 ArrayList<String>中
ArrayList<String> resultList = new ArrayList<String>();
for (int i = 0;i < list.size(); i++) {
resultList.add(list.get(i).getName());
Log.e("Amap", "根据 " + keyword + "匹配到的结果" + list.get(i).toString());
}
// 2 创建一个根据ArrayList<String>的ArrayAdapter<String>
ArrayAdapter<String> resultAdapter = new ArrayAdapter<String>(
getApplicationContext(),
android.R.layout.simple_list_item_1,
resultList
);
// 3 将这个Adapter设置给AutoCompleteTextView控件
_autotv_startAddr.setAdapter(resultAdapter);
// 4 启动通知 ,一旦这个Adapter数据内容发生改变,告知控件
resultAdapter.notifyDataSetChanged();
}
});
// 开始 定位
search.requestInputtipsAsyn();
}
//控件内容 改变之后会触发此回调
@Override
public void afterTextChanged(Editable s) {
}
});
}