1.首先需要定义一个起点和一个终点
// 定义一个起始点和终点 private MKPlanNode start; private MKPlanNode end;
2.实例化地图查询类
// 实例化地图的查询类 mkSearch = new MKSearch(); mkSearch.init(bMapManager, new MySearchListener());
3.初始化起点和终点的坐标
// 给起始点和终点构建一个坐标 start = new MKPlanNode(); start.pt = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)); end = new MKPlanNode(); end.pt = new GeoPoint(40057031, 116307852);
4.驾车路线的优先策略
// 驾车路线考虑的优先策略 mkSearch.setDrivingPolicy(MKSearch.EBUS_TIME_FIRST);// 时间优先 mkSearch.drivingSearch(null, start, null, end);
5.驾车路线主要用到了onGetDrivingRouteResult()方法;
@Override public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) { // TODO Auto-generated method stub if (arg0 == null) { return; } RouteOverlay routeOverlay = new RouteOverlay(MainActivity.this, mapView); // 两点的驾车路线会有多条 routeOverlay.setData(arg0.getPlan(0).getRoute(0)); mapView.getOverlays().add(routeOverlay); mapView.invalidate(); }
6.运行效果
行走路线主要用到了onGetWalkingRouteResult()非法
@Override public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) { // TODO Auto-generated method stub if (arg0 == null) { return; } RouteOverlay routeOverlay = new RouteOverlay(MainActivity.this, mapView); // 两点的驾车路线会有多条,采用第一种方案 routeOverlay.setData(arg0.getPlan(0).getRoute(0)); mapView.getOverlays().add(routeOverlay); mapView.invalidate(); }
运行效果:
公交换乘线路搜索需要用到onGetTransitRouteResult()方法:
@Override public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) { // TODO Auto-generated method stub if (arg0 == null) { return; } RouteOverlay routeOverlay = new RouteOverlay(MainActivity.this, mapView); // 两点的驾车路线会有多条,采用第一种方案 routeOverlay.setData(arg0.getPlan(0).getRoute(0)); mapView.getOverlays().add(routeOverlay); mapView.invalidate(); }