android笔记---百度地图api应用 (二) 获取公交路线的详细信息

package com.example.bdtest;


import com.baidu.mapapi.MKEvent;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiInfo;
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.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSuggestionResult;
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 android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends MapActivity {

    private BMapManager mBMapMan = null;
    private MapView mMapView = null;
    private MKSearch mMKSearch = null;
    private String mCityName = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBMapMan=new BMapManager(getApplication());
        mBMapMan.init("3B910277809EF23A5FAFAB66F09168D73CB3B793", null);  
        //注意:请在试用setContentView前初始化BMapManager对象,否则会报错
        setContentView(R.layout.activity_main);
        super.initMapActivity(mBMapMan);
        mMKSearch = new MKSearch();
        mMKSearch.init(mBMapMan, new MySearchListener(){

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

            public void onGetBusDetailResult(MKBusLineResult result, int iError) {
                   if (iError != 0 || result == null) {
                           Toast.makeText(MainActivity.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
                           return;
                   }
                   RouteOverlay routeOverlay = new RouteOverlay(MainActivity.this, mMapView);    // 此处仅展示一个方案作为示例
                   routeOverlay.setData(result.getBusRoute());
                   mMapView.getOverlays().clear();
                   mMapView.getOverlays().add(routeOverlay);
                   mMapView.refreshDrawableState();
                   mMapView.getController().animateTo(result.getBusRoute().getStart());
            }

            @Override
            public void onGetDrivingRouteResult(MKDrivingRouteResult result, int iError) {
                if (result == null) {
                        return;
                }
                RouteOverlay routeOverlay = new RouteOverlay(MainActivity.this, mMapView);  // 此处仅展示一个方案作为示例
                routeOverlay.setData(result.getPlan(0).getRoute(0));
                mMapView.getOverlays().add(routeOverlay);
                mMapView.refreshDrawableState();
            }


            @Override
            public void onGetPoiDetailSearchResult(int arg0, int arg1) {
                // TODO Auto-generated method stub
                super.onGetPoiDetailSearchResult(arg0, arg1);
            }


            @Override
            public void onGetPoiResult(MKPoiResult res, int type, int error) {
                   // 错误号可参考MKEvent中的定义
                   if (error != 0 || res == null) {
                           Toast.makeText(MainActivity.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
                           return;
                   }
                   // 找到公交路线poi node 
                   MKPoiInfo curPoi = null;
                   int totalPoiNum  = res.getNumPois();
                   for(int idx = 0; idx<totalPoiNum; idx++ ) {
                           curPoi = res.getPoi(idx); 
                           if ( 2 == curPoi.ePoiType ) {
                                   break; 
                           }
                   }
                   mMKSearch.busLineSearch(mCityName, curPoi.uid);
            }


            @Override
            public void onGetRGCShareUrlResult(String arg0, int arg1) {
                // TODO Auto-generated method stub
                super.onGetRGCShareUrlResult(arg0, arg1);
            }

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

            @Override
            public void onGetTransitRouteResult(MKTransitRouteResult arg0,
                    int arg1) {
                // TODO Auto-generated method stub
                super.onGetTransitRouteResult(arg0, arg1);
            }

            @Override
            public void onGetWalkingRouteResult(MKWalkingRouteResult arg0,
                    int arg1) {
                // TODO Auto-generated method stub
                super.onGetWalkingRouteResult(arg0, arg1);
            }

            @Override
            protected Object clone() throws CloneNotSupportedException {
                // TODO Auto-generated method stub
                return super.clone();
            }

            @Override
            public boolean equals(Object o) {
                // TODO Auto-generated method stub
                return super.equals(o);
            }

            @Override
            protected void finalize() throws Throwable {
                // TODO Auto-generated method stub
                super.finalize();
            }

            @Override
            public int hashCode() {
                // TODO Auto-generated method stub
                return super.hashCode();
            }

            @Override
            public String toString() {
                // TODO Auto-generated method stub
                return super.toString();
            }
            
        });//注意,MKSearchListener只支持一个,以最后一次设置为准
        
        
        mMapView=(MapView)findViewById(R.id.bmapView);
        mMapView.setBuiltInZoomControls(true);
        //设置启用内置的缩放控件
        MapController mMapController=mMapView.getController();
        // 得到mMapView的控制权,可以用它控制和驱动平移和缩放
        GeoPoint point =new GeoPoint((int)(39.915* 1E6),(int)(116.404* 1E6));
        //用给定的经纬度构造一个GeoPoint,单位是微度 (度 * 1E6)
        mMapController.setCenter(point);//设置地图中心点
        mMapController.setZoom(12);//设置地图zoom级别

        //显示交通信息图
        //mMapView.setTraffic(true);
        
        //显示卫星图
        //mMapView.setSatellite(true); 
        
        //检索北京西站与北京北站为顶点所确定的距形区域内的KFC餐厅
//        // 北京西站
//        GeoPoint ptLB = new GeoPoint( (int)(39.901375 * 1E6),(int)(116.329099 * 1E6)); 
//        // 北京北站
//        GeoPoint ptRT = new GeoPoint( (int)(39.949404 * 1E6),(int)(116.360719 * 1E6));
//        mMKSearch.poiSearchInbounds("KFC", ptLB, ptRT);
        
        
        //检索从天安门到百度大厦的驾车路线
//        MKPlanNode start = new MKPlanNode();
//        start.pt = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6));
//        MKPlanNode end = new MKPlanNode();
//        end.pt = new GeoPoint(40057031, 116307852);// 设置驾车路线搜索策略,时间优先、费用最少或距离最短
//        mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
//        mMKSearch.drivingSearch(null, start, null, end);

        //获取公交路线的详细信息
        mCityName = "北京";
        mMKSearch.poiSearchInCity(mCityName, "717");

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    protected void onDestroy(){
            if(mBMapMan!=null){
                    mBMapMan.destroy();
                    mBMapMan=null;
            }
            super.onDestroy();
    }
    @Override
    protected void onPause(){
            if(mBMapMan!=null){
                   mBMapMan.stop();
            }
            super.onPause();
    }
    @Override
    protected void onResume(){
            if(mBMapMan!=null){
                    mBMapMan.start();
            }
           super.onResume();
    }


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





    

}

 

你可能感兴趣的:(android)