步骤一:初始化以及配置
dependencies { compile 'com.baidu.lbsyun:BaiduMapSDK_Search:7.4.0' }SuggestionSearch mSuggestionSearch = SuggestionSearch.newInstance();
步骤二:内容的搜索
mSuggestionSearch.requestSuggestion((new SuggestionSearchOption())
.keyword(editable.toString()) // 关键字
.city("西安市")); // 城市
mSuggestionSearch.setOnGetSuggestionResultListener(new OnGetSuggestionResultListener() {
@Override
public void onGetSuggestionResult(SuggestionResult suggestionResult) {
mTipListStart.clear();
ChoiceAddressModel choiceAddressModel ;
for(int i=0;i<suggestionResult.getAllSuggestions().size();i++){
Log.i("pony_log", "address:" + suggestionResult.getAllSuggestions().get(i).getKey());
if (suggestionResult.getAllSuggestions().get(i).getKey().length()>0) {
choiceAddressModel = new ChoiceAddressModel();
choiceAddressModel.setAddress(suggestionResult.getAllSuggestions().get(i).getKey());
choiceAddressModel.setZuobiao(suggestionResult.getAllSuggestions().get(i).getPt());
choiceAddressModel.setZoom(suggestionResult.getAllSuggestions().get(i).getDistrict());
mTipListStart.add(choiceAddressModel);
}
}
Log.i("pony_log", "address:" + mTipListStart.size());
BusPoiAdapter busPoiAdapter = new BusPoiAdapter(SearchActivity.this, mTipListStart);
mListMessage.setAdapter(busPoiAdapter);
}
});
全部代码:
package com.meal;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
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;
import com.pony.adapter.BusPoiAdapter;
import com.pony.base.BaseActivity;
import com.pony.model.ChoiceAddressModel;
import com.pony.observer.ChoiceObservable;
import com.pony.util.ToastUtil;
import java.util.ArrayList;
import java.util.List;
public class SearchActivity extends BaseActivity {
// 标题
private TextView mTvTitle;
// 返回
private ImageView mIvBack;
private TextView mIvStu;
private ListView mListMessage;
private LinearLayout mllNomessage;
List<ChoiceAddressModel> mTipListStart = new ArrayList<ChoiceAddressModel>();
private EditText metName;
private SuggestionSearch mSuggestionSearch = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_im_recommend);
initWidget();
initData();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mIvBack:
finish();
break;
case R.id.mtvSearch:
if (TextUtils.isEmpty(metName.getText().toString())) {
ToastUtil.ShowCentre(this, "请输入搜索信息");
return;
}
hideSoft(this, metName);
break;
}
}
/*
* 隐藏软键盘
*/
public void hideSoft(Context context, EditText edittext) {
try {
if (edittext != null && context != null) {
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void initWidget() {
metName = (EditText) findViewById(R.id.metName);
mIvStu = (TextView) findViewById(R.id.mIvStu);
mIvStu.setText("添加");
mIvStu.setVisibility(View.GONE);
mllNomessage = (LinearLayout) findViewById(R.id.mllNomessage);
mListMessage = (ListView) findViewById(R.id.mListMessage);
mIvBack = (ImageView) findViewById(R.id.mIvBack);
mTvTitle = (TextView) findViewById(R.id.mTvTitle);
mTvTitle.setText("地址信息搜索");
mIvBack.setVisibility(View.VISIBLE);
mIvBack.setOnClickListener(this);
mIvStu.setOnClickListener(this);
metName.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if(editable.toString().length()>0){
mSuggestionSearch.requestSuggestion((new SuggestionSearchOption())
.keyword(editable.toString()) // 关键字
.city("西安市")); // 城市
mSuggestionSearch.setOnGetSuggestionResultListener(new OnGetSuggestionResultListener() {
@Override
public void onGetSuggestionResult(SuggestionResult suggestionResult) {
mTipListStart.clear();
ChoiceAddressModel choiceAddressModel ;
for(int i=0;i<suggestionResult.getAllSuggestions().size();i++){
Log.i("pony_log", "address:" + suggestionResult.getAllSuggestions().get(i).getKey());
if (suggestionResult.getAllSuggestions().get(i).getKey().length()>0) {
choiceAddressModel = new ChoiceAddressModel();
choiceAddressModel.setAddress(suggestionResult.getAllSuggestions().get(i).getKey());
choiceAddressModel.setZuobiao(suggestionResult.getAllSuggestions().get(i).getPt());
choiceAddressModel.setZoom(suggestionResult.getAllSuggestions().get(i).getDistrict());
mTipListStart.add(choiceAddressModel);
}
}
Log.i("pony_log", "address:" + mTipListStart.size());
BusPoiAdapter busPoiAdapter = new BusPoiAdapter(SearchActivity.this, mTipListStart);
mListMessage.setAdapter(busPoiAdapter);
}
});
}
}
});
}
@Override
public void initData() {
// 初始化建议搜索模块,注册建议搜索事件监听
mSuggestionSearch = SuggestionSearch.newInstance();
mListMessage.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
//
// Log.i("pony_log","address111111111:"+mTipListStart.get(pos).getAddress());
// Intent intent = new Intent();
// //把需要返回的数据存放在intent
// intent.putExtra("zuobiao", mTipListStart.get(pos));
// intent.putExtra("address", mTipListStart.get(pos).getAddress()+"");
// //设置返回数据
// setResult(RESULT_OK, intent);
if(SearchActivity.this.getIntent().getStringExtra("type").equals("1")){
mTipListStart.get(pos).setType(1);
}else{
mTipListStart.get(pos).setType(2);
}
ChoiceObservable.getInstance().notifyStepChange(mTipListStart.get(pos));
finish();
}
});
}
}