Android高仿百度地图公交与位置建议搜索(四)

Android高仿百度地图公交与位置建议搜索

转载请注明:http://blog.csdn.net/smile5005/article/details/40475393

        最近自己在开发一个Android的app,由于需要主动搜索位置以及公交信息,所以想先借鉴一下百度地图的位置公交搜索模块,姑且先模仿百度地图来实现。

1、UI布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/common_background_color"
    android:orientation="vertical" >
    	 
   	<include
   		android:id="@+id/common_search_action_bar"
   		layout="@layout/baidu_search_action_bar" />

   	<ListView
   	    android:id="@+id/lv_position_list"
   	    android:layout_marginLeft="5dp"
   	    android:layout_marginRight="5dp"
   	    android:layout_marginTop="5dp"
   	    android:layout_marginBottom="5dp"
   	    android:layout_width="match_parent"
   	    android:layout_height="wrap_content" >
   	</ListView>

</LinearLayout>
其中Include是自己写的一个共同的搜索模块的View,大家可以根据自己的具体项目来替换。

2、Activity中代码如下:
package com.remind.ui;

import java.util.ArrayList;
import java.util.List;

import com.baidu.mapapi.search.core.CityInfo;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.core.PoiInfo.POITYPE;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiDetailResult;
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;
import com.mr.remind.R;
import com.remind.adapter.PoiResultAdapter;
import com.remind.bean.MrPoiResult;
import com.remind.config.Constant;
import com.remind.db.AlarmColumn;
import com.remind.db.DBHelper;
import com.remind.db.PoiHistoryColumn;
import com.remind.utils.LogUtils;
import com.remind.widget.ClearEditText;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

/**
 * 搜索细节的Activity;
 * @author Render
 */
public class SearchDetailActivity extends Activity implements OnGetSuggestionResultListener{

	private static final String TAG = "SearchDetailActivity";
	
	private SuggestionSearch mSuggestionSearch = null;   //百度地图建议搜索对象;
	private ClearEditText mEditText;                     //自定义的一个Edit控件,也可以用标准的EditView替代.
	private ListView mListView;
	private PoiResultAdapter mPoiResultAdapter=null; //自定义的ListView适配器对象。
	private List<MrPoiResult> mPoiResults=null;      //查询后的结果数据链表;其中MrPoiResult是自定义的Bean对象。
        private String mSearchName;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search_detail);
		
		initView();
		initData();
	}
	
	private void initView(){
		
	       mEditText=(ClearEditText) findViewById(R.id.et_search);
	       mListView=(ListView) findViewById(R.id.lv_position_list);
	}
	
	private void initData(){
		
		mPoiResults=new ArrayList<MrPoiResult>();
		mPoiResultAdapter=new PoiResultAdapter(this, mPoiResults);
		mListView.setAdapter(mPoiResultAdapter);
		
		mSuggestionSearch = SuggestionSearch.newInstance();
		mSuggestionSearch.setOnGetSuggestionResultListener(this);
		
		mEditText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
				mSearchName=mEditText.getText().toString();
				excuteSuggestionSearch();
			}
		});
	}

	/**
	 * 开始执行建议搜索;
	 */
	private void excuteSuggestionSearch(){

		mPoiResults.clear();
		
		//清空链表;
		if(mSearchName.equals("")||mSearchName==null){
			mPoiResultAdapter.updateListView(mPoiResults,null);
			return;
		}
		
                //根据输入的名称和城市进行建议搜索,建议搜索很强大,即使固定了城市,也可以搜索到别的地方。
		mSuggestionSearch
		.requestSuggestion((new SuggestionSearchOption())
				.keyword(mSearchName).city(Constant.city));
		
	}
	
	@Override
	public void onGetSuggestionResult(SuggestionResult res) {
		
		if (res == null || res.getAllSuggestions() == null) {
			return;
		}
		
		for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) {
			if (info.key != null){
				MrPoiResult poiResult=new MrPoiResult();
				
                                //记录搜索到的城市,名称和地区;
				poiResult.setCity(info.city);
				poiResult.setName(info.key);
				poiResult.setAddress(info.city+info.district);
				
				mPoiResults.add(poiResult);
			}
		}
		mPoiResultAdapter.updateListView(mPoiResults,mSearchName);
	}
	
	@Override
	protected void onDestroy() {
		
	       mSuggestionSearch.destroy();
	       super.onDestroy();
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}
}

上文的Activity主要是进行简单的建议搜索,并把搜索的结果显示在ListVIew中,接下来我们看看ListView中的适配器对象,由于在项目中我们会经常用到适配器对象,因此这里把适配器对象写成了一个模板基类,先看看基类适配器对象。

3、适配器基类对象
package com.remind.adapter.base;

import java.util.LinkedList;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public abstract class AdapterBase<T> extends BaseAdapter {
	
	private final List<T> mList = new LinkedList<T>();
	
	public List<T> getList(){
		return mList;
	}
	
	public void appendToList(List<T> list) {
		if (list == null) {
			return;
		}
		mList.addAll(list);
		notifyDataSetChanged();
	}

	public void appendToTopList(List<T> list) {
		if (list == null) {
			return;
		}
		mList.addAll(0, list);
		notifyDataSetChanged();
	}

	public void clear() {
		mList.clear();
		notifyDataSetChanged();
	}
	@Override
	public int getCount() {
		
		if(mList!=null){
			return mList.size();
		}
		return 0;
	}

	@Override
	public Object getItem(int position) {
		if(position > mList.size()-1){
			return null;
		}
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (position == getCount() - 1) {
			onReachBottom();
		}
		return getExView(position, convertView, parent);
	}
	
        // 两个基类对象,由子类进行扩展;
	protected abstract View getExView(int position, View convertView, ViewGroup parent);
	protected abstract void onReachBottom();
}

4、ListView中的适配器对象,代码如下:
package com.remind.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mr.remind.R;
import com.remind.adapter.base.AdapterBase;
import com.remind.bean.MrPoiResult;

public class PoiResultAdapter extends AdapterBase<MrPoiResult>{

	private Context ct;
	private ViewHolder viewHolder=null;
	private String mSearchKey;        //搜索的关键字对象;
	
	public PoiResultAdapter(Context ct,List<MrPoiResult> postions){
		this.ct=ct;
		appendToList(postions);
	}

	public void updateListView(List<MrPoiResult> datas,String searchKey){

		mSearchKey=searchKey;
		
		clear();
		appendToList(datas);
		notifyDataSetChanged();
	}
	
	static class ViewHolder {
		
		ImageView imageView;
		TextView tvName;
		TextView tvAddress;
	}

	@Override
	protected View getExView(int position, View convertView, ViewGroup parent) {
		
		MrPoiResult poiResult=getList().get(position);
		
		if (convertView == null) {

			convertView = LayoutInflater.from(ct).inflate(R.layout.item_poi_result, null);
			viewHolder = new ViewHolder();
		
			viewHolder.imageView = (ImageView) convertView.findViewById(R.id.iv_postion_icon);
			viewHolder.tvName=(TextView)convertView.findViewById(R.id.tv_position_name);
			viewHolder.tvAddress=(TextView)convertView.findViewById(R.id.tv_position_address);
			
			convertView.setTag(viewHolder);
			
		} else{
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
                //该部分主要是针对于搜索的结果,用不同的颜色标记出和关键字相同的部分。
		String name=poiResult.getName();
		int nBegIndex=name.indexOf(mSearchKey);
		int nEndIndex=nBegIndex+mSearchKey.length();
		
		if(nBegIndex!=-1)
		{
			//设置指定位置文字的颜色
			SpannableStringBuilder style=new SpannableStringBuilder(name);   
			style.setSpan(new ForegroundColorSpan(Color.rgb(118, 173, 255)),
					nBegIndex,nEndIndex,Spannable.SPAN_EXCLUSIVE_INCLUSIVE);    
			viewHolder.tvName.setText(style);
		}
                
                //下面部分主要是对于如果搜索到的地址为空时,把搜索的名字动态居中显示。
		if(poiResult.getAddress()==null||poiResult.getAddress().equals("")){
			
			RelativeLayout.LayoutParams lParams=
					new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			
			lParams.addRule(RelativeLayout.CENTER_VERTICAL);
			viewHolder.tvName.setLayoutParams(lParams);
			viewHolder.tvAddress.setText("");
			
		}else{
			
			RelativeLayout.LayoutParams lParams=
					new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
					RelativeLayout.LayoutParams.WRAP_CONTENT);
			
			lParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
			viewHolder.tvName.setLayoutParams(lParams);
			viewHolder.tvAddress.setText(poiResult.getAddress());
		}

		return convertView;
	}

	@Override
	protected void onReachBottom() {
	
	}
}

5、建议搜索结果如下,分别进行了位置搜索和公交搜索,结果如下:























评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值