上次我们一块研究下了最基本的POI检索,今天我们来看下,个人觉得比较有用的公交线路检索。
下面我们看下这个类中的方法
程序包 com.baidu.mapapi.search.busline
BusLineResult |
公共交通信息查询结果 |
BusLineResult.BusStation |
公交站点信息 |
BusLineResult.BusStep |
公交路线分段信息 |
BusLineSearch |
城市公交信息(包含地铁信息)查询 该接口用于查询整条公交线路信息 公交换乘路线查询请参看 RoutePlanSearch
|
BusLineSearchOption |
城市公交信息查询参数 |
void |
destroy()
释放检索对象资源 |
static BusLineSearch |
newInstance()
获取一个新的检索实例 |
boolean |
searchBusLine(BusLineSearchOption option)
公交检索入口 |
void |
setOnGetBusLineSearchResultListener(OnGetBusLineSearchResultListener listener)
设置公交详情检索结果监听者 |
>>BusLineSearchOption 参数如下
BusLineSearchOption |
city(java.lang.String city)
设置查询城市 |
BusLineSearchOption |
uid(java.lang.String uid)
设置公交路线uid. |
结合POI搜索完成今天的例子
xml文件依旧仅仅包含一个listview
<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.ArrayList; 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.search.busline.BusLineResult; import com.baidu.mapapi.search.busline.BusLineResult.BusStation; import com.baidu.mapapi.search.busline.BusLineSearch; import com.baidu.mapapi.search.busline.BusLineSearchOption; import com.baidu.mapapi.search.busline.OnGetBusLineSearchResultListener; 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.PoiCitySearchOption; import com.baidu.mapapi.search.poi.PoiDetailResult; import com.baidu.mapapi.search.poi.PoiResult; import com.baidu.mapapi.search.poi.PoiSearch; public class BusMapActivity extends Activity { private int totalpages = 0; private ListView listView = null; private PoiSearch poiSearch; private int currentPage = 0; ArrayAdapter<String> adapter = null; protected String busLineId; private BusLineSearch buslineSearch ; @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); // 第一步,创建Busline检索实例 buslineSearch = BusLineSearch.newInstance(); // 第三步,设置<span style="font-family: Arial, Helvetica, sans-serif;">Busline</span><span style="font-family: Arial, Helvetica, sans-serif;">检索监听者;</span> buslineSearch.setOnGetBusLineSearchResultListener(busLineListener); } <span style="color:#ff0000;"> OnGetBusLineSearchResultListener busLineListener =new OnGetBusLineSearchResultListener() { @Override public void onGetBusLineResult(BusLineResult result) { String title; List<BusStation> stations = result.getStations(); List<String> datas=new ArrayList<String>(); for (BusStation busStation : stations) { title = busStation.getTitle(); datas.add(title); }</span> listView.setAdapter(new ArrayAdapter<String>(BusMapActivity.this, android.R.layout.simple_list_item_1, datas)); } }; // 第二步,创建POI检索监听者; OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() { public void onGetPoiResult(PoiResult result) { // 获取POI检索结果 if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) { Toast.makeText(BusMapActivity.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]; //遍历所有POI,找到类型为公交线路的POI <span style="color:#ff0000;"> for (PoiInfo poi : result.getAllPoi()) { if (poi.type == PoiInfo.POITYPE.BUS_LINE ||poi.type == PoiInfo.POITYPE.SUBWAY_LINE) { //说明该条POI为公交信息,获取该条POI的UID busLineId = poi.uid; break; } } //如下代码为发起检索代码,定义监听者和设置监听器的方法与POI中的类似 buslineSearch .searchBusLine((new BusLineSearchOption() .city("开封") .uid(busLineId)));</span> } if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) { // 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表 String strInfo = "在"; for (CityInfo cityInfo : result.getSuggestCityList()) { strInfo += cityInfo.city; strInfo += ","; } strInfo += "找到结果"; Toast.makeText(BusMapActivity.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)); */ <span style="color:#ff0000;"> poiSearch.searchInCity((new PoiCitySearchOption()).city("开封").keyword( "33").pageNum(currentPage));</span> /** * poiSearch.searchNearby(new PoiNearbySearchOption() .keyword("大学") * .location(new LatLng(34.82336,114.31496)) .radius(3000) * );//可以为定位的坐标也可自己制定 */ currentPage++; if (currentPage == totalpages) { currentPage = 0; } break; } return super.onKeyDown(keyCode, event); } }
运行结果为开封市33路的线路
下图为百度搜索的结果
时间: 2024-11-06 17:23:14