百度地图之六标注行走路线搜索

如何标注行走路线搜索及RouteOverlay

在实际开发中我们需要标注最优行走路线

实战案例:从天安门到百度大厦行走路线

主要代码:

 
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.RouteOverlay;

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

public class MainActivity extends MapActivity {

    // 添加百度地图的相关控件
    private MapView mapView;
    // 加载地图的引擎
    private BMapManager bMapManager;
    // 百度地图的key
    private String KeyString = "8C426377B3526B6BF3672C9BA3BB544D1E8289CD";
    // 用于位置检索、周边检索、范围检索、公交检索、驾乘检索、步行检索等
    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(MainActivity.this);
        // 必须要加载key
        bMapManager.init(KeyString, new MKGeneralListener() {

            public void onGetPermissionState(int arg0) {
                
            }

            public void onGetNetworkState(int arg0) {
                if (arg0 == 300) {
                    Toast.makeText(MainActivity.this, "输入的key有错,请核实!!", 1).show();
                }
            }
        });
        this.initMapActivity(bMapManager);
        //实例化地图的查询类
        mkSearch = new MKSearch();
        mkSearch.init(bMapManager, new MySearchListener());
        //给起始点构建一个坐标
        start = new MKPlanNode();
        start.pt = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6));
        end = new MKPlanNode();
        end.pt = new GeoPoint(40057031,116307852);
        mkSearch.walkingSearch(null, start, null, end);
        

    }

    public class MySearchListener implements MKSearchListener {

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

        }

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

        }

        public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1) {
            // TODO Auto-generated method stub
        }

        public void onGetPoiResult(MKPoiResult result, int type, int iError) {
            // TODO Auto-generated method stub
        }

        public void onGetRGCShareUrlResult(String arg0, int arg1) {
            // TODO Auto-generated method stub

        }

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

        }

        public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1) {
            // TODO Auto-generated method stub

        }

        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();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bMapManager != null) {
            bMapManager.destroy();
            bMapManager = null;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (bMapManager != null) {
            bMapManager.start();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (bMapManager != null) {
            bMapManager.stop();
        }
    }

    @Override
    protected boolean isRouteDisplayed() {
        return false;
    }

}

 

 

 

百度地图之六标注行走路线搜索_第1张图片


你可能感兴趣的:(百度地图之六标注行走路线搜索)