POI查找主要有3个:全城查找,周边查找,区域查找。
当然这里的数据都是从百度数据库取出来的,如果是自己数据库的东西,这需要重新定义一些经纬度信息。
下面我们先来介绍全城查找:废话不多说,直接上代码:
1:建立xml布局文件:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="在"></TextView>
<EditText
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="北京" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="市内找"></TextView>
<AutoCompleteTextView
android:id="@+id/searchkey"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.88"
android:text="餐厅" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:orientation="horizontal">
<Button
android:id="@+id/search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="12"
android:onClick="searchButtonProcess"
android:padding="10dip"
android:text="城市内搜索" />
</LinearLayout>
<!--<fragment
android:id="@+id/map"
class="com.baidu.mapapi.map.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->
<TextView
android:id="@+id/tv_test"
android:layout_width="match_parent"
android:layout_height="40dip" />
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
第二步:
初始化搜索模块,注册搜索事件监听
mPoiSearch = PoiSearch.newInstance();
mPoiSearch.setOnGetPoiSearchResultListener(this);
第三步:初始化建议搜索模块,这个是保证搜索合法性:
mSuggestionSearch = SuggestionSearch.newInstance(); mSuggestionSearch.setOnGetSuggestionResultListener(this);sugAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line); keyWorldsView.setAdapter(sugAdapter); keyWorldsView.setThreshold(1);第四步:初始化地图控件
// 初始化mapview对象,并且设置显示缩放控件 mMapView = (MapView) findViewById(R.id.bmapView1); //mapView.set mBaiduMap = mMapView.getMap();第五步:输入关键字时,建议更新列表实现:
/** * 当输入关键字变化时,动态更新建议列表 */ keyWorldsView.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; } /** * 使用建议搜索服务获取建议列表,结果在onSuggestionResult()中更新 */ mSuggestionSearch .requestSuggestion((new SuggestionSearchOption()) .keyword(cs.toString()).city(editCity.getText().toString())); } });第六步:获取在线搜索结果实现:
/** * 获取在线建议搜索结果,得到requestSuggestion返回的搜索结果 * @param res */ @Override public void onGetSuggestionResult(SuggestionResult res) { if (res == null || res.getAllSuggestions() == null) { return; } suggest = new ArrayList<String>(); for (SuggestionResult.SuggestionInfo info : res.getAllSuggestions()) { if (info.key != null) { suggest.add(info.key); } } sugAdapter = new ArrayAdapter<String>(PoiSearchDemo.this, android.R.layout.simple_dropdown_item_1line, suggest); keyWorldsView.setAdapter(sugAdapter); sugAdapter.notifyDataSetChanged(); }第七步:城市内点击事件实现:
/** * 响应城市内搜索按钮点击事件 * * @param v */ public void searchButtonProcess(View v) { searchType = 1; String citystr = editCity.getText().toString(); String keystr = keyWorldsView.getText().toString(); mPoiSearch.searchInCity((new PoiCitySearchOption()) .city(citystr).keyword(keystr).pageNum(loadIndex)); }第八步:获取poi搜索结果:
/** * 获取POI搜索结果,包括searchInCity,searchNearby,searchInBound返回的搜索结果 * @param result */ public void onGetPoiResult(PoiResult result) { if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) { Toast.makeText(PoiSearchDemo.this, "未找到结果", Toast.LENGTH_LONG) .show(); return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { mBaiduMap.clear(); PoiOverlay overlay = new MyPoiOverlay(mBaiduMap); mBaiduMap.setOnMarkerClickListener(overlay); List<PoiInfo> poiInfoList=result.getAllPoi(); for(PoiInfo poiInfo:poiInfoList){ Log.e("搜索信息","地址"+poiInfo.address+"名字"+poiInfo.name+"电话"+poiInfo.phoneNum); } overlay.setData(result); overlay.addToMap(); overlay.zoomToSpan(); switch( searchType ) { case 2: showNearbyArea(center, radius); break; case 3: showBound(searchbound); break; default: break; } return; } if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) { // 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表 String strInfo = "在"; for (CityInfo cityInfo : result.getSuggestCityList()) { strInfo += cityInfo.city; strInfo += ","; } strInfo += "找到结果"; Toast.makeText(PoiSearchDemo.this, strInfo, Toast.LENGTH_LONG) .show(); } }完成上述八步:城市内搜索便完成:结果展示:
项目中overlay工具包下载请见:
百度地图工具包overlayutils,百度地图开发必备工具包