POI搜索有三种方式,根据范围和检索词发起范围检索poiSearchInbounds,城市poi检索poiSearchInCity,周边检索poiSearchNearBy。
public int poiSearchInCity(java.lang.String city, java.lang.String key)
-
城市内poi检索.
- 异步函数,返回结果在MKSearchListener里的onGetPoiResult方法通知
-
参数:
- city - 城市名,如果设置为"",则将在MapView所在的当前城市内进行搜索
- key - 关键词
-
返回:
成功返回0,否则返回-1
- mKSearch.poiSearchInCity("黄岛", "KTV")
- 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();
- }
- }
Demo: 检索天安门周边5000米之内的KFC餐厅
mKSearch.poiSearchInCity("黄岛", "KTV")
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(); } }
-
- 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); // 必须设置城市名
- @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);
- }
-
公交路线搜索.
- 异步函数,返回结果在MKSearchListener里的onGetTransitRouteResult方法通知
-
参数:
- city - 城市名,用于在哪个城市内进行检索
- start - 检索的起点,可通过关键字,坐标两种方式指定
- end - 检索的终点,可通过关键字,坐标两种方式指定
-
返回:
- 成功返回0,否则返回-1
-
设置路线规划策略.
- 参数为策略常量。对下次搜索有效
-
参数:
- policy - EBUS_TIME_FIRST:时间优先;EBUS_TRANSFER_FIRST:少换乘;EBUS_WALK_FIRST:少步行;EBUS_NO_SUBWAY: 非地铁
-
返回:
- 成功返回0,否则返回-1
- 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();
- }
- }
公交换乘路线搜索及TransitOverlay
这里只提供java代码,xml代码参照:Android学习笔记之初步学百度地图
重要代码:
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),并展示检索结果:
@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)
setTransitPolicy
public int setTransitPolicy(int policy)
具体实现:
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(); } }
-
- 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 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);
- }
-
驾乘路线搜索.
- 异步函数,返回结果在MKSearchListener里的onGetDrivingRouteResult方法通知
-
参数:
- startCity - 起点所在城市,起点为坐标时可不填
- start - 搜索的起点,可以为坐标,名称任一种
- endCity - 终点所在城市,终点为坐标可不填
- end - 搜索的终点,可以为坐标,名称任一种
-
返回:
- 成功返回0,否则返回-1
- 设置驾车路线规划策略. 参数为策略常量。对下次搜索有效
-
参数:
- policy - ECAR_TIME_FIRST:时间优先;ECAR_DIS_FIRST:距离最短;ECAR_FEE_FIRST:费用最少
-
返回:
- 成功返回0,否则返回-1
- 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();
- }
- }
-
根据地址名获取地址信息
- 异步函数, 返回结果在MKSearchListener里的onGetAddrResult方法通知
-
参数:
- strAddr - 地址名
- city - 地址所在城市
-
返回:
- 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
具体实现:
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(); } }
步行路线搜索及RouteOverlay
方式与驾车路线搜索类似,只需将mMKSearch.drivingSearch(null, start, null, end)修改为mMKSearch.walkingSearch(null, start, null, end),实现的方法改为onGetWalkingRouteResult即可,不再赘述。
驾车路线搜索及RouteOverlay
重要代码:
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),并展示检索结果:
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)
setDrivingPolicy
public int setDrivingPolicy(int policy)
具体实现:
注意:在模拟器上模拟不能显示乘车线路,不知道是我的问题,还是模拟器的问题。但在真机上能体现出路线。
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)