百度地图开发第六天(一个地图上显示多条导航路线)

来源:互联网 发布:windows 微软雅黑字体 编辑:程序博客网 时间:2024/06/05 06:49

 

源代码:

package com.android.mybaidu;

import android.os.Bundle;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKBusLineResult;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKGeneralListener;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
import com.baidu.mapapi.MKSuggestionResult;
import com.baidu.mapapi.MKTransitRouteResult;
import com.baidu.mapapi.MKWalkingRouteResult;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.TransitOverlay;

public class Main extends MapActivity {
 /** Called when the activity is first created. */

 // 添加百度地图的相关控件
 private MapView mapView;
 private BMapManager bMapManager;// 加载地图的引擎
 // 百度地图的key
 private String keyString = "A270F85CD72A01E8519A9677A75FB4016ED9A5A3" ;
 private MKSearch mkSearch;//用于检索的类
 //定义一个起始点和终点。
 private MKPlanNode start;
 private MKPlanNode end;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  mapView = (MapView) this.findViewById(R.id.bmapView);
  bMapManager = new BMapManager(Main.this);
  // 必须要加载key
  bMapManager.init(keyString, new MKGeneralListener() {

   @Override
   public void onGetPermissionState(int arg0) {
    // TODO Auto-generated method stub
    if (arg0 == 300) {
     Toast.makeText(Main.this, "输入的Key有错!请核实!!", 1).show();
    }
   }

   @Override
   public void onGetNetworkState(int arg0) {
    // TODO Auto-generated method stub

   }
  });
   /*initMapActivity(arg0)是MapActivity的一个方法,
         * MapActivity管理显示MapView中Activity 处理逻辑的基础类,
         * initMapActivity初始化这个类
        */
  this.initMapActivity(bMapManager);
  mkSearch=new MKSearch();
  mkSearch.init(bMapManager, new MySearchListener());
  start=new MKPlanNode();
  start.pt= new GeoPoint((int) (39.244 * 1E6),
    (int) (117.052 * 1E6));
   mapView.getController().setCenter(new GeoPoint((int) (39.244 * 1E6),
     (int) (117.052 * 1E6)));
   end=new MKPlanNode();
   end.pt=new GeoPoint((int) (41.244 * 1E6),
     (int) (118.052 * 1E6));
  mkSearch.transitSearch("天津", start, end);
 }
 public class MySearchListener implements MKSearchListener{

  @Override
  public void onGetAddrResult(MKAddrInfo arg0, int arg1) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onGetBusDetailResult(MKBusLineResult arg0, int arg1) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {

 

  }

  @Override
  public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onGetSuggestionResult(MKSuggestionResult arg0, int arg1) {
   // TODO Auto-generated method stub
   
  }

  @Override
  public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
   // 公交路线查询方法   
   /*RouteOverlay说明:
    * 使用SDK内置图标及展现方式展示路线信息
                                          通过setdata接口设置路线数据
                */
   if(arg0==null){
    return;
   }
   int planNum=arg0.getNumPlan();//获得所有的驾车路线图
   //下面把所有路线都在地图上显示出来
   for(int i=0;i<planNum;i++){
    TransitOverlay overlay=new TransitOverlay(Main.this, mapView);
    overlay.setData(arg0.getPlan(i));
   }
   mapView.invalidate();//刷新地图
   mapView.getController().animateTo(arg0.getStart().pt);
  }
   
  @Override
  public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1) {
  
 }
 }


 @Override
 protected void onDestroy() {
  // TODO Auto-generated method stub
  super.onDestroy();
  if (bMapManager != null) {
   bMapManager.destroy();
   bMapManager = null;
  }
 }

 @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  if (bMapManager != null) {
   bMapManager.start();
  }
 }

 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  if (bMapManager != null) {
   bMapManager.stop();
  }
 }

 @Override
 protected boolean isRouteDisplayed() {
  // TODO Auto-generated method stub
  return false;
 }
}

0 0
原创粉丝点击