赞
踩
目前百度地图SDK所集成的检索服务包括:POI检索、公交信息查询、线路规划、地理编码、在线建议查询、短串分享。
上篇博客讲解了POI检索和在线建议查询,这篇博客将讲解经常用到的线路规划。
在讲解代码之前先上张效果图:
好了!现在我们上代码,来实现上面的功能(代码中都做了相应的注解)
路线规划检索有三种检索:驾车,步行,公交车!三种实现的步骤基本类似,下面我们就拿一种来做解析(公交车)。
1.首先我们要实例化路线规划检索的实例
- // 初始化搜索模块,注册事件监听
- mSearch = RoutePlanSearch.newInstance();
- mSearch.setOnGetRoutePlanResultListener(this);
2. 创建公交线路规划检索监听者
- @Override
- public void onGetTransitRouteResult(TransitRouteResult result) {
- if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
- Toast.makeText(MainActivity.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;
- pre_next_id.setVisibility(View.VISIBLE);
- route = result.getRouteLines().get(0);
- TransitRouteOverlay overlay = new MyTransitRouteOverlay(mBaiduMap);
- mBaiduMap.setOnMarkerClickListener(overlay);
- routeOverlay = overlay;
- overlay.setData(result.getRouteLines().get(0));
- overlay.addToMap();
- overlay.zoomToSpan();
- }
-
- }
3.检索起、终点信息
- /**
- * 发起路线规划搜索示例
- *
- * @param v
- */
- public void Search_RoutePlan_Process(View v) {
- // 重置浏览节点的路线数据
- route = null;
- pre_next_id.setVisibility(View.INVISIBLE);
- mBaiduMap.clear();
- // 处理搜索按钮响应
- EditText editSt = (EditText) findViewById(R.id.start);
- EditText editEn = (EditText) findViewById(R.id.end);
- // 设置起终点信息,对于tranist search 来说,城市名无意义
- PlanNode stNode = PlanNode.withCityNameAndPlaceName("北京", editSt
- .getText().toString());
- PlanNode enNode = PlanNode.withCityNameAndPlaceName("北京", editEn
- .getText().toString());
-
- // 实际使用中请对起点终点城市进行正确的设定
- if (v.getId() == R.id.drive) {
- mSearch.drivingSearch((new DrivingRoutePlanOption()).from(stNode)
- .to(enNode));
- } else if (v.getId() == R.id.transit) {
- mSearch.transitSearch((new TransitRoutePlanOption()).from(stNode)
- .city("北京").to(enNode));
- } else if (v.getId() == R.id.walk) {
- mSearch.walkingSearch((new WalkingRoutePlanOption()).from(stNode)
- .to(enNode));
- }
- }
4.节点浏览的实现(就是上一站和下一站)
- /**
- * 节点浏览示例
- *
- * @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).getEntrace()
- .getLocation();
- nodeTitle = ((DrivingRouteLine.DrivingStep) step).getInstructions();
- } else if (step instanceof WalkingRouteLine.WalkingStep) {
- nodeLocation = ((WalkingRouteLine.WalkingStep) step).getEntrace()
- .getLocation();
- nodeTitle = ((WalkingRouteLine.WalkingStep) step).getInstructions();
- } else if (step instanceof TransitRouteLine.TransitStep) {
- nodeLocation = ((TransitRouteLine.TransitStep) step).getEntrace()
- .getLocation();
- nodeTitle = ((TransitRouteLine.TransitStep) step).getInstructions();
- }
-
- if (nodeLocation == null || nodeTitle == null) {
- return;
- }
- // 移动节点至中心
- mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(nodeLocation));
- // show popup
- popupText = new TextView(MainActivity.this);
- popupText.setBackgroundResource(R.drawable.popup);
- popupText.setTextColor(0xFF000000);
- popupText.setText(nodeTitle);
- mBaiduMap.showInfoWindow(new InfoWindow(popupText, nodeLocation, 0));
-
- }
5.获取开始和结束坐标
- private class MyTransitRouteOverlay extends TransitRouteOverlay {
-
- public MyTransitRouteOverlay(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;
- }
- }
6.释放检索实例;
mSearch.destroy();
好了!关于公交车路线检索讲解完啦!驾车和步行的和公交车路线的差不多,想了解的可以下载源码查看!
本篇博客大家觉得有什么新的想法和建议可以在评论区评论,谢谢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。