百度地图SDK for Android【检索服务】

时间:2023-03-09 04:29:16
百度地图SDK for Android【检索服务】

1搜索服务

百度地图SDK集成搜索服务包括:位置检索、周边检索、范围检索、公交检索、驾乘检索、步行检索,通过初始化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. //返回poi搜索结果
  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, intiError) {
  28. //返回联想词信息搜索结果
  29. }
  30. }

在MyMapActivity中添加成员变量:

  1. MKSearch mMKSearch = null;

然后在onCreate()中初始化:

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

2兴趣点(poi)搜索

2.1 范围检索

指在给定的一个矩形区域内,根据开发者设定的指定关键字,搜索兴趣点信息,所使用的方法为:poiSearchInbounds(String key, GeoPoint ptLB, GeoPoint ptRT);核心代码如下:

如要检索北京西站与北京北站为顶点所确定的距形区域内的KFC餐厅,使用以下代码发起检索:

  1. // 北京西站
  2. GeoPoint ptLB = new GeoPoint( (int)(39.901375 * 1E6),(int)(116.329099 * 1E6));
  3. // 北京北站
  4. GeoPoint ptRT = new GeoPoint( (int)(39.949404 * 1E6),(int)(116.360719 * 1E6));
  5. mMKSearch.poiSearchInbounds("KFC", ptLB, ptRT);

        Tips:想知道某个兴趣点的百度地图坐标吗?

        请移步百度地图坐标拾取系统http://api.map.baidu.com/lbsapi/getpoint/index.html

2.2 城市检索

城市检索,即在某一城市内搜索兴趣点信息。所使用的方法是:poiSearchInCity(String city, String key);核心代码如下:

如要检索北京的KFC餐厅,使用以下代码发起检索:

  1. mMKSearch.poiSearchInCity("北京", "KFC");

2.3 周边检索

周边检索指的是以指定坐标点为圆心,根据给定关键字查询一定半径范围内的全部兴趣点。使用方法:poiSearchNearBy(String key, GeoPoint pt, int radius);核心代码如下:

检索*周边5000米之内的KFC餐厅:

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

2.4 展示搜索结果

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

  1. @Override
  2. public void onGetPoiResult(MKPoiResult res, int type, int error) {
  3. // 错误号可参考MKEvent中的定义
  4. if ( error == MKEvent.ERROR_RESULT_NOT_FOUND){
  5. Toast.makeText(MyMapActivity.this, "抱歉,未找到结果",Toast.LENGTH_LONG).show();
  6. return ;
  7. }
  8. else if (error != 0 || res == null) {
  9. Toast.makeText(MyMapActivity.this, "搜索出错啦..", Toast.LENGTH_LONG).show();
  10. return;
  11. }
  12. // 将poi结果显示到地图上
  13. PoiOverlay poiOverlay = new PoiOverlay(MyMapActivity.this, mMapView);
  14. poiOverlay.setData(res.getAllPoi());
  15. mMapView.getOverlays().clear();
  16. mMapView.getOverlays().add(poiOverlay);
  17. mMapView.refresh();
  18. //当ePoiType为2(公交线路)或4(地铁线路)时, poi坐标为空
  19. for(MKPoiInfo info : res.getAllPoi() ){
  20. if ( info.pt != null ){
  21. mMapView.getController().animateTo(info.pt);
  22. break;
  23. }
  24. }
  25. }

运行结果如下图所示:

百度地图SDK for Android【检索服务】

3 地址信息查询

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

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

reverseGeocode返回结果在MKSearchListener里的onGetAddrResult方法,核心代码如下所示:

  1. public void onGetAddrResult(MKAddrInfo res, int error) {
  2. if (error != 0) {
  3. String str = String.format("错误号:%d", error);
  4. Toast.makeText(MyMapActivity.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(MyMapActivity.this, strInfo, Toast.LENGTH_LONG).show();
  10. }

geocode返回结果在MKSearchListener里的onGetPoiResult方法,核心代码如下:

  1. public void onGetPoiResult(MKPoiResult res, int type, int error) {
  2. if (error != 0 || res == null) {
  3. Toast.makeText(MyMapActivity.this, "解析失败", Toast.LENGTH_LONG).show();
  4. return;
  5. }
  6. if (res != null&&res.getCurrentNumPois() > 0) {
  7. GeoPointptGeo = res.getAllPoi().get(0).pt;  // 移动地图到该点:
  8. mMapView.getController().animateTo(ptGeo);
  9. String strInfo = String.format("纬度:%f 经度:%f\r\n", ptGeo.getLatitudeE6()/1e6,ptGeo.getLongitudeE6()/1e6);
  10. strInfo += "\r\n附近有:";
  11. for (int i = 0; i <res.getAllPoi().size(); i++) {
  12. strInfo += (res.getAllPoi().get(i).name + ";");
  13. }
  14. Toast.makeText(MyMapActivity.this, strInfo, Toast.LENGTH_LONG).show();
  15. }
  16. }

4 在线建议查询

根据关键词查询在线建议词,具体使用的方法为:suggestionSearch(String key),参数key为关键字;获取查询结果的方法需要实现MKSearchListener接口中的onGetSuggestionResult方法,核心代码如下所示:

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