Android学习笔记之百度地图(POI搜索之城市poi检索poiSearchInCity)

POI搜索有三种方式,根据范围和检索词发起范围检索poiSearchInbounds,城市poi检索poiSearchInCity,周边检索poiSearchNearBy。

public int poiSearchInCity(java.lang.String city, java.lang.String key)

城市内poi检索. 

异步函数,返回结果在MKSearchListener里的onGetPoiResult方法通知
参数:
city - 城市名,如果设置为"",则将在MapView所在的当前城市内进行搜索
key - 关键词

返回:

成功返回0,否则返回-1


Demo: 检索天安门周边5000米之内的KFC餐厅

  1. mKSearch.poiSearchInCity("黄岛""KTV")  
mKSearch.poiSearchInCity("黄岛", "KTV")




  1. package xiaosi.baiduMap;  
  2.   
  3. import android.app.AlertDialog;  
  4. import android.content.DialogInterface;  
  5. import android.os.Bundle;  
  6.   
  7. import com.baidu.mapapi.BMapManager;  
  8. import com.baidu.mapapi.GeoPoint;  
  9. import com.baidu.mapapi.MKAddrInfo;  
  10. import com.baidu.mapapi.MKDrivingRouteResult;  
  11. import com.baidu.mapapi.MKPoiInfo;  
  12. import com.baidu.mapapi.MKPoiResult;  
  13. import com.baidu.mapapi.MKSearch;  
  14. import com.baidu.mapapi.MKSearchListener;  
  15. import com.baidu.mapapi.MKTransitRouteResult;  
  16. import com.baidu.mapapi.MKWalkingRouteResult;  
  17. import com.baidu.mapapi.MapActivity;  
  18. import com.baidu.mapapi.MapController;  
  19. import com.baidu.mapapi.MapView;  
  20. import com.baidu.mapapi.PoiOverlay;  
  21.   
  22. public class BaiduMapActivity extends MapActivity  
  23. {  
  24.     /** Called when the activity is first created. */  
  25.     private BMapManager mapManager = null;  
  26.     private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";  
  27.     private MapView mapView = null;  
  28.     private MKSearch mKSearch;  
  29.     private MapController mapController = null;  
  30.   
  31.     @Override  
  32.     public void onCreate(Bundle savedInstanceState)  
  33.     {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.main);  
  36.         mapManager = new BMapManager(getApplication());  
  37.         mapManager.init(key, null);  
  38.         super.initMapActivity(mapManager);  
  39.         mapView = (MapView) findViewById(R.id.mapView);  
  40.         // 设置启用内置的缩放控件   
  41.         mapView.setBuiltInZoomControls(true);   
  42.         // 得到mMapView的控制权,可以用它控制和驱动平移和缩放   
  43.         mapController = mapView.getController();   
  44.         // 设置地图zoom级别   
  45.         mapController.setZoom(12);   
  46.           
  47.         mKSearch = new MKSearch();  
  48.         mKSearch.init(mapManager, new MySearchListener());// 注意,MKSearchListener只支持一个,以最后一次设置为准   
  49.         //搜索山东科技大学(36.001618315221194,120.11934041976929)附近(5000)的KTV   
  50.         if (mKSearch.poiSearchInCity("黄岛""KTV") == -1)  
  51.         {  
  52.             System.out.println("失败");  
  53.         }  
  54.         else  
  55.         {  
  56.             System.out.println("成功");  
  57.         }  
  58.     }  
  59.   
  60.     public class MySearchListener implements MKSearchListener  
  61.     {  
  62.         public void onGetAddrResult(MKAddrInfo arg0, int arg1)  
  63.         {  
  64.             /* 
  65.              * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息 
  66.              */  
  67.         }  
  68.   
  69.         public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)  
  70.         {  
  71.             /* 
  72.              * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回 
  73.              */  
  74.         }  
  75.   
  76.         public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)  
  77.         {  
  78.             String result = "";  
  79.             /* 
  80.              * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回 
  81.              */  
  82.             if (arg0 == null)  
  83.             {  
  84.                 return;  
  85.             }  
  86.             // 清除地图上已有的所有覆盖物   
  87.             // mapView.getOverlays().clear();   
  88.             // PoiOverlay是baidu map api提供的用于显示POI的Overlay   
  89.             PoiOverlay poioverlay = new PoiOverlay(BaiduMapActivity.this,  
  90.                     mapView);  
  91.             // 在地图上显示PoiOverlay(将搜索到的兴趣点标注在地图上)   
  92.             poioverlay.setData(arg0.getAllPoi());  
  93.             // 为地图添加覆盖物   
  94.             mapView.getOverlays().add(poioverlay);  
  95.             //刚开始忘记加这几句代码,地图一直没改变,纠结了很长时间   
  96.             if (arg0.getNumPois() > 0)  
  97.             {  
  98.                 // 设置其中一个搜索结果所在地理坐标为地图的中心   
  99.                 MKPoiInfo poiInfo = arg0.getPoi(0);  
  100.                 mapController.setCenter(poiInfo.pt);  
  101.             }  
  102.               
  103.             // 遍历当前页返回的搜索结果(默认只返回10个)    
  104.             for (MKPoiInfo poiInfo : arg0.getAllPoi())  
  105.             {  
  106.                 result = result + "\n"+  "名称:" + poiInfo.name + "\n" + "地址:" + poiInfo.address + "\n" + "城市:" + poiInfo.city;  
  107.             }  
  108.               
  109.               
  110.             //用AlertDialog来显示搜索到的内容   
  111.             AlertDialog.Builder builder = new AlertDialog.Builder(BaiduMapActivity.this);     
  112.             builder.setTitle("搜索结果");    
  113.             builder.setMessage(result);    
  114.             builder.setPositiveButton("关闭"new android.content.DialogInterface.OnClickListener(){    
  115.         
  116.                 public void onClick(DialogInterface dialog, int which) {    
  117.                     dialog.dismiss();  
  118.                 }    
  119.             });    
  120.             builder.show();  
  121.               
  122.         }  
  123.   
  124.         public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)  
  125.         {  
  126.             /* 
  127.              * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息 
  128.              */  
  129.         }  
  130.   
  131.         public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)  
  132.         {  
  133.             /* 
  134.              * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回 
  135.              */  
  136.         }  
  137.     }  
  138.   
  139.     @Override  
  140.     protected boolean isRouteDisplayed()  
  141.     {  
  142.         return false;  
  143.     }  
  144.   
  145.     @Override  
  146.     protected void onDestroy()  
  147.     {  
  148.         if (mapManager != null)  
  149.         {  
  150.             mapManager.destroy();  
  151.             mapManager = null;  
  152.         }  
  153.         super.onDestroy();  
  154.     }  
  155.   
  156.     @Override  
  157.     protected void onPause()  
  158.     {  
  159.         if (mapManager != null)  
  160.         {  
  161.             mapManager.stop();  
  162.         }  
  163.         super.onPause();  
  164.     }  
  165.   
  166.     @Override  
  167.     protected void onResume()  
  168.     {  
  169.         if (mapManager != null)  
  170.         {  
  171.             mapManager.start();  
  172.         }  
  173.         super.onResume();  
  174.     }  
  175. }  
package xiaosi.baiduMap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
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;

public class BaiduMapActivity extends MapActivity
{
	/** Called when the activity is first created. */
	private BMapManager mapManager = null;
	private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";
	private MapView mapView = null;
	private MKSearch mKSearch;
	private MapController mapController = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mapManager = new BMapManager(getApplication());
		mapManager.init(key, null);
		super.initMapActivity(mapManager);
		mapView = (MapView) findViewById(R.id.mapView);
		// 设置启用内置的缩放控件
		mapView.setBuiltInZoomControls(true); 
		// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
		mapController = mapView.getController(); 
		// 设置地图zoom级别
		mapController.setZoom(12); 
		
		mKSearch = new MKSearch();
		mKSearch.init(mapManager, new MySearchListener());// 注意,MKSearchListener只支持一个,以最后一次设置为准
		//搜索山东科技大学(36.001618315221194,120.11934041976929)附近(5000)的KTV
		if (mKSearch.poiSearchInCity("黄岛", "KTV") == -1)
		{
			System.out.println("失败");
		}
		else
		{
			System.out.println("成功");
		}
	}

	public class MySearchListener implements MKSearchListener
	{
		public void onGetAddrResult(MKAddrInfo arg0, int arg1)
		{
			/*
			 * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息
			 */
		}

		public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)
		{
			/*
			 * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
			 */
		}

		public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)
		{
			String result = "";
			/*
			 * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回
			 */
			if (arg0 == null)
			{
				return;
			}
			// 清除地图上已有的所有覆盖物
			// mapView.getOverlays().clear();
			// PoiOverlay是baidu map api提供的用于显示POI的Overlay
			PoiOverlay poioverlay = new PoiOverlay(BaiduMapActivity.this,
					mapView);
			// 在地图上显示PoiOverlay(将搜索到的兴趣点标注在地图上)
			poioverlay.setData(arg0.getAllPoi());
			// 为地图添加覆盖物
			mapView.getOverlays().add(poioverlay);
			//刚开始忘记加这几句代码,地图一直没改变,纠结了很长时间
			if (arg0.getNumPois() > 0)
			{
				// 设置其中一个搜索结果所在地理坐标为地图的中心
				MKPoiInfo poiInfo = arg0.getPoi(0);
				mapController.setCenter(poiInfo.pt);
			}
			
			// 遍历当前页返回的搜索结果(默认只返回10个) 
			for (MKPoiInfo poiInfo : arg0.getAllPoi())
			{
				result = result + "\n"+  "名称:" + poiInfo.name + "\n" + "地址:" + poiInfo.address + "\n" + "城市:" + poiInfo.city;
			}
			
			
			//用AlertDialog来显示搜索到的内容
			AlertDialog.Builder builder = new AlertDialog.Builder(BaiduMapActivity.this);   
	        builder.setTitle("搜索结果");  
	        builder.setMessage(result);  
	        builder.setPositiveButton("关闭", new android.content.DialogInterface.OnClickListener(){  
	  
	            public void onClick(DialogInterface dialog, int which) {  
	            	dialog.dismiss();
	            }  
	        });  
	        builder.show();
			
		}

		public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)
		{
			/*
			 * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息
			 */
		}

		public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)
		{
			/*
			 * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
			 */
		}
	}

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

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

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

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


 

公交换乘路线搜索及TransitOverlay

       这里只提供java代码,xml代码参照:Android学习笔记之初步学百度地图

重要代码:

  1. MKPlanNode start = new MKPlanNode();  
  2.         // 起点:天安门   
  3.         start.pt = new GeoPoint((int) (40.003834809598516 * 1E6),  
  4.                 (int) (116.3263213634491 * 1E6));  
  5.          // 设置地图的中心    
  6.         mapController.setCenter(start.pt);  
  7.         MKPlanNode end = new MKPlanNode();  
  8.         // 终点:鸟巢   
  9.         end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6));  
  10.         // 设置驾车路线搜索策略,时间优先、费用最少或距离最短   
  11.         /* 
  12.          * EBUS_TIME_FIRST 
  13.          * public static final int EBUS_TIME_FIRST 
  14.          * 公交检索策略常量:时间优先 
  15.          * EBUS_TRANSFER_FIRST 
  16.          * public static final int EBUS_TRANSFER_FIRST 
  17.          * 公交检索策略常量:最少换乘 
  18.          * EBUS_WALK_FIRST 
  19.          * public static final int EBUS_WALK_FIRST 
  20.          * 公交检索策略常量:最少步行距离 
  21.          * EBUS_NO_SUBWAY 
  22.          * public static final int EBUS_NO_SUBWAY 
  23.          * 公交检索策略常量:不含地铁 
  24.          */  
  25.         // 设置乘车路线搜索策略,时间优先、最少换乘、最少步行距离或不含地铁   
  26.         mKSearch.setTransitPolicy(MKSearch.EBUS_TRANSFER_FIRST);  
  27.         mKSearch.transitSearch("北京", start, end); // 必须设置城市名  
MKPlanNode start = new MKPlanNode();
		// 起点:天安门
		start.pt = new GeoPoint((int) (40.003834809598516 * 1E6),
				(int) (116.3263213634491 * 1E6));
		 // 设置地图的中心 
		mapController.setCenter(start.pt);
		MKPlanNode end = new MKPlanNode();
		// 终点:鸟巢
		end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6));
		// 设置驾车路线搜索策略,时间优先、费用最少或距离最短
		/*
		 * EBUS_TIME_FIRST
		 * public static final int EBUS_TIME_FIRST
		 * 公交检索策略常量:时间优先
		 * EBUS_TRANSFER_FIRST
		 * public static final int EBUS_TRANSFER_FIRST
		 * 公交检索策略常量:最少换乘
		 * EBUS_WALK_FIRST
		 * public static final int EBUS_WALK_FIRST
		 * 公交检索策略常量:最少步行距离
		 * EBUS_NO_SUBWAY
		 * public static final int EBUS_NO_SUBWAY
		 * 公交检索策略常量:不含地铁
		 */
		// 设置乘车路线搜索策略,时间优先、最少换乘、最少步行距离或不含地铁
		mKSearch.setTransitPolicy(MKSearch.EBUS_TRANSFER_FIRST);
		mKSearch.transitSearch("北京", start, end); // 必须设置城市名



 

实现MySearchListener的onGetTransitRouteResult(MKTransitRouteResult, int),并展示检索结果:

  1. @Override  
  2. public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {  
  3.     if (result == null) {  
  4.         return;  
  5.     }  
  6.     TransitOverlay transitOverlay = new TransitOverlay(MyMapActivity.this, mMapView);  
  7.     // 此处仅展示一个方案作为示例   
  8.     transitOverlay.setData(result.getPlan(0));  
  9.     mMapView.getOverlays().add(transitOverlay);  
  10. }  
@Override
public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {
    if (result == null) {
        return;
    }
    TransitOverlay transitOverlay = new TransitOverlay(MyMapActivity.this, mMapView);
    // 此处仅展示一个方案作为示例
    transitOverlay.setData(result.getPlan(0));
    mMapView.getOverlays().add(transitOverlay);
}



 

API:

transitSearch

public int transitSearch(java.lang.String city, MKPlanNode start, MKPlanNode end)

公交路线搜索. 

异步函数,返回结果在MKSearchListener里的onGetTransitRouteResult方法通知
参数:
city - 城市名,用于在哪个城市内进行检索
start - 检索的起点,可通过关键字,坐标两种方式指定
end - 检索的终点,可通过关键字,坐标两种方式指定
返回:
成功返回0,否则返回-1

setTransitPolicy

public int setTransitPolicy(int policy)

设置路线规划策略. 

参数为策略常量。对下次搜索有效
参数:
policy - EBUS_TIME_FIRST:时间优先;EBUS_TRANSFER_FIRST:少换乘;EBUS_WALK_FIRST:少步行;EBUS_NO_SUBWAY: 非地铁
返回:
成功返回0,否则返回-1


 

具体实现:

  1. package xiaosi.baiduMap;  
  2.   
  3. import android.app.AlertDialog;  
  4. import android.content.DialogInterface;  
  5. import android.os.Bundle;  
  6.   
  7. import com.baidu.mapapi.BMapManager;  
  8. import com.baidu.mapapi.GeoPoint;  
  9. import com.baidu.mapapi.MKAddrInfo;  
  10. import com.baidu.mapapi.MKDrivingRouteResult;  
  11. import com.baidu.mapapi.MKPlanNode;  
  12. import com.baidu.mapapi.MKPoiInfo;  
  13. import com.baidu.mapapi.MKPoiResult;  
  14. import com.baidu.mapapi.MKSearch;  
  15. import com.baidu.mapapi.MKSearchListener;  
  16. import com.baidu.mapapi.MKTransitRouteResult;  
  17. import com.baidu.mapapi.MKWalkingRouteResult;  
  18. import com.baidu.mapapi.MapActivity;  
  19. import com.baidu.mapapi.MapController;  
  20. import com.baidu.mapapi.MapView;  
  21. import com.baidu.mapapi.PoiOverlay;  
  22. import com.baidu.mapapi.RouteOverlay;  
  23. import com.baidu.mapapi.TransitOverlay;  
  24.   
  25. public class BaiduMapActivity extends MapActivity  
  26. {  
  27.     /** Called when the activity is first created. */  
  28.     private BMapManager mapManager = null;  
  29.     private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";  
  30.     private MapView mapView = null;  
  31.     private MKSearch mKSearch;  
  32.     private MapController mapController = null;  
  33.   
  34.     @Override  
  35.     public void onCreate(Bundle savedInstanceState)  
  36.     {  
  37.         super.onCreate(savedInstanceState);  
  38.         setContentView(R.layout.main);  
  39.         mapManager = new BMapManager(getApplication());  
  40.         mapManager.init(key, null);  
  41.         super.initMapActivity(mapManager);  
  42.         mapView = (MapView) findViewById(R.id.mapView);  
  43.         // 设置启用内置的缩放控件   
  44.         mapView.setBuiltInZoomControls(true);  
  45.         // 得到mMapView的控制权,可以用它控制和驱动平移和缩放   
  46.         mapController = mapView.getController();  
  47.         // 设置地图zoom级别   
  48.         mapController.setZoom(12);  
  49.         mKSearch = new MKSearch();  
  50.         // 注意,MKSearchListener只支持一个,以最后一次设置为准   
  51.         mKSearch.init(mapManager, new MySearchListener());  
  52.           
  53.         MKPlanNode start = new MKPlanNode();  
  54.         // 起点:天安门   
  55.         start.pt = new GeoPoint((int) (40.003834809598516 * 1E6),  
  56.                 (int) (116.3263213634491 * 1E6));  
  57.          // 设置地图的中心    
  58.         mapController.setCenter(start.pt);  
  59.         MKPlanNode end = new MKPlanNode();  
  60.         // 终点:鸟巢   
  61.         end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6));  
  62.         // 设置驾车路线搜索策略,时间优先、费用最少或距离最短   
  63.         /* 
  64.          * EBUS_TIME_FIRST 
  65.          * public static final int EBUS_TIME_FIRST 
  66.          * 公交检索策略常量:时间优先 
  67.          * EBUS_TRANSFER_FIRST 
  68.          * public static final int EBUS_TRANSFER_FIRST 
  69.          * 公交检索策略常量:最少换乘 
  70.          * EBUS_WALK_FIRST 
  71.          * public static final int EBUS_WALK_FIRST 
  72.          * 公交检索策略常量:最少步行距离 
  73.          * EBUS_NO_SUBWAY 
  74.          * public static final int EBUS_NO_SUBWAY 
  75.          * 公交检索策略常量:不含地铁 
  76.          */  
  77.         // 设置乘车路线搜索策略,时间优先、最少换乘、最少步行距离或不含地铁   
  78.         mKSearch.setTransitPolicy(MKSearch.EBUS_TRANSFER_FIRST);  
  79.         mKSearch.transitSearch("北京", start, end); // 必须设置城市名   
  80.     }  
  81.   
  82.     public class MySearchListener implements MKSearchListener  
  83.     {  
  84.         public void onGetAddrResult(MKAddrInfo arg0, int arg1)  
  85.         {  
  86.             /* 
  87.              * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息 
  88.              */  
  89.         }  
  90.   
  91.         public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)  
  92.         {  
  93.             /* 
  94.              * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回 
  95.              */  
  96.         }  
  97.   
  98.         public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)  
  99.         {  
  100.             /* 
  101.              * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回 
  102.              */  
  103.               
  104.         }  
  105.   
  106.         public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)  
  107.         {  
  108.             /* 
  109.              * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息 
  110.              */  
  111.              if (arg0 == null) {  
  112.                     return;  
  113.                 }  
  114.                 TransitOverlay transitOverlay = new TransitOverlay(BaiduMapActivity.this, mapView);  
  115.                 // 此处仅展示一个方案作为示例   
  116.                 transitOverlay.setData(arg0.getPlan(0));  
  117.                 mapView.getOverlays().add(transitOverlay);  
  118.         }  
  119.   
  120.         public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)  
  121.         {  
  122.             /* 
  123.              * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回 
  124.              */  
  125.         }  
  126.     }  
  127.   
  128.     @Override  
  129.     protected boolean isRouteDisplayed()  
  130.     {  
  131.         return false;  
  132.     }  
  133.   
  134.     @Override  
  135.     protected void onDestroy()  
  136.     {  
  137.         if (mapManager != null)  
  138.         {  
  139.             mapManager.destroy();  
  140.             mapManager = null;  
  141.         }  
  142.         super.onDestroy();  
  143.     }  
  144.   
  145.     @Override  
  146.     protected void onPause()  
  147.     {  
  148.         if (mapManager != null)  
  149.         {  
  150.             mapManager.stop();  
  151.         }  
  152.         super.onPause();  
  153.     }  
  154.   
  155.     @Override  
  156.     protected void onResume()  
  157.     {  
  158.         if (mapManager != null)  
  159.         {  
  160.             mapManager.start();  
  161.         }  
  162.         super.onResume();  
  163.     }  
  164. }  
package xiaosi.baiduMap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
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 com.baidu.mapapi.TransitOverlay;

public class BaiduMapActivity extends MapActivity
{
	/** Called when the activity is first created. */
	private BMapManager mapManager = null;
	private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";
	private MapView mapView = null;
	private MKSearch mKSearch;
	private MapController mapController = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mapManager = new BMapManager(getApplication());
		mapManager.init(key, null);
		super.initMapActivity(mapManager);
		mapView = (MapView) findViewById(R.id.mapView);
		// 设置启用内置的缩放控件
		mapView.setBuiltInZoomControls(true);
		// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
		mapController = mapView.getController();
		// 设置地图zoom级别
		mapController.setZoom(12);
		mKSearch = new MKSearch();
		// 注意,MKSearchListener只支持一个,以最后一次设置为准
		mKSearch.init(mapManager, new MySearchListener());
		
		MKPlanNode start = new MKPlanNode();
		// 起点:天安门
		start.pt = new GeoPoint((int) (40.003834809598516 * 1E6),
				(int) (116.3263213634491 * 1E6));
		 // 设置地图的中心 
		mapController.setCenter(start.pt);
		MKPlanNode end = new MKPlanNode();
		// 终点:鸟巢
		end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6));
		// 设置驾车路线搜索策略,时间优先、费用最少或距离最短
		/*
		 * EBUS_TIME_FIRST
		 * public static final int EBUS_TIME_FIRST
		 * 公交检索策略常量:时间优先
		 * EBUS_TRANSFER_FIRST
		 * public static final int EBUS_TRANSFER_FIRST
		 * 公交检索策略常量:最少换乘
		 * EBUS_WALK_FIRST
		 * public static final int EBUS_WALK_FIRST
		 * 公交检索策略常量:最少步行距离
		 * EBUS_NO_SUBWAY
		 * public static final int EBUS_NO_SUBWAY
		 * 公交检索策略常量:不含地铁
		 */
		// 设置乘车路线搜索策略,时间优先、最少换乘、最少步行距离或不含地铁
		mKSearch.setTransitPolicy(MKSearch.EBUS_TRANSFER_FIRST);
		mKSearch.transitSearch("北京", start, end); // 必须设置城市名
	}

	public class MySearchListener implements MKSearchListener
	{
		public void onGetAddrResult(MKAddrInfo arg0, int arg1)
		{
			/*
			 * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息
			 */
		}

		public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)
		{
			/*
			 * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
			 */
		}

		public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)
		{
			/*
			 * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回
			 */
			
		}

		public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)
		{
			/*
			 * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息
			 */
			 if (arg0 == null) {
			        return;
			    }
			    TransitOverlay transitOverlay = new TransitOverlay(BaiduMapActivity.this, mapView);
			    // 此处仅展示一个方案作为示例
			    transitOverlay.setData(arg0.getPlan(0));
			    mapView.getOverlays().add(transitOverlay);
		}

		public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)
		{
			/*
			 * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
			 */
		}
	}

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

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

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

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


 

步行路线搜索及RouteOverlay

方式与驾车路线搜索类似,只需将mMKSearch.drivingSearch(null, start, null, end)修改为mMKSearch.walkingSearch(null, start, null, end),实现的方法改为onGetWalkingRouteResult即可,不再赘述。

驾车路线搜索及RouteOverlay

重要代码:

  1. MKPlanNode start = new MKPlanNode();  
  2.         // 起点:天安门   
  3.         start.pt = new GeoPoint((int) (40.003834809598516 * 1E6),  
  4.                 (int) (116.3263213634491 * 1E6));  
  5.          // 设置地图的中心    
  6.         mapController.setCenter(start.pt);  
  7.         MKPlanNode end = new MKPlanNode();  
  8.         // 终点:鸟巢   
  9.         end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6));  
  10.         // 设置驾车路线搜索策略,时间优先、费用最少或距离最短   
  11.         /* 
  12.          * ECAR_DIS_FIRST 
  13.          * public static final int ECAR_DIS_FIRST 
  14.          * 驾乘检索策略常量:最短距离 
  15.          * ECAR_FEE_FIRST 
  16.          * public static final int ECAR_FEE_FIRST 
  17.          * 驾乘检索策略常量:较少费用 
  18.          */  
  19.         //设置驾车路线规划策略.   
  20.         mKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);  
  21.         //驾乘路线搜索.   
  22.         mKSearch.drivingSearch("北京", start, "北京", end);  
MKPlanNode start = new MKPlanNode();
		// 起点:天安门
		start.pt = new GeoPoint((int) (40.003834809598516 * 1E6),
				(int) (116.3263213634491 * 1E6));
		 // 设置地图的中心 
		mapController.setCenter(start.pt);
		MKPlanNode end = new MKPlanNode();
		// 终点:鸟巢
		end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6));
		// 设置驾车路线搜索策略,时间优先、费用最少或距离最短
		/*
		 * ECAR_DIS_FIRST
		 * public static final int ECAR_DIS_FIRST
		 * 驾乘检索策略常量:最短距离
		 * ECAR_FEE_FIRST
		 * public static final int ECAR_FEE_FIRST
		 * 驾乘检索策略常量:较少费用
		 */
		//设置驾车路线规划策略.
		mKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
		//驾乘路线搜索.
		mKSearch.drivingSearch("北京", start, "北京", end);



实现MySearchListener的onGetTransitRouteResult(MKTransitRouteResult, int),并展示检索结果:

  1. public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)  
  2.         {  
  3.             /* 
  4.              * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回 
  5.              */  
  6.             if (arg0 == null)  
  7.             {  
  8.                 return;  
  9.             }  
  10.             RouteOverlay routeOverlay = new RouteOverlay(BaiduMapActivity.this,  
  11.                     mapView);  
  12.             // 此处仅展示一个方案作为示例   
  13.             routeOverlay.setData(arg0.getPlan(0).getRoute(0));  
  14.             mapView.getOverlays().add(routeOverlay);  
  15.         }  
public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)
		{
			/*
			 * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
			 */
			if (arg0 == null)
			{
				return;
			}
			RouteOverlay routeOverlay = new RouteOverlay(BaiduMapActivity.this,
					mapView);
			// 此处仅展示一个方案作为示例
			routeOverlay.setData(arg0.getPlan(0).getRoute(0));
			mapView.getOverlays().add(routeOverlay);
		}



 

API:

drivingSearch

public int drivingSearch(java.lang.String startCity, MKPlanNode start, java.lang.String endCity, MKPlanNode end)

驾乘路线搜索.

异步函数,返回结果在MKSearchListener里的onGetDrivingRouteResult方法通知
参数:
startCity - 起点所在城市,起点为坐标时可不填
start - 搜索的起点,可以为坐标,名称任一种
endCity - 终点所在城市,终点为坐标可不填
end - 搜索的终点,可以为坐标,名称任一种
返回:
成功返回0,否则返回-1


 

setDrivingPolicy

public int setDrivingPolicy(int policy)

设置驾车路线规划策略. 参数为策略常量。对下次搜索有效
参数:
policy - ECAR_TIME_FIRST:时间优先;ECAR_DIS_FIRST:距离最短;ECAR_FEE_FIRST:费用最少
返回:
成功返回0,否则返回-1


 


具体实现:

注意:在模拟器上模拟不能显示乘车线路,不知道是我的问题,还是模拟器的问题。但在真机上能体现出路线。

  1. package xiaosi.baiduMap;  
  2.   
  3. import android.app.AlertDialog;  
  4. import android.content.DialogInterface;  
  5. import android.os.Bundle;  
  6.   
  7. import com.baidu.mapapi.BMapManager;  
  8. import com.baidu.mapapi.GeoPoint;  
  9. import com.baidu.mapapi.MKAddrInfo;  
  10. import com.baidu.mapapi.MKDrivingRouteResult;  
  11. import com.baidu.mapapi.MKPlanNode;  
  12. import com.baidu.mapapi.MKPoiInfo;  
  13. import com.baidu.mapapi.MKPoiResult;  
  14. import com.baidu.mapapi.MKSearch;  
  15. import com.baidu.mapapi.MKSearchListener;  
  16. import com.baidu.mapapi.MKTransitRouteResult;  
  17. import com.baidu.mapapi.MKWalkingRouteResult;  
  18. import com.baidu.mapapi.MapActivity;  
  19. import com.baidu.mapapi.MapController;  
  20. import com.baidu.mapapi.MapView;  
  21. import com.baidu.mapapi.PoiOverlay;  
  22. import com.baidu.mapapi.RouteOverlay;  
  23.   
  24. public class BaiduMapActivity extends MapActivity  
  25. {  
  26.     /** Called when the activity is first created. */  
  27.     private BMapManager mapManager = null;  
  28.     private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";  
  29.     private MapView mapView = null;  
  30.     private MKSearch mKSearch;  
  31.     private MapController mapController = null;  
  32.   
  33.     @Override  
  34.     public void onCreate(Bundle savedInstanceState)  
  35.     {  
  36.         super.onCreate(savedInstanceState);  
  37.         setContentView(R.layout.main);  
  38.         mapManager = new BMapManager(getApplication());  
  39.         mapManager.init(key, null);  
  40.         super.initMapActivity(mapManager);  
  41.         mapView = (MapView) findViewById(R.id.mapView);  
  42.         // 设置启用内置的缩放控件   
  43.         mapView.setBuiltInZoomControls(true);  
  44.         // 得到mMapView的控制权,可以用它控制和驱动平移和缩放   
  45.         mapController = mapView.getController();  
  46.         // 设置地图zoom级别   
  47.         mapController.setZoom(12);  
  48.         mKSearch = new MKSearch();  
  49.         // 注意,MKSearchListener只支持一个,以最后一次设置为准   
  50.         mKSearch.init(mapManager, new MySearchListener());  
  51.           
  52.         MKPlanNode start = new MKPlanNode();  
  53.         // 起点:天安门   
  54.         start.pt = new GeoPoint((int) (40.003834809598516 * 1E6),  
  55.                 (int) (116.3263213634491 * 1E6));  
  56.          // 设置地图的中心    
  57.         mapController.setCenter(start.pt);  
  58.         MKPlanNode end = new MKPlanNode();  
  59.         // 终点:鸟巢   
  60.         end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6));  
  61.         // 设置驾车路线搜索策略,时间优先、费用最少或距离最短   
  62.         /* 
  63.          * ECAR_DIS_FIRST 
  64.          * public static final int ECAR_DIS_FIRST 
  65.          * 驾乘检索策略常量:最短距离 
  66.          * ECAR_FEE_FIRST 
  67.          * public static final int ECAR_FEE_FIRST 
  68.          * 驾乘检索策略常量:较少费用 
  69.          */  
  70.         //设置驾车路线规划策略.   
  71.         mKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);  
  72.         //驾乘路线搜索.   
  73.         mKSearch.drivingSearch("北京", start, "北京", end);  
  74.     }  
  75.   
  76.     public class MySearchListener implements MKSearchListener  
  77.     {  
  78.         public void onGetAddrResult(MKAddrInfo arg0, int arg1)  
  79.         {  
  80.             /* 
  81.              * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息 
  82.              */  
  83.         }  
  84.   
  85.         public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)  
  86.         {  
  87.             /* 
  88.              * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回 
  89.              */  
  90.             if (arg0 == null)  
  91.             {  
  92.                 return;  
  93.             }  
  94.             RouteOverlay routeOverlay = new RouteOverlay(BaiduMapActivity.this,  
  95.                     mapView);  
  96.             // 此处仅展示一个方案作为示例   
  97.             routeOverlay.setData(arg0.getPlan(0).getRoute(0));  
  98.             mapView.getOverlays().add(routeOverlay);  
  99.         }  
  100.   
  101.         public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)  
  102.         {  
  103.             String result = "";  
  104.             /* 
  105.              * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回 
  106.              */  
  107.             if (arg0 == null)  
  108.             {  
  109.                 return;  
  110.             }  
  111.             // 清除地图上已有的所有覆盖物   
  112.             // mapView.getOverlays().clear();   
  113.             // PoiOverlay是baidu map api提供的用于显示POI的Overlay   
  114.             PoiOverlay poioverlay = new PoiOverlay(BaiduMapActivity.this,  
  115.                     mapView);  
  116.             // 在地图上显示PoiOverlay(将搜索到的兴趣点标注在地图上)   
  117.             poioverlay.setData(arg0.getAllPoi());  
  118.             // 为地图添加覆盖物   
  119.             mapView.getOverlays().add(poioverlay);  
  120.             // 刚开始忘记加这几句代码,地图一直没改变,纠结了很长时间   
  121.             if (arg0.getNumPois() > 0)  
  122.             {  
  123.                 // 设置其中一个搜索结果所在地理坐标为地图的中心   
  124.                 MKPoiInfo poiInfo = arg0.getPoi(0);  
  125.                 mapController.setCenter(poiInfo.pt);  
  126.             }  
  127.             // 遍历当前页返回的搜索结果(默认只返回10个)   
  128.             for (MKPoiInfo poiInfo : arg0.getAllPoi())  
  129.             {  
  130.                 result = result + "\n" + "名称:" + poiInfo.name + "\n" + "地址:"  
  131.                         + poiInfo.address + "\n" + "城市:" + poiInfo.city;  
  132.             }  
  133.             // 用AlertDialog来显示搜索到的内容   
  134.             AlertDialog.Builder builder = new AlertDialog.Builder(  
  135.                     BaiduMapActivity.this);  
  136.             builder.setTitle("搜索结果");  
  137.             builder.setMessage(result);  
  138.             builder.setPositiveButton("关闭",  
  139.                     new android.content.DialogInterface.OnClickListener() {  
  140.                         public void onClick(DialogInterface dialog, int which)  
  141.                         {  
  142.                             dialog.dismiss();  
  143.                         }  
  144.                     });  
  145.             builder.show();  
  146.         }  
  147.   
  148.         public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)  
  149.         {  
  150.             /* 
  151.              * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息 
  152.              */  
  153.         }  
  154.   
  155.         public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)  
  156.         {  
  157.             /* 
  158.              * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回 
  159.              */  
  160.         }  
  161.     }  
  162.   
  163.     @Override  
  164.     protected boolean isRouteDisplayed()  
  165.     {  
  166.         return false;  
  167.     }  
  168.   
  169.     @Override  
  170.     protected void onDestroy()  
  171.     {  
  172.         if (mapManager != null)  
  173.         {  
  174.             mapManager.destroy();  
  175.             mapManager = null;  
  176.         }  
  177.         super.onDestroy();  
  178.     }  
  179.   
  180.     @Override  
  181.     protected void onPause()  
  182.     {  
  183.         if (mapManager != null)  
  184.         {  
  185.             mapManager.stop();  
  186.         }  
  187.         super.onPause();  
  188.     }  
  189.   
  190.     @Override  
  191.     protected void onResume()  
  192.     {  
  193.         if (mapManager != null)  
  194.         {  
  195.             mapManager.start();  
  196.         }  
  197.         super.onResume();  
  198.     }  
  199. }  
package xiaosi.baiduMap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPlanNode;
import com.baidu.mapapi.MKPoiInfo;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
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;

public class BaiduMapActivity extends MapActivity
{
	/** Called when the activity is first created. */
	private BMapManager mapManager = null;
	private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";
	private MapView mapView = null;
	private MKSearch mKSearch;
	private MapController mapController = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		mapManager = new BMapManager(getApplication());
		mapManager.init(key, null);
		super.initMapActivity(mapManager);
		mapView = (MapView) findViewById(R.id.mapView);
		// 设置启用内置的缩放控件
		mapView.setBuiltInZoomControls(true);
		// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
		mapController = mapView.getController();
		// 设置地图zoom级别
		mapController.setZoom(12);
		mKSearch = new MKSearch();
		// 注意,MKSearchListener只支持一个,以最后一次设置为准
		mKSearch.init(mapManager, new MySearchListener());
		
		MKPlanNode start = new MKPlanNode();
		// 起点:天安门
		start.pt = new GeoPoint((int) (40.003834809598516 * 1E6),
				(int) (116.3263213634491 * 1E6));
		 // 设置地图的中心 
		mapController.setCenter(start.pt);
		MKPlanNode end = new MKPlanNode();
		// 终点:鸟巢
		end.pt = new GeoPoint((int)(39.99142 * 1E6),(int)(116.39026999999998* 1E6));
		// 设置驾车路线搜索策略,时间优先、费用最少或距离最短
		/*
		 * ECAR_DIS_FIRST
		 * public static final int ECAR_DIS_FIRST
		 * 驾乘检索策略常量:最短距离
		 * ECAR_FEE_FIRST
		 * public static final int ECAR_FEE_FIRST
		 * 驾乘检索策略常量:较少费用
		 */
		//设置驾车路线规划策略.
		mKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
		//驾乘路线搜索.
		mKSearch.drivingSearch("北京", start, "北京", end);
	}

	public class MySearchListener implements MKSearchListener
	{
		public void onGetAddrResult(MKAddrInfo arg0, int arg1)
		{
			/*
			 * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息
			 */
		}

		public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)
		{
			/*
			 * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
			 */
			if (arg0 == null)
			{
				return;
			}
			RouteOverlay routeOverlay = new RouteOverlay(BaiduMapActivity.this,
					mapView);
			// 此处仅展示一个方案作为示例
			routeOverlay.setData(arg0.getPlan(0).getRoute(0));
			mapView.getOverlays().add(routeOverlay);
		}

		public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)
		{
			String result = "";
			/*
			 * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回
			 */
			if (arg0 == null)
			{
				return;
			}
			// 清除地图上已有的所有覆盖物
			// mapView.getOverlays().clear();
			// PoiOverlay是baidu map api提供的用于显示POI的Overlay
			PoiOverlay poioverlay = new PoiOverlay(BaiduMapActivity.this,
					mapView);
			// 在地图上显示PoiOverlay(将搜索到的兴趣点标注在地图上)
			poioverlay.setData(arg0.getAllPoi());
			// 为地图添加覆盖物
			mapView.getOverlays().add(poioverlay);
			// 刚开始忘记加这几句代码,地图一直没改变,纠结了很长时间
			if (arg0.getNumPois() > 0)
			{
				// 设置其中一个搜索结果所在地理坐标为地图的中心
				MKPoiInfo poiInfo = arg0.getPoi(0);
				mapController.setCenter(poiInfo.pt);
			}
			// 遍历当前页返回的搜索结果(默认只返回10个)
			for (MKPoiInfo poiInfo : arg0.getAllPoi())
			{
				result = result + "\n" + "名称:" + poiInfo.name + "\n" + "地址:"
						+ poiInfo.address + "\n" + "城市:" + poiInfo.city;
			}
			// 用AlertDialog来显示搜索到的内容
			AlertDialog.Builder builder = new AlertDialog.Builder(
					BaiduMapActivity.this);
			builder.setTitle("搜索结果");
			builder.setMessage(result);
			builder.setPositiveButton("关闭",
					new android.content.DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.dismiss();
						}
					});
			builder.show();
		}

		public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)
		{
			/*
			 * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息
			 */
		}

		public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)
		{
			/*
			 * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
			 */
		}
	}

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

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

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

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


 



重要方法:

public int geocode(java.lang.String strAddr, java.lang.String city)

根据地址名获取地址信息 

异步函数, 返回结果在MKSearchListener里的onGetAddrResult方法通知
参数:
strAddr - 地址名
city - 地址所在城市
返回:

成功返回0,否则返回-1


具体实现:

  1. package xiaosi.baiduMap;  
  2.   
  3. import android.app.AlertDialog;  
  4. import android.content.DialogInterface;  
  5. import android.os.Bundle;  
  6.   
  7. import com.baidu.mapapi.BMapManager;  
  8. import com.baidu.mapapi.MKAddrInfo;  
  9. import com.baidu.mapapi.MKDrivingRouteResult;  
  10. import com.baidu.mapapi.MKPoiResult;  
  11. import com.baidu.mapapi.MKSearch;  
  12. import com.baidu.mapapi.MKSearchListener;  
  13. import com.baidu.mapapi.MKTransitRouteResult;  
  14. import com.baidu.mapapi.MKWalkingRouteResult;  
  15. import com.baidu.mapapi.MapActivity;  
  16. import com.baidu.mapapi.MapController;  
  17. import com.baidu.mapapi.MapView;  
  18.   
  19. public class BaiduMapActivity extends MapActivity  
  20. {  
  21.     /** Called when the activity is first created. */  
  22.     private BMapManager mapManager = null;  
  23.     private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";  
  24.     private MapView mapView = null;  
  25.     private MKSearch mKSearch;  
  26.     private MapController mapController = null;  
  27.     @Override  
  28.     public void onCreate(Bundle savedInstanceState)  
  29.     {  
  30.         super.onCreate(savedInstanceState);  
  31.         setContentView(R.layout.main);  
  32.         init();  
  33.     }  
  34.   
  35.     private void init()  
  36.     {  
  37.         mapManager = new BMapManager(getApplication());  
  38.         mapManager.init(key, null);  
  39.         super.initMapActivity(mapManager);  
  40.         mapView = (MapView) findViewById(R.id.mapView);  
  41.         // 设置启用内置的缩放控件   
  42.         mapView.setBuiltInZoomControls(true);  
  43.         // 得到mMapView的控制权,可以用它控制和驱动平移和缩放   
  44.         mapController = mapView.getController();  
  45.         // 设置地图zoom级别   
  46.         mapController.setZoom(12);  
  47.         mKSearch = new MKSearch();  
  48.         // 注意,MKSearchListener只支持一个,以最后一次设置为准   
  49.         mKSearch.init(mapManager, new MySearchListener());  
  50.         if (mKSearch.geocode("五四广场""青岛") == 0)  
  51.         {  
  52.             System.out.println("搜索成功");  
  53.         }  
  54.         else  
  55.         {  
  56.             System.out.println("搜索失败");  
  57.         }  
  58.     }  
  59.   
  60.     public class MySearchListener implements MKSearchListener  
  61.     {  
  62.         public void onGetAddrResult(MKAddrInfo arg0, int arg1)  
  63.         {  
  64.             /* 
  65.              * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息 
  66.              */  
  67.             String Location = null;  
  68.             if (arg0 == null)  
  69.             {  
  70.                 Location = "没有搜索到该地址";  
  71.             }  
  72.             else  
  73.             {  
  74.                 // 获得搜索地址的经纬度   
  75.                 Location = "纬度:" + arg0.geoPt.getLatitudeE6() / 1E6 + "\n"  
  76.                         + "经度:" + arg0.geoPt.getLongitudeE6() / 1E6 + "\n";  
  77.                 mapController.animateTo(arg0.geoPt);  
  78.             }  
  79.             AlertDialog.Builder builder = new AlertDialog.Builder(  
  80.                     BaiduMapActivity.this);  
  81.             builder.setTitle("搜索结果");  
  82.             builder.setMessage(Location);  
  83.             builder.setPositiveButton("关闭",  
  84.                     new android.content.DialogInterface.OnClickListener() {  
  85.                         public void onClick(DialogInterface dialog, int which)  
  86.                         {  
  87.                             dialog.dismiss();  
  88.                         }  
  89.                     });  
  90.             builder.show();  
  91.         }  
  92.   
  93.         public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)  
  94.         {  
  95.             /* 
  96.              * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回 
  97.              */  
  98.         }  
  99.   
  100.         public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)  
  101.         {  
  102.             /* 
  103.              * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回 
  104.              */  
  105.         }  
  106.   
  107.         public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)  
  108.         {  
  109.             /* 
  110.              * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息 
  111.              */  
  112.         }  
  113.   
  114.         public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)  
  115.         {  
  116.             /* 
  117.              * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回 
  118.              */  
  119.         }  
  120.     }  
  121.   
  122.     @Override  
  123.     protected boolean isRouteDisplayed()  
  124.     {  
  125.         return false;  
  126.     }  
  127.   
  128.     @Override  
  129.     protected void onDestroy()  
  130.     {  
  131.         if (mapManager != null)  
  132.         {  
  133.             mapManager.destroy();  
  134.             mapManager = null;  
  135.         }  
  136.         super.onDestroy();  
  137.     }  
  138.   
  139.     @Override  
  140.     protected void onPause()  
  141.     {  
  142.         if (mapManager != null)  
  143.         {  
  144.             mapManager.stop();  
  145.         }  
  146.         super.onPause();  
  147.     }  
  148.   
  149.     @Override  
  150.     protected void onResume()  
  151.     {  
  152.         if (mapManager != null)  
  153.         {  
  154.             mapManager.start();  
  155.         }  
  156.         super.onResume();  
  157.     }  
  158. }  
package xiaosi.baiduMap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.MKAddrInfo;
import com.baidu.mapapi.MKDrivingRouteResult;
import com.baidu.mapapi.MKPoiResult;
import com.baidu.mapapi.MKSearch;
import com.baidu.mapapi.MKSearchListener;
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;

public class BaiduMapActivity extends MapActivity
{
	/** Called when the activity is first created. */
	private BMapManager mapManager = null;
	private String key = "1B79478DA01F7800AEA8602517A6D89B38151105";
	private MapView mapView = null;
	private MKSearch mKSearch;
	private MapController mapController = null;
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}

	private void init()
	{
		mapManager = new BMapManager(getApplication());
		mapManager.init(key, null);
		super.initMapActivity(mapManager);
		mapView = (MapView) findViewById(R.id.mapView);
		// 设置启用内置的缩放控件
		mapView.setBuiltInZoomControls(true);
		// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
		mapController = mapView.getController();
		// 设置地图zoom级别
		mapController.setZoom(12);
		mKSearch = new MKSearch();
		// 注意,MKSearchListener只支持一个,以最后一次设置为准
		mKSearch.init(mapManager, new MySearchListener());
		if (mKSearch.geocode("五四广场", "青岛") == 0)
		{
			System.out.println("搜索成功");
		}
		else
		{
			System.out.println("搜索失败");
		}
	}

	public class MySearchListener implements MKSearchListener
	{
		public void onGetAddrResult(MKAddrInfo arg0, int arg1)
		{
			/*
			 * 返回地址信息搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示结果正确,result中有相关结果信息;100表示结果正确,无相关地址信息
			 */
			String Location = null;
			if (arg0 == null)
			{
				Location = "没有搜索到该地址";
			}
			else
			{
				// 获得搜索地址的经纬度
				Location = "纬度:" + arg0.geoPt.getLatitudeE6() / 1E6 + "\n"
						+ "经度:" + arg0.geoPt.getLongitudeE6() / 1E6 + "\n";
				mapController.animateTo(arg0.geoPt);
			}
			AlertDialog.Builder builder = new AlertDialog.Builder(
					BaiduMapActivity.this);
			builder.setTitle("搜索结果");
			builder.setMessage(Location);
			builder.setPositiveButton("关闭",
					new android.content.DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which)
						{
							dialog.dismiss();
						}
					});
			builder.show();
		}

		public void onGetDrivingRouteResult(MKDrivingRouteResult arg0, int arg1)
		{
			/*
			 * 返回驾乘路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
			 */
		}

		public void onGetPoiResult(MKPoiResult arg0, int arg1, int arg2)
		{
			/*
			 * 返回poi搜索结果。 参数: arg0 - 搜索结果 arg1 - 返回结果类型: MKSearch.TYPE_POI_LIST MKSearch.TYPE_AREA_POI_LIST MKSearch.TYPE_CITY_LIST arg2 - 错误号,0表示正确返回
			 */
		}

		public void onGetTransitRouteResult(MKTransitRouteResult arg0, int arg1)
		{
			/*
			 * 返回公交搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回, 当返回MKEvent.ERROR_ROUTE_ADDR时,表示起点或终点有歧义, 调用MKTransitRouteResult的getAddrResult方法获取推荐的起点或终点信息
			 */
		}

		public void onGetWalkingRouteResult(MKWalkingRouteResult arg0, int arg1)
		{
			/*
			 * 返回步行路线搜索结果。 参数: arg0 - 搜索结果 arg1 - 错误号,0表示正确返回
			 */
		}
	}

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

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

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

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

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值