Android学习笔记之百度地图(公交换乘路线搜索及TransitOverlay)
公交换乘路线搜索及TransitOverlay
这里只提供java代码,xml代码参照:Android学习笔记之初步学百度地图http://www.zzzyk.com/kf/201203/121875.html
重要代码:
[java] MKPlanNode start = new MKPlanNode();
// 起点:天安门
start.pt = new GeoPoint((int) (40.003834809598516 * 1E6),
(int) (116.3263213634491 * 1E6));
// 设置地图的中心
mapController.setCenter(start.pt);
MKPlanNode end = new MKPlanNode();
// 终点:鸟巢
end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6));
// 设置驾车路线搜索策略,时间优先、费用最少或距离最短
/*
* EBUS_TIME_FIRST
* public static final int EBUS_TIME_FIRST
* 公交检索策略常量:时间优先
* EBUS_TRANSFER_FIRST
* public static final int EBUS_TRANSFER_FIRST
* 公交检索策略常量:最少换乘
* EBUS_WALK_FIRST
* public static final int EBUS_WALK_FIRST
* 公交检索策略常量:最少步行距离
* EBUS_NO_SUBWAY
* public static final int EBUS_NO_SUBWAY
* 公交检索策略常量:不含地铁
*/
// 设置乘车路线搜索策略,时间优先、最少换乘、最少步行距离或不含地铁
mKSearch.setTransitPolicy(MKSearch.EBUS_TRANSFER_FIRST);
mKSearch.transitSearch("北京", start, end); // 必须设置城市名
MKPlanNode start = new MKPlanNode();
// 起点:天安门
start.pt = new GeoPoint((int) (40.003834809598516 * 1E6),
(int) (116.3263213634491 * 1E6));
// 设置地图的中心
mapController.setCenter(start.pt);
MKPlanNode end = new MKPlanNode();
// 终点:鸟巢
end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6));
// 设置驾车路线搜索策略,时间优先、费用最少或距离最短
/*
* EBUS_TIME_FIRST
* public static final int EBUS_TIME_FIRST
* 公交检索策略常量:时间优先
* EBUS_TRANSFER_FIRST
* public static final int EBUS_TRANSFER_FIRST
* 公交检索策略常量:最少换乘
* EBUS_WALK_FIRST
* public static final int EBUS_WALK_FIRST
* 公交检索策略常量:最少步行距离
* EBUS_NO_SUBWAY
* public static final int EBUS_NO_SUBWAY
* 公交检索策略常量:不含地铁
*/
// 设置乘车路线搜索策略,时间优先、最少换乘、最少步行距离或不含地铁
mKSearch.setTransitPolicy(MKSearch.EBUS_TRANSFER_FIRST);
mKSearch.transitSearch("北京", start, end); // 必须设置城市名
实现MySearchListener的onGetTransitRouteResult(MKTransitRouteResult, int),并展示检索结果:
[java] @Override
public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {
if (result == null) {
return;
}
TransitOverlay transitOverlay = new TransitOverlay(MyMapActivity.this, mMapView);
// 此处仅展示一个方案作为示例
transitOverlay.setData(result.getPlan(0));
mMapView.getOverlays().add(transitOverlay);
}
@Override
public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {
if (result == null) {
return;
}
TransitOverlay transitOverlay = new TransitOverlay(MyMapActivity.this, mMapView);
// 此处仅展示一个方案作为示例
transitOverlay.setData(result.getPlan(0));
mMapView.getOverlays().add(transitOverlay);
}
API:
transitSearch
public int transitSearch(java.lang.String city, MKPlanNode start, MKPlanNode end)
公交路线搜索.
异步函数,返回结果在MKSearchListener里的onGetTransitRouteResult方法通知
参数:
city - 城市名,用于在哪个城市内进行检索
start - 检索的起点,可通过关键字,坐标两种方式指定
end - 检索的终点,可通过关键字,坐标两种方式指定
返回:
成功返回0,否则返回-1
setTransitPolicy
public int setTransitPolicy(int policy)
设置路线规划策略.
参数为策略常量。对下次搜索有效
参数:
policy - EBUS_TIME_FIRST:时间优先;EBUS_TRANSFER_FIRST:少换乘;EBUS_WALK_FIRST:少步行;EBUS_NO_SUBWAY: 非地铁
返回:
成功返回0,否则返回-1
具体实现:
[java] view plaincopyprint? package xiaosi.baiduMap;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.PoiOverlay;
import com.baidu.mapapi.RouteOverlay;
import com.baidu.mapapi.TransitOverlay;
public class BaiduMapActivity extends MapActivity
{
/** Called when the activity is first created. */
private BMapManager mapManager = null;
private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";
private MapView mapView = null;
private MKSearch mKSearch;
private MapController mapController = null;
@Override
public void onCreate(Bundle savedInstanceState)
&补充:移动开发 , Android ,