已解决:百度地图的查询驾车路线,集成到应用里面无法画出路线,无法得到MKSearchListener的结果


百度地图的查询驾车路线,集成到应用里面无法画出路线(跳转进入这个Activity发现路线无法找到,也就是无法得到MKSearchListener的结果),应用未出现崩溃,log日志是正常的,不知道原因。先保存下。

解决方法:开个子线程来装,休眠1秒后再进行数据的搜索和获取。即可解决该问题。

package huaxa.it.map.demo.route;

import huaxa.it.map.BaseActivity;
import huaxa.it.map.BaseAdapter;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.baidu.mapapi.map.RouteOverlay;
import com.baidu.mapapi.search.MKDrivingRouteResult;
import com.baidu.mapapi.search.MKPlanNode;
import com.baidu.mapapi.search.MKSearch;
import com.baidu.mapapi.search.MKWpNode;
import com.baidu.platform.comapi.basestruct.GeoPoint;

/**
 * @项目名: BaiduMap
 * @包名: huaxa.it.map.demo.route
 * @类名: LeastTime_Driving
 * @创建者: 黄夏莲
 * @创建时间: 2016年12月12日 ,下午2:11:42
 * 
 * @描述: TODO
 */
public class DrivingRouteLeastTime extends BaseActivity
{
	private MySearchListener	listener;
	private int[]				startPt;
	private int[]				endPt;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		new Thread(new Runnable()
		{

			@Override
			public void run()
			{
				try
				{
					Thread.sleep(1000);
					// 获取起点终点的数据
					getIntentData();
					search();
				} catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		})
		{

		}.start();
		;
	}

	// 获取起点终点的数据
	private void getIntentData()
	{
		startPt = new int[2];
		endPt = new int[2];
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		startPt = bundle.getIntArray("startpt");
		endPt = bundle.getIntArray("endpt");
		Log.i("TAG", "跳转到路线页面:startpt[0]=" + startPt[0] + "endpt[0]" + endPt[0]);
	}

	private void search()
	{
		search = new MKSearch();
		listener = new MySearchListener();
		search.init(manager, listener);
		// ********驾车路线规划策略************
		// setDrivingPolicy
		// public int setDrivingPolicy(int policy)
		// 设置驾车路线规划策略. 参数为策略常量。对下次搜索有效
		// 参数:
		// policy - ECAR_TIME_FIRST:时间优先;ECAR_DIS_FIRST:距离最短;ECAR_FEE_FIRST:费用最少
		// 返回:
		// 成功返回0,否则返回-1
		search.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
		// **********驾乘路线搜索***************
		// public int drivingSearch(String startCity,MKPlanNode start,String
		// endCity,MKPlanNode end)
		// 驾乘路线搜索.
		// 异步函数,返回结果在MKSearchListener里的onGetDrivingRouteResult方法通知
		// 参数:
		// startCity - 起点所在城市,起点为坐标时可不填
		// start - 搜索的起点,可以为坐标,名称任一种
		// endCity - 终点所在城市,终点为坐标时可不填
		// end - 搜索的终点,可以为坐标,名称任一种
		// 返回:
		// 成功返回0,否则返回-1
		// 补充:MKPlanNode的属性
		// 1、String name结点名称
		// 2、GeoPoint pt结点坐标
		MKPlanNode start = new MKPlanNode();
		// start.pt = geoPoint;
		start.pt = new GeoPoint(startPt[0], startPt[1]);
		MKPlanNode end = new MKPlanNode();
		end.name = "广州塔";
		end.pt = new GeoPoint(endPt[0], endPt[1]);

		search.drivingSearch("广州", start, "广州", end);

		// public int drivingSearch(String startCity,
		// MKPlanNode start,
		// String endCity,
		// MKPlanNode end,
		// ArrayList<MKWpNode> wpNodes)
		// 驾车路线搜索,可设置途经点
		// 异步函数,返回结果在MKSearchListener里的onGetDrivingRouteResult方法通知
		//
		// 参数:
		// startCity - 起点所在城市,起点为坐标时可不填
		// start - 搜索的起点,可以为坐标,名称任一种
		// endCity - 终点所在城市,终点为坐标时可不填
		// end - 搜索的终点,可以为坐标,名称任一种
		// wpNodes - 途经点数据
		// 返回:
		// 成功返回0,否则返回-1
		// ArrayList<MKWpNode> wpNodes = new ArrayList<MKWpNode>();
		// MKWpNode node = new MKWpNode();
		// node.city = "广州";
		// node.name = "中山大学";
		// wpNodes.add(node);

		// search.drivingSearch("", start, "", end, wpNodes);
		Log.i("TAG", "开始查询路线");

	}

	private class MySearchListener extends BaseAdapter
	{
		@Override
		public void onGetDrivingRouteResult(MKDrivingRouteResult result,
				int iError)
		{
			Log.i("TAG", "进入搜索结果");
			// MKDrivingRouteResult() getAddrResult() 返回起点或终点的地址信息结果,
			// MKPlanNode getEnd() 获取路线终站,
			// int getNumPlan() 获取方案数目,
			// MKRoutePlan getPlan(int index) 获取第index个方案 ,
			// MKPlanNode getStart() 获取路线起点

			if (result != null && iError == 0)
			{
				// 结果处理
				RouteOverlay overlay = new RouteOverlay(
						DrivingRouteLeastTime.this, mapView);
				// 获得路线信息
				if (result.getNumPlan() > 0)
				{
					Log.i("TAG", "路线方案数量" + result.getNumPlan());
					overlay.setData(result.getPlan(0).getRoute(0));
					mapView.getOverlays().add(overlay);
					mapView.refresh();
				}

			} else
			{
				Toast.makeText(getApplicationContext(), "没有搜索到结果",
						Toast.LENGTH_LONG).show();
			}
			super.onGetDrivingRouteResult(result, iError);
		}
	}

	@Override
	protected void onResume()
	{
		// TODO Auto-generated method stub
		super.onResume();
		mapView.onResume();
	}

	@Override
	protected void onPause()
	{
		// TODO Auto-generated method stub
		super.onPause();
		mapView.onPause();
	}

	@Override
	protected void onDestroy()
	{
		// TODO Auto-generated method stub
		super.onDestroy();
		mapView.destroy();
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值