百度地图--路线(步行)

  Button mBtnPre = null; // 上一个节点
    Button mBtnNext = null; // 下一个节点
    int nodeIndex = -1; // 节点索引,供浏览节点时使用
    RouteLine route = null;
    WalkingRouteOverlay routeOverlay = null;
    boolean useDefaultIcon = false;
    private TextView popupText = null; // 泡泡view

    // 地图相关,使用继承MapViewMyRouteMapView目的是重写touch事件实现泡泡处理
    // 如果不处理touch事件,则无需继承,直接使用MapView即可
    MapView mMapView = null;    // 地图View
    BaiduMap mBaidumap = null;
    // 搜索相关
    RoutePlanSearch mSearch = null;    // 搜索模块,也可去掉地图模块独立使用

    TransitRouteResult nowResult = null;
    DrivingRouteResult nowResultd = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            SDKInitializer.initialize(getApplicationContext());
        } catch (Exception e) {
            // TODO: handle exception
            ToastUtils.shortToast(getApplicationContext(), "地图api初始化失败");
            return;
        }
        setContentView(R.layout.activity_lu_xian_map);
        initSystemBar(this);
        initTitle();
//        // 初始化地图
        mMapView = (MapView) findViewById(R.id.map);
        mBaidumap = mMapView.getMap();
        mBtnPre = (Button) findViewById(R.id.pre);
        mBtnNext = (Button) findViewById(R.id.next);
        mBtnPre.setVisibility(View.INVISIBLE);
        mBtnNext.setVisibility(View.INVISIBLE);
        // 地图点击事件处理
        mBaidumap.setOnMapClickListener(this);
        // 初始化搜索模块,注册事件监听
//        mSearch = RoutePlanSearch.newInstance();
//        mSearch.setOnGetRoutePlanResultListener(this);
//        searchButtonProcess();

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.currentThread().sleep(5000);//阻断2                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        // 天安门坐标
        double mLat1 = 39.915291;
        double mLon1 = 116.403857;
        // 百度大厦坐标
        double mLat2 = 40.056858;
        double mLon2 = 116.308194;
        LatLng loc_start = new LatLng(mLat1, mLon1);
        LatLng loc_end = new LatLng(mLat2, mLon2);
        mSearch = RoutePlanSearch.newInstance();
        mSearch.setOnGetRoutePlanResultListener(this);

        PlanNode stNode = PlanNode.withLocation(loc_start);
        PlanNode enNode = PlanNode.withLocation(loc_end);
        mSearch.walkingSearch((new WalkingRoutePlanOption()).from(stNode).to(enNode));
    }
/**
 * 节点浏览示例
 *
 * @param v
 */
public void nodeClick(View v) {
    if (route == null || route.getAllStep() == null) {
        return;
    }
    if (nodeIndex == -1 && v.getId() == R.id.pre) {
        return;
    }
    // 设置节点索引
    if (v.getId() == R.id.next) {
        if (nodeIndex < route.getAllStep().size() - 1) {
            nodeIndex++;
        } else {
            return;
        }
    } else if (v.getId() == R.id.pre) {
        if (nodeIndex > 0) {
            nodeIndex--;
        } else {
            return;
        }
    }
    // 获取节结果信息
    LatLng nodeLocation = null;
    String nodeTitle = null;
    Object step = route.getAllStep().get(nodeIndex);
    if (step instanceof DrivingRouteLine.DrivingStep) {
        nodeLocation = ((DrivingRouteLine.DrivingStep) step).getEntrance().getLocation();
        nodeTitle = ((DrivingRouteLine.DrivingStep) step).getInstructions();
    } else if (step instanceof WalkingRouteLine.WalkingStep) {
        nodeLocation = ((WalkingRouteLine.WalkingStep) step).getEntrance().getLocation();
        nodeTitle = ((WalkingRouteLine.WalkingStep) step).getInstructions();
    } else if (step instanceof TransitRouteLine.TransitStep) {
        nodeLocation = ((TransitRouteLine.TransitStep) step).getEntrance().getLocation();
        nodeTitle = ((TransitRouteLine.TransitStep) step).getInstructions();
    } else if (step instanceof BikingRouteLine.BikingStep) {
        nodeLocation = ((BikingRouteLine.BikingStep) step).getEntrance().getLocation();
        nodeTitle = ((BikingRouteLine.BikingStep) step).getInstructions();
    }

    if (nodeLocation == null || nodeTitle == null) {
        return;
    }
    // 移动节点至中心
    mBaidumap.setMapStatus(MapStatusUpdateFactory.newLatLng(nodeLocation));
    // show popup
    popupText = new TextView(LuXianMapActivity.this);
    popupText.setBackgroundResource(R.drawable.popup);
    popupText.setTextColor(0xFF000000);
    popupText.setText(nodeTitle);
    mBaidumap.showInfoWindow(new InfoWindow(popupText, nodeLocation, 0));

}
 
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
}

@Override
public void onGetWalkingRouteResult(WalkingRouteResult result) {
    if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
        Toast.makeText(LuXianMapActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show();
    }
    if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
        // 起终点或途经点地址有岐义,通过以下接口获取建议查询信息
        // result.getSuggestAddrInfo()
        return;
    }
    if (result.error == SearchResult.ERRORNO.NO_ERROR) {
        nodeIndex = -1;
        mBtnPre.setVisibility(View.VISIBLE);
        mBtnNext.setVisibility(View.VISIBLE);
        route = result.getRouteLines().get(0);
        WalkingRouteOverlay overlay = new MyWalkingRouteOverlay(mBaidumap);
        mBaidumap.setOnMarkerClickListener(overlay);
        routeOverlay = overlay;
        overlay.setData(result.getRouteLines().get(0));
        overlay.addToMap();
        overlay.zoomToSpan();
    }

}
private class MyWalkingRouteOverlay extends WalkingRouteOverlay {

    public MyWalkingRouteOverlay(BaiduMap baiduMap) {
        super(baiduMap);
    }

    @Override
    public BitmapDescriptor getStartMarker() {
        if (useDefaultIcon) {
            return BitmapDescriptorFactory.fromResource(R.drawable.icon_st);
        }
        return null;
    }

    @Override
    public BitmapDescriptor getTerminalMarker() {
        if (useDefaultIcon) {
            return BitmapDescriptorFactory.fromResource(R.drawable.icon_en);
        }
        return null;
    }
}
@Override
public void onMapClick(LatLng point) {
    mBaidumap.hideInfoWindow();
}

@Override
public boolean onMapPoiClick(MapPoi poi) {
    return false;
}

@Override
protected void onPause() {
    mMapView.onPause();
    super.onPause();
}

@Override
protected void onResume() {
    mMapView.onResume();
    super.onResume();
}

@Override
protected void onDestroy() {
    mSearch.destroy();
    mMapView.onDestroy();
    super.onDestroy();
}

// 响应DLg中的List item 点击
interface OnItemInDlgClickListener {
    public void onItemClick(int position);
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值