安卓百度地图之地址关键字搜索

步骤一:初始化以及配置
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();
			}
		});


	}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android毕业设计源码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值