Android 百度地图最新SDK v3.2.0和Android定位SDK:v5.0应用(4)

这一篇要实现的百度地图的POI检索

先上效果图(Android5.0.1):



1.首百度地图poi 检索 OnGetPoiSearchResultListener简单介绍


主要是通过实现百度地图poi 检索结果回调(OnGetPoiSearchResultListener),下面把主要的几个类列出来:

PoiBoundSearchOption
POI范围内检索参数
PoiCitySearchOption
poi城市内检索参数
PoiDetailResult
详情检索结果
PoiDetailSearchOption
poi 详情检索参数
PoiNearbySearchOption
附近检索参数

使用方法大同小异,不作说明。


2.百度地图poi 检索页面顶部布局


titlebar_search.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:background="@color/lightslategray"
    android:paddingLeft="12dp"
    android:paddingRight="12dp" >

    <ImageView
        android:id="@+id/imv_back"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:background="@drawable/icon_back_normal"
        android:contentDescription="@null" />

    <AutoCompleteTextView
        android:id="@+id/et_search_key"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="3dp"
        android:layout_toLeftOf="@+id/tv_confirm"
        android:layout_toRightOf="@+id/imv_back"
        android:background="@drawable/bg_set_origion_text"
        android:hint="搜索地点"
        android:paddingLeft="8dip"
        android:textColor="#d3d3d3"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/tv_confirm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="12dp"
        android:text="搜索"
        android:background="@color/darkgray"
        android:textColor="@color/white"
        android:textSize="18sp" />

</RelativeLayout>

3.百度地图poi 检索结果列表的Adapter和布局


SeachAdpter.java


public class SeachAdpter extends BaseAdapter {
	// 运行上下文
	private Context context;

	private List<PoiInfo> list;

	public SeachAdpter(Context context, List<PoiInfo> list) {
		this.context = context;
		this.list = list;
	}

	@Override
	// 得到总的数量
	public int getCount() {
		return list.size();
	}

	@Override
	// 根据ListView位置返回View
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	// 根据ListView位置得到List中的ID
	public long getItemId(int position) {
		return position;
	}

	@Override
	// 根据位置得到View对象
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_search, null);
			holder = new ViewHolder();
			holder.tv_address = (TextView) convertView
					.findViewById(R.id.tv_add);
			holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
			convertView.setTag(holder);// 绑定ViewHolder对象
		} else {

			holder = (ViewHolder) convertView.getTag();// 取出ViewHolder对象

		}

		holder.tv_name.setText(list.get(position).name);
		holder.tv_address.setText(list.get(position).address);
		return convertView;
	}

	/* 存放控件 */
	public final class ViewHolder {
		public TextView tv_address;
		public TextView tv_name;

	}
}

item_search.xml

<?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="@drawable/bg_search_item"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:src="@drawable/icon_location" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/tv_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textColor="@color/darkgray"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/tv_add"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textColor="#DFDFDF" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

4.百度地图poi检索实现


已上传Demo中的seachAdpter.notifyDataSetChanged()位置放错了……尴尬尴尬尴尬
<pre name="code" class="java">@Override
	public void onGetPoiResult(PoiResult result) {
		if (result == null
				|| result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
			return;
		}
		if (result.error == SearchResult.ERRORNO.NO_ERROR) {
			list.addAll(result.getAllPoi());
			/*
			 * for (PoiInfo cityingo : list) { L.i(TAG, cityingo.name + "," +
			 * cityingo.address);
			 * 
			 * }
			 */
			if (seachAdpter == null) {
				seachAdpter = new SeachAdpter(getApplicationContext(), list);
				lv.setAdapter(seachAdpter);
			} else {
				seachAdpter.notifyDataSetChanged();
			}
			
			return;
		}
		if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
			T.showLong(getApplicationContext(), "当前城市未找到");
			L.i(TAG, "当前城市未找到");
			return;
		}
	}


 
  
 然后调用mPoiSearch.searchNearby()方法: 
 
mPoiSearch.searchInCity((new PoiCitySearchOption()).city(cityName)
				.keyword(search_key.getText().toString()).pageNum(0)
				.pageCapacity(20));
就可以了。

也可以通过传递经纬度调用mPoiSearch.searchInCity()方法:
mPoiSearch.searchNearby(new
		PoiNearbySearchOption().keyword("搜索关键字").location(latLng).pageCapacity(20).pageNum(0).radius(10*1000));
		// 搜索不到,会把关键字拆开再搜索


5.百度地图SuggestionSearch使用

 
 当输入关键字变化时,动态更新建议列表
/**
		 * 当输入关键字变化时,动态更新建议列表
		 */
		search_key.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 = cityName;
				/**
				 * 使用建议搜索服务获取建议列表,结果在onSuggestionResult()中更新
				 */
				mSuggestionSearch
						.requestSuggestion((new SuggestionSearchOption())
								.keyword(cs.toString()).city(city));
			}
		});

@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();
	}

至此,已经实现了截图上的效果了。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值