百度地图之搜索公交路线

来源:互联网 发布:调薪后薪酬数据分析 编辑:程序博客网 时间:2024/04/27 04:50

百度SDK提供了查询公交路线的功能,并且可以浏览路线要经过的每一个站,百度Demo代码如下:

Activity:

package com.home;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;import com.baidu.mapapi.map.MKMapTouchListener;import com.baidu.mapapi.map.MapView;import com.baidu.mapapi.map.PopupClickListener;import com.baidu.mapapi.map.PopupOverlay;import com.baidu.mapapi.map.RouteOverlay;import com.baidu.mapapi.search.MKAddrInfo;import com.baidu.mapapi.search.MKBusLineResult;import com.baidu.mapapi.search.MKDrivingRouteResult;import com.baidu.mapapi.search.MKPoiInfo;import com.baidu.mapapi.search.MKPoiResult;import com.baidu.mapapi.search.MKRoute;import com.baidu.mapapi.search.MKSearch;import com.baidu.mapapi.search.MKSearchListener;import com.baidu.mapapi.search.MKShareUrlResult;import com.baidu.mapapi.search.MKSuggestionResult;import com.baidu.mapapi.search.MKTransitRouteResult;import com.baidu.mapapi.search.MKWalkingRouteResult;import com.baidu.platform.comapi.basestruct.GeoPoint;/** * 此demo用来展示如何进行公交线路详情检索,并使用RouteOverlay在地图上绘制 同时展示如何浏览路线节点并弹出泡泡 *  */public class BusLineSearchActivity extends Activity {// UI相关Button mBtnSearch = null;Button mBtnNextLine = null;// 浏览路线节点相关Button mBtnPre = null;// 上一个节点Button mBtnNext = null;// 下一个节点int nodeIndex = -2;// 节点索引,供浏览节点时使用MKRoute route = null;// 保存驾车/步行路线数据的变量,供浏览节点时使用private PopupOverlay pop = null;// 弹出泡泡图层,浏览节点时使用private TextView popupText = null;// 泡泡viewprivate View viewCache = null;private List<String> busLineIDList = null;int busLineIndex = 0;// 地图相关,使用继承MapView的MyBusLineMapView目的是重写touch事件实现泡泡处理// 如果不处理touch事件,则无需继承,直接使用MapView即可MapView mMapView = null; // 地图View// 搜索相关MKSearch mSearch = null; // 搜索模块,也可去掉地图模块独立使用protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);DemoApplication app = (DemoApplication) this.getApplication();setContentView(R.layout.activity_buslinesearch);CharSequence titleLable = "公交线路查询功能";setTitle(titleLable);// 地图初始化mMapView = (MapView) findViewById(R.id.bmapView);mMapView.getController().enableClick(true);mMapView.getController().setZoom(12);busLineIDList = new ArrayList<String>();// 创建 弹出泡泡图层createPaopao();// 地图点击事件处理mMapView.regMapTouchListner(new MKMapTouchListener() {@Overridepublic void onMapClick(GeoPoint point) {// 在此处理地图点击事件// 消隐popif (pop != null) {pop.hidePop();}}@Overridepublic void onMapDoubleClick(GeoPoint point) {}@Overridepublic void onMapLongClick(GeoPoint point) {}});// 初始化搜索模块,注册事件监听mSearch = new MKSearch();mSearch.init(app.mBMapManager, new MKSearchListener() {@Overridepublic void onGetPoiDetailSearchResult(int type, int error) {}public void onGetPoiResult(MKPoiResult res, int type, int error) {// 错误号可参考MKEvent中的定义if (error != 0 || res == null) {Toast.makeText(BusLineSearchActivity.this, "抱歉,未找到结果",Toast.LENGTH_SHORT).show();return;}// 找到公交路线poi nodeMKPoiInfo curPoi = null;int totalPoiNum = res.getCurrentNumPois();// 遍历所有poi,找到类型为公交线路的poibusLineIDList.clear();for (int idx = 0; idx < totalPoiNum; idx++) {if (2 == res.getPoi(idx).ePoiType) {// poi类型,0:普通点,1:公交站,2:公交线路,3:地铁站,4:地铁线路curPoi = res.getPoi(idx);// 使用poi的uid发起公交详情检索busLineIDList.add(curPoi.uid);System.out.println(curPoi.uid);}}SearchNextBusline();// 没有找到公交信息if (curPoi == null) {Toast.makeText(BusLineSearchActivity.this, "抱歉,未找到结果",Toast.LENGTH_LONG).show();return;}route = null;}public void onGetDrivingRouteResult(MKDrivingRouteResult res,int error) {}public void onGetTransitRouteResult(MKTransitRouteResult res,int error) {}public void onGetWalkingRouteResult(MKWalkingRouteResult res,int error) {}public void onGetAddrResult(MKAddrInfo res, int error) {}/** * 获取公交路线结果,展示公交线路 */public void onGetBusDetailResult(MKBusLineResult result, int iError) {if (iError != 0 || result == null) {Toast.makeText(BusLineSearchActivity.this, "抱歉,未找到结果",Toast.LENGTH_LONG).show();return;}RouteOverlay routeOverlay = new RouteOverlay(BusLineSearchActivity.this, mMapView);// 此处仅展示一个方案作为示例routeOverlay.setData(result.getBusRoute());// 清除其他图层mMapView.getOverlays().clear();// 添加路线图层mMapView.getOverlays().add(routeOverlay);// 刷新地图使生效mMapView.refresh();// 移动地图到起点mMapView.getController().animateTo(result.getBusRoute().getStart());// 将路线数据保存给全局变量route = result.getBusRoute();// 重置路线节点索引,节点浏览时使用nodeIndex = -1;mBtnPre.setVisibility(View.VISIBLE);mBtnNext.setVisibility(View.VISIBLE);Toast.makeText(BusLineSearchActivity.this, result.getBusName(),Toast.LENGTH_SHORT).show();}@Overridepublic void onGetSuggestionResult(MKSuggestionResult res, int arg1) {}@Overridepublic void onGetShareUrlResult(MKShareUrlResult result, int type,int error) {}});// 设定搜索按钮的响应mBtnSearch = (Button) findViewById(R.id.search);mBtnNextLine = (Button) findViewById(R.id.nextline);mBtnPre = (Button) findViewById(R.id.pre);mBtnNext = (Button) findViewById(R.id.next);mBtnPre.setVisibility(View.INVISIBLE);mBtnNext.setVisibility(View.INVISIBLE);OnClickListener clickListener = new OnClickListener() {public void onClick(View v) {// 发起搜索SearchButtonProcess(v);}};OnClickListener nextLineClickListener = new OnClickListener() {public void onClick(View v) {// 搜索下一条公交线SearchNextBusline();}};OnClickListener nodeClickListener = new OnClickListener() {public void onClick(View v) {// 浏览路线节点nodeClick(v);}};mBtnSearch.setOnClickListener(clickListener);mBtnNextLine.setOnClickListener(nextLineClickListener);mBtnPre.setOnClickListener(nodeClickListener);mBtnNext.setOnClickListener(nodeClickListener);}/** * 发起检索 *  * @param v */void SearchButtonProcess(View v) {busLineIDList.clear();busLineIndex = 0;mBtnPre.setVisibility(View.INVISIBLE);mBtnNext.setVisibility(View.INVISIBLE);if (mBtnSearch.equals(v)) {EditText editCity = (EditText) findViewById(R.id.city);EditText editSearchKey = (EditText) findViewById(R.id.searchkey);// 发起poi检索,从得到所有poi中找到公交线路类型的poi,再使用该poi的id进行公交详情搜索mSearch.poiSearchInCity(editCity.getText().toString(),editSearchKey.getText().toString());}}void SearchNextBusline() {if (busLineIndex >= busLineIDList.size()) {busLineIndex = 0;}if (busLineIndex >= 0 && busLineIndex < busLineIDList.size()&& busLineIDList.size() > 0) {mSearch.busLineSearch(((EditText) findViewById(R.id.city)).getText().toString(), busLineIDList.get(busLineIndex));busLineIndex++;}}/** * 创建弹出泡泡图层 */public void createPaopao() {viewCache = getLayoutInflater().inflate(R.layout.custom_text_view, null);popupText = (TextView) viewCache.findViewById(R.id.textcache);// 泡泡点击响应回调PopupClickListener popListener = new PopupClickListener() {@Overridepublic void onClickedPopup(int index) {Log.v("click", "clickapoapo");}};pop = new PopupOverlay(mMapView, popListener);}/** * 节点浏览示例 *  * @param v */public void nodeClick(View v) {if (nodeIndex < -1 || route == null || nodeIndex >= route.getNumSteps())return;viewCache = getLayoutInflater().inflate(R.layout.custom_text_view, null);popupText = (TextView) viewCache.findViewById(R.id.textcache);// 上一个节点if (mBtnPre.equals(v) && nodeIndex > 0) {// 索引减nodeIndex--;// 移动到指定索引的坐标mMapView.getController().animateTo(route.getStep(nodeIndex).getPoint());// 弹出泡泡popupText.setText(route.getStep(nodeIndex).getContent());popupText.setBackgroundResource(R.drawable.popup);pop.showPopup(BMapUtil.getBitmapFromView(popupText),route.getStep(nodeIndex).getPoint(), 5);}// 下一个节点if (mBtnNext.equals(v) && nodeIndex < (route.getNumSteps() - 1)) {// 索引加nodeIndex++;// 移动到指定索引的坐标mMapView.getController().animateTo(route.getStep(nodeIndex).getPoint());// 弹出泡泡popupText.setText(route.getStep(nodeIndex).getContent());popupText.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup));pop.showPopup(BMapUtil.getBitmapFromView(popupText),route.getStep(nodeIndex).getPoint(), 5);}}@Overrideprotected void onPause() {mMapView.onPause();super.onPause();}@Overrideprotected void onResume() {mMapView.onResume();super.onResume();}@Overrideprotected void onDestroy() {mMapView.destroy();super.onDestroy();}@Overrideprotected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);mMapView.onSaveInstanceState(outState);}@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {super.onRestoreInstanceState(savedInstanceState);mMapView.onRestoreInstanceState(savedInstanceState);}}

布局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:orientation="vertical" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="在" />        <EditText            android:id="@+id/city"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="北京" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="市内找" />        <EditText            android:id="@+id/searchkey"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="717" />        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="公交车" />        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="horizontal" >            <Button                android:id="@+id/search"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginLeft="2dip"                android:layout_marginRight="2dip"                android:layout_weight="1"                android:background="@drawable/button_style"                android:text="开始" />            <Button                android:id="@+id/nextline"                android:layout_width="match_parent"                android:layout_height="wrap_content"                android:layout_marginLeft="2dip"                android:layout_marginRight="2dip"                android:layout_weight="1"                android:background="@drawable/button_style"                android:text="下一条" />        </LinearLayout>    </LinearLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent" >        <com.baidu.mapapi.map.MapView            android:id="@+id/bmapView"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:clickable="true" />        <LinearLayout            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"            android:layout_alignWithParentIfMissing="false"            android:layout_centerHorizontal="true"            android:layout_centerVertical="false"            android:layout_marginBottom="10dip" >            <Button                android:id="@+id/pre"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_marginLeft="2dip"                android:layout_marginRight="2dip"                android:layout_weight="1.0"                android:background="@drawable/pre_" />            <Button                android:id="@+id/next"                android:layout_width="match_parent"                android:layout_height="match_parent"                android:layout_marginLeft="2dip"                android:layout_marginRight="2dip"                android:layout_weight="1.0"                android:background="@drawable/next_" />        </LinearLayout>    </RelativeLayout></LinearLayout>

配置文件和Application类同上文

附上图片效果:

原创粉丝点击