百度地图从零学起(七)百度提供给开发者的服务

来源:互联网 发布:mac怎么照片去硬盘 编辑:程序博客网 时间:2024/04/28 23:03

搜索服务

百度地图移动版API集成搜索服务包括:位置检索、周边检索、范围检索、公交检索、驾乘检索、步行检索,通过初始化MKSearch类,注册搜索结果的监听对象MKSearchListener,实现异步搜索服务。首先自定义MySearchListener实现MKSearchListener接口,通过不同的回调方法,获得搜索结果:

  1. public class MySearchListener implements MKSearchListener {
  2. @Override
  3. public void onGetAddrResult(MKAddrInfo result, int iError) {
  4. }
  5.  
  6. @Override
  7. public void onGetDrivingRouteResult(MKDrivingRouteResult result, int iError) {
  8. }
  9.  
  10. @Override
  11. public void onGetPoiResult(MKPoiResult result, int type, int iError) {
  12. }
  13.  
  14. @Override
  15. public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) {
  16. }
  17.  
  18. @Override
  19. public void onGetWalkingRouteResult(MKWalkingRouteResult result, int iError) {
  20. }
  21.  
  22. @Override
  23. public void onGetBusDetailResult(MKBusLineResult result, int iError) {
  24. }
  25.  
  26. @Override
  27. public void onGetSuggestionResult(MKSuggestionResult result, int iError) {
  28. }
  29. }

然后初始化MKSearch类:

  1. mMKSearch = new MKSearch();
  2. mMKSearch.init(mBMapMan, new MySearchListener());//注意,MKSearchListener只支持一个,以最后一次设置为准

POI搜索及PoiOverlay

POI搜索有三种方式,根据范围和检索词发起范围检索poiSearchInbounds,城市poi检索poiSearchInCity,周边检索poiSearchNearBy,以下以周边检索为例介绍如何进行检索并显示覆盖物PoiOverlay:

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

  1. mMKSearch.poiSearchNearBy("KFC", new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6)), 5000);

实现MySearchListener的onGetPoiResult,并展示检索结果:

  1. @Override
  2. public void onGetPoiResult(MKPoiResult result, int type, int iError) {
  3. if (result == null) {
  4. return;
  5. }
  6. PoiOverlay poioverlay = new PoiOverlay(MyMapActivity.this, mMapView);
  7. poioverlay.setData(result.getAllPoi());
  8. mMapView.getOverlays().add(poioverlay);
  9. mMapView.invalidate(); //刷新地图
  10. }

运行结果如下:app_poioverlay.png

place详情页面使用方法

当执行完poi检索后,我们会得到一个poi的列表,每个poi节点都有个uid属性,我们可以根据这个uid获取关于这个poi的一些更详细的信息.比如:评论、图片、商户描述等。

在AndroidManifest.xml中设置如下信息:

<activity android:name="com.baidu.mapapi.PlaceCaterActivity"          android:configChanges="orientation|keyboardHidden"          android:theme="@android:style/Theme.NoTitleBar"> </activity>

发起请求:

mSearch.poiDetailSearch(poi.uid);

详情页运行结果:place详情页面使用方法

具体实现详见相关下载提供的Demo(PoiSearch.java)

驾车路线搜索及RouteOverlay

检索从天安门到百度大厦的驾车路线:

  1. MKPlanNode start = new MKPlanNode();
  2. start.pt = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6));
  3. MKPlanNode end = new MKPlanNode();
  4. end.pt = new GeoPoint(40057031, 116307852);
  5. // 设置驾车路线搜索策略,时间优先、费用最少或距离最短
  6. mMKSearch.setDrivingPolicy(MKSearch.ECAR_TIME_FIRST);
  7. mMKSearch.drivingSearch(null, start, null, end);

实现MySearchListener的onGetDrivingRouteResult,并展示检索结果:

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

运行结果如下:app_routeoverlay_drive.png

步行路线搜索及RouteOverlay

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

公交换乘路线搜索及TransitOverlay

检索从天安门到百度大厦的公交换乘路线:

  1. MKPlanNode start = new MKPlanNode();
  2. start.pt = new GeoPoint((int) (39.915 * 1E6), (int) (116.404 * 1E6));
  3. MKPlanNode end = new MKPlanNode();
  4. end.pt = new GeoPoint(40057031, 116307852);
  5. // 设置乘车路线搜索策略,时间优先、最少换乘、最少步行距离或不含地铁
  6. mMKSearch.setTransitPolicy(MKSearch.EBUS_TRANSFER_FIRST);
  7. mMKSearch.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. mMapView.invalidate(); //刷新地图
  11. }

公交路线详情搜索

检索北京市公交路线717的poi,获取公交路线的uid:

  1. mSearch.poiSearchInCity("北京", "717");

实现MySearchListener的onGetPoiResult (MKPoiResult res, int type, int error),获得公交线路poi的uid,并根据此uid发起公交线路详情的检索:

  1. @Override
  2. public void onGetPoiResult(MKPoiResult res, int type, int error) {
  3. // 错误号可参考MKEvent中的定义
  4. if (error != 0 || res == null) {
  5. Toast.makeText(BusLineSearch.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
  6. return;
  7. }
  8.  
  9. // 找到公交路线poi node
  10. MKPoiInfo curPoi = null;
  11. int totalPoiNum = res.getNumPois();
  12. for( int idx = 0; idx < totalPoiNum; idx++ ) {
  13. curPoi = res.getPoi(idx);
  14. if ( 2 == curPoi.ePoiType ) {
  15. break;
  16. }
  17. }
  18.  
  19. mSearch.busLineSearch(mCityName, curPoi.uid);
  20. }

实现MySearchListener的onGetBusDetailResult(MKBusLineResult result, int iError),并展示搜索结果:

  1. public void onGetBusDetailResult(MKBusLineResult result, int iError) {
  2. if (iError != 0 || result == null) {
  3. Toast.makeText(BusLineSearch.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
  4. return;
  5. }
  6.  
  7. RouteOverlay routeOverlay = new RouteOverlay(BusLineSearch.this, mMapView);
  8. // 此处仅展示一个方案作为示例
  9. routeOverlay.setData(result.getBusRoute());
  10. mMapView.getOverlays().clear();
  11. mMapView.getOverlays().add(routeOverlay);
  12. mMapView.invalidate();
  13.  
  14. mMapView.getController().animateTo(result.getBusRoute().getStart());
  15. }

运行结果如下:app_buslineoverlay.png

地址信息查询

根据地理坐标查询地址信息:

  1. mMKSearch.reverseGeocode(new GeoPoint(40057031, 116307852)); //逆地址解析
  2. mMKSearch.geocode(key, city);//地址解析

geocode返回结果在MKSearchListener里的onGetAddrResult方法。

reverseGeocode返回结果在MKSearchListener里的onGetPoiResult方法。

  1. public void onGetAddrResult(MKAddrInfo res, int error) {
  2. if (error != 0) {
  3. String str = String.format("错误号:%d", error);
  4. Toast.makeText(GeoCoder.this, str, Toast.LENGTH_LONG).show();
  5. return;
  6. }
  7. mMapView.getController().animateTo(res.geoPt);
  8. String strInfo = String.format("纬度:%f 经度:%f\r\n", res.geoPt.getLatitudeE6()/1e6,res.geoPt.getLongitudeE6()/1e6);
  9. Toast.makeText(GeoCoder.this, strInfo, Toast.LENGTH_LONG).show();
  10. Drawable marker = getResources().getDrawable(R.drawable.iconmarka); //得到需要标在地图上的资源
  11. marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight()); //为maker定义位置和边界
  12. mMapView.getOverlays().clear();
  13. mMapView.getOverlays().add(new OverItemT(marker, GeoCoder.this, res.geoPt, res.strAddr));
  14. }
  1. public void onGetPoiResult(MKPoiResult res, int type, int error) {
  2. if (error != 0 || res == null) {
  3. Toast.makeText(GeoCoder.this, "解析失败", Toast.LENGTH_LONG).show();
  4. return;
  5. }
  6. if (res != null && res.getCurrentNumPois() > 0) {
  7. GeoPoint ptGeo = res.getAllPoi().get(0).pt;
  8. // 移动地图到该点:
  9. mMapView.getController().animateTo(ptGeo);
  10. String strInfo = String.format("纬度:%f 经度:%f\r\n", ptGeo.getLatitudeE6()/1e6,ptGeo.getLongitudeE6()/1e6);
  11. strInfo += "\r\n附近有:";
  12. for (int i = 0; i < res.getAllPoi().size(); i++) {
  13. strInfo += (res.getAllPoi().get(i).name + ";");
  14. }
  15. Toast.makeText(GeoCoder.this, strInfo, Toast.LENGTH_LONG).show();
  16. }
  17. }

在线建议查询

根据关键词查询在线建议词:

  1. mMKSearch.suggestionSearch("天安门"); //输入关键词

实现MySearchListener的onGetSuggestionResult,得到查询结果:

  1. ListView mSuggestionList = (ListView) findViewById(R.id.listView1);
  2. @Override
  3. public void onGetSuggestionResult(MKSuggestionResult res, int iError)
  4. {
  5. if (iError!= 0 || res == null) {
  6. Toast.makeText(PoiSearch.this, "抱歉,未找到结果", Toast.LENGTH_LONG).show();
  7. return;
  8. }
  9. int nSize = res.getSuggestionNum();
  10. String[] mStrSuggestions = new String[nSize];
  11. for (int i = 0; i < nSize; i++)
  12. {
  13. mStrSuggestions[i] = res.getSuggestion(i).city + res.getSuggestion(i).key;
  14. }
  15. ArrayAdapter<String> suggestionString = new ArrayAdapter<String>(PoiSearch.this, android.R.layout.simple_list_item_1,mStrSuggestions);
  16. mSuggestionList.setAdapter(suggestionString);
  17.  
  18. }

云检索模块的使用

首先实例化GeoSearchListener接口:

  1. @Override
  2. public void onGetGeoDetailsResult(DetailResult result, int type, int iError)
  3. {
  4. if (result != null) {
  5. if (result.content != null) {
  6. Toast.makeText(CloudSearchDemo.this, result.content.name, Toast.LENGTH_LONG).show();
  7. }
  8. else {
  9. Toast.makeText(CloudSearchDemo.this, "status:" + result.status, Toast.LENGTH_SHORT).show();
  10. }
  11. }
  12. }
  13. @Override
  14. public void onGetGeoResult(GeoSearchResult result, int type, int iError)
  15. {
  16. if (result != null && result.poiList != null && result.poiList.size() > 0) {
  17. CloudOverlay poiOverlay = new CloudOverlay(this);
  18. poiOverlay.setData(result.poiList);
  19. mMapView.getOverlays().clear();
  20. mMapView.getOverlays().add(poiOverlay);
  21. mMapView.invalidate();
  22. mMapView.getController().animateTo(new GeoPoint((int)(result.poiList.get(0).latitude * 1e6), (int)(result.poiList.get(0).longitude * 1e6)));
  23. }
  24. }

发起云检索请求:

  1. BoundsSearchInfo r = new BoundsSearchInfo();
  2. r.queryWords = "五中";
  3. r.ak = "输入你的ak";
  4. r.bounds = new Bounds(39843895, 116402214, 40956948, 116431457);
  5. r.filter.put("databox", 848);
  6. r.scope = 2;
  7. GeoSearchManager.getInstance().searchBounds(r);
  8. // 当请求成功时,会回调onGetGeoResult函数,执行打点操作。
  9. CloudOverlay poiOverlay = new CloudOverlay(this);
  10. poiOverlay.setData(result.poiList);
  11. mMapView.getOverlays().clear();
  12. mMapView.getOverlays().add(poiOverlay);
  13. mMapView.invalidate();

运行结果如下:云检索模块的使用