前面学习百度地图的一些基本的用法,这次我们一起来看一看百度地图的检索功能吧
百度地图的POI类中共有如下几个方法
PoiBoundSearchOption |
POI范围内检索参数
|
PoiCitySearchOption |
poi城市内检索参数
|
PoiDetailResult |
详情检索结果
|
PoiDetailSearchOption |
poi 详情检索参数
|
PoiNearbySearchOption |
附近检索参数
|
PoiResult |
poi搜索结果。
|
PoiSearch |
POI检索接口
|
我们现在先看下PoiSearch中的内容
void |
destroy()
释放检索对象
|
static PoiSearch |
newInstance()
创建PoiSearch实例
|
boolean |
searchInBound(PoiBoundSearchOption option)
范围内检索
|
boolean |
searchInCity(PoiCitySearchOption option)
城市内检索
|
boolean |
searchNearby(PoiNearbySearchOption option)
周边检索
|
boolean |
searchPoiDetail(PoiDetailSearchOption option)
POI 详情检索
|
void |
setOnGetPoiSearchResultListener(OnGetPoiSearchResultListener listener)
设置poi检索监听者
|
<span style="color:#ff6666;"> // 第一步,创建POI检索实例</span> poiSearch = PoiSearch.newInstance();
<span style="color:#ff6666;"> // 第二步,创建POI检索监听者;</span> OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() { public void onGetPoiResult(PoiResult result) { // 获取POI检索结果 if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) { Toast.makeText(POISearchMapActivity.this, "未找到结果", Toast.LENGTH_LONG).show(); return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { List<PoiInfo> allPoi = result.getAllPoi(); totalpages = result.getTotalPageNum(); String[] datas = new String[10]; for (int i = 0; i < 10; i++) { datas[i] = allPoi.get(i).name; } adapter = new ArrayAdapter<String>(POISearchMapActivity.this, android.R.layout.simple_list_item_1, datas); listView.setAdapter(adapter); return; } if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) { // 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表 String strInfo = "在"; for (CityInfo cityInfo : result.getSuggestCityList()) { strInfo += cityInfo.city; strInfo += ","; } strInfo += "找到结果"; Toast.makeText(POISearchMapActivity.this, strInfo, Toast.LENGTH_LONG).show(); } } public void onGetPoiDetailResult(PoiDetailResult result) { // 获取Place详情页检索结果 } };
<span style="color:#ff6666;">// 第三步,设置POI检索监听者;</span> poiSearch.setOnGetPoiSearchResultListener(poiListener);
// 这是范围 LatLngBounds.Builder b = new LatLngBounds.Builder(); b.include(new LatLng(39.976446, 116.320331)); b.include(new LatLng(39.900766, 116.32838)); b.include(new LatLng(39.910065, 116.491081)); b.include(new LatLng(39.976004, 116.475558)); LatLngBounds build = b.build();<pre name="code" class="java">获得LatLngBounds// 第四步,发起检索请求; poiSearch.searchInBound(new PoiBoundSearchOption().bound(build).keyword("美食").pageNum(currentPage)); 下面使用一个简单的例子来进行说明
<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="match_parent" tools:context=".IndexActivity" > <ListView android:id="@+id/mylistView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
package com.flyou.hellomap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.model.LatLngBounds; import com.baidu.mapapi.search.core.CityInfo; import com.baidu.mapapi.search.core.PoiInfo; import com.baidu.mapapi.search.core.SearchResult; import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener; import com.baidu.mapapi.search.poi.PoiBoundSearchOption; import com.baidu.mapapi.search.poi.PoiDetailResult; import com.baidu.mapapi.search.poi.PoiResult; import com.baidu.mapapi.search.poi.PoiSearch; public class POISearchMapActivity extends Activity { private int totalpages = 0; private ListView listView = null; private PoiSearch poiSearch; private int currentPage = 0; ArrayAdapter<String> adapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 在使用SDK各组件之前初始化context信息,传入ApplicationContext // 注意该方法要再setContentView方法之前实现 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_index); listView = (ListView) findViewById(R.id.mylistView); // 第一步,创建POI检索实例 poiSearch = PoiSearch.newInstance(); // 第三步,设置POI检索监听者; poiSearch.setOnGetPoiSearchResultListener(poiListener); } // 第二步,创建POI检索监听者; OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() { public void onGetPoiResult(PoiResult result) { // 获取POI检索结果 if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) { Toast.makeText(POISearchMapActivity.this, "未找到结果", Toast.LENGTH_LONG).show(); return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { List<PoiInfo> allPoi = result.getAllPoi(); totalpages = result.getTotalPageNum(); String[] datas = new String[10]; for (int i = 0; i < 10; i++) { datas[i] = allPoi.get(i).name; } Toast.makeText(POISearchMapActivity.this, "当前第"+currentPage+"页,共"+result.getTotalPageNum()+"页", 0).show(); adapter = new ArrayAdapter<String>(POISearchMapActivity.this, android.R.layout.simple_list_item_1, datas); listView.setAdapter(adapter); return; } if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) { // 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表 String strInfo = "在"; for (CityInfo cityInfo : result.getSuggestCityList()) { strInfo += cityInfo.city; strInfo += ","; } strInfo += "找到结果"; Toast.makeText(POISearchMapActivity.this, strInfo, Toast.LENGTH_LONG).show(); } } public void onGetPoiDetailResult(PoiDetailResult result) { // 获取Place详情页检索结果 } }; @Override protected void onDestroy() { super.onDestroy(); // 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 poiSearch.destroy(); } @Override protected void onResume() { super.onResume(); // 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 } @Override protected void onPause() { super.onPause(); // 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_MENU: // 这是范围 LatLngBounds.Builder b = new LatLngBounds.Builder(); b.include(new LatLng(39.976446, 116.320331)); b.include(new LatLng(39.900766, 116.32838)); b.include(new LatLng(39.910065, 116.491081)); b.include(new LatLng(39.976004, 116.475558)); LatLngBounds build = b.build(); // 第四步,发起检索请求; poiSearch.searchInBound(new PoiBoundSearchOption().bound(build).keyword("美食").pageNum(currentPage)); // poiSearch.searchInCity((new // PoiCitySearchOption()).city("开封").keyword("美食").pageNum(currentPage)); currentPage++; if (currentPage == totalpages) { currentPage = 0; } break; } return super.onKeyDown(keyCode, event); } }
poiSearch.searchInCity((new PoiCitySearchOption()).city("开封").keyword("美食").pageNum(currentPage));
PoiNearbySearchOption |
keyword(java.lang.String key)
检索关键字
|
PoiNearbySearchOption |
location(LatLng location)
检索位置
|
PoiNearbySearchOption |
pageCapacity(int pageCapacity)
设置每页容量,默认为每页10条
|
PoiNearbySearchOption |
pageNum(int pageNum)
分页编号
|
PoiNearbySearchOption |
radius(int radius)
设置检索的半径范围----------单位为米
|
poiSearch.searchNearby(new PoiNearbySearchOption() .keyword("大学") .location(new LatLng(34.82336,114.31496)) .radius(3000) );//可以为定位的坐标也可自己制定
原文地址:http://blog.csdn.net/u013616976/article/details/45442279