百度地图-实现POI的搜索(搜索周边)


百度地图-实现POI的搜索(搜索周边) :

  1. package com.lbsproject;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import Android.content.Context;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.graphics.Point;  
  10. import android.graphics.drawable.Drawable;  
  11. import android.location.Criteria;  
  12. import android.location.Location;  
  13. import android.location.LocationManager;  
  14. import android.os.Bundle;  
  15. import android.util.Log;  
  16. import android.view.Menu;  
  17. import android.view.MenuItem;  
  18. import android.view.MotionEvent;  
  19. import android.view.View;  
  20. import android.widget.Button;  
  21. import android.widget.EditText;  
  22. import android.widget.Toast;  
  23.   
  24. import com.baidu.mapapi.BMapManager;  
  25. import com.baidu.mapapi.GeoPoint;  
  26. import com.baidu.mapapi.LocationListener;  
  27. import com.baidu.mapapi.MKAddrInfo;  
  28. import com.baidu.mapapi.MKDrivingRouteResult;  
  29. import com.baidu.mapapi.MKPoiResult;  
  30. import com.baidu.mapapi.MKSearch;  
  31. import com.baidu.mapapi.MKSearchListener;  
  32. import com.baidu.mapapi.MKTransitRouteResult;  
  33. import com.baidu.mapapi.MKWalkingRouteResult;  
  34. import com.baidu.mapapi.MapActivity;  
  35. import com.baidu.mapapi.MapController;  
  36. import com.baidu.mapapi.MapView;  
  37. import com.baidu.mapapi.MyLocationOverlay;  
  38. import com.baidu.mapapi.Overlay;  
  39. import com.baidu.mapapi.PoiOverlay;  
  40. import com.baidu.mapapi.Projection;  
  41. import com.lbsproject.LBSProjectActivity.GetOverlay;  
  42.   
  43. public class LBSProjectActivity extends MapActivity {  
  44.     protected static final String TAG = null;  
  45.     private BMapManager mapManager;  
  46.     private MapView mapView;  
  47.     private MapController mapController;  
  48.     private MKSearch mSearch = null;   
  49.     private GeoPoint geoPoint = new GeoPoint(12208409537422006);;  
  50.     LocationListener mLocationListener = null;  
  51.     MyLocationOverlay mLocationOverlay = null;   
  52.     private EditText editCity;  
  53.     private EditText editGeoCodeKey;  
  54.     private EditText areaText;  
  55.     private GetOverlay getOverlay;  
  56.   
  57.     @Override  
  58.     public void onCreate(Bundle savedInstanceState) {  
  59.         super.onCreate(savedInstanceState);  
  60.         setContentView(R.layout.main);  
  61.       
  62.         getInitLoc();// 程序打开时候获取当前位置 显示地图上   
  63.         mapManager = new BMapManager(getApplication());  
  64.   
  65.         // init方法的第一个参数需填入申请的APIKey   
  66.         mapManager.init("2150650BE0DCF874994B845CEC7B60A0518E6AFE"null);  
  67.         super.initMapActivity(mapManager);  
  68.         mapView = (MapView) findViewById(R.id.mapView);  
  69.         mapView.setBuiltInZoomControls(true);  
  70.         mapView.setDrawOverlayWhenZooming(true);  
  71.   
  72.         // 添加定位图层   
  73.         mLocationOverlay = new MyLocationOverlay(this, mapView);  
  74.         mapView.getOverlays().add(mLocationOverlay);  
  75.         getOverlay = new GetOverlay();  
  76.         mapView.getOverlays().add(getOverlay);  
  77.   
  78.         gotoLocate();  
  79.   
  80.         Button buttonGetPos = (Button) findViewById(R.id.buttonGetPos);//获取"我的位置的方法"   
  81.         buttonGetPos.setOnClickListener(new View.OnClickListener() {  
  82.   
  83.             public void onClick(View v) {  
  84.                 getInitLoc();  
  85.                 gotoLocate();  
  86.             }  
  87.         });  
  88.   
  89.         editCity = (EditText) findViewById(R.id.editTextCity);  
  90.         editGeoCodeKey = (EditText) findViewById(R.id.editTextAera);  
  91.         Button buttonSearchCity = (Button) findViewById(R.id.buttonSearchCity);  
  92.         buttonSearchCity.setOnClickListener(new View.OnClickListener() {//点击按钮定位需要去的地方   
  93.   
  94.             @Override  
  95.             public void onClick(View v) {  
  96.   
  97.                 mSearch.geocode(editGeoCodeKey.getText().toString(), editCity  
  98.                         .getText().toString());  
  99.                 // mapView.getOverlays().add(getOverlay);   
  100.   
  101.             }  
  102.         });  
  103.         areaText = (EditText) findViewById(R.id.searchKeywords);  
  104.         Button buttonSearchArea = (Button) findViewById(R.id.buttonSearchArea);  
  105.         buttonSearchArea.setOnClickListener(new View.OnClickListener() {  
  106.   
  107.             @Override  
  108.             public void onClick(View v) {  
  109.   
  110.                   
  111.                 mSearch.poiSearchNearBy(areaText.getText().toString(),// 搜索XX附近5000米范围的XXX   
  112.                         geoPoint, 5000);  
  113.   
  114.             }  
  115.         });  
  116.   
  117.         // 注册定位事件   
  118.         mLocationListener = new LocationListener() {  
  119.             public void onLocationChanged(Location location) {  
  120.                 if (location != null) {  
  121.                     Log.i(TAG, "" + location.getLatitude());  
  122.                     Log.i(TAG, "" + location.getLongitude());  
  123.                     geoPoint = new GeoPoint(  
  124.                             (int) (location.getLatitude() * 1e6),  
  125.                             (int) (location.getLongitude() * 1e6));  
  126.                     gotoLocate();  
  127.                 }  
  128.             }  
  129.   
  130.         };  
  131.   
  132.         mSearch = new MKSearch();//搜索服务类   
  133.         mSearch.init(mapManager, new MKSearchListener() {  
  134.             public void onGetAddrResult(MKAddrInfo res, int error) {  
  135.                 if (error != 0) {  
  136.                     String str = String.format("错误号:%d", error);  
  137.                     Toast.makeText(LBSProjectActivity.this, str,  
  138.                             Toast.LENGTH_LONG).show();  
  139.                     return;  
  140.                 }  
  141.   
  142.                 mapView.getController().animateTo(res.geoPt);  
  143.   
  144. //              String strInfo = String.format("纬度:%f 经度:%f\r\n",   
  145. //                      res.geoPt.getLatitudeE6() / 1e6,   
  146. //                      res.geoPt.getLongitudeE6() / 1e6);   
  147.   
  148.                 geoPoint = res.geoPt;  
  149.   
  150.                 // Toast.makeText(LBSProjectActivity.this, strInfo,   
  151.                 // Toast.LENGTH_LONG).show();   
  152.                 Drawable marker = getResources().getDrawable(  
  153.                         R.drawable.iconmarka); // 得到需要标在地图上的资源   
  154.                 marker.setBounds(00, marker.getIntrinsicWidth(),  
  155.                         marker.getIntrinsicHeight()); // 为maker定义位置和边界   
  156.                 mapView.getOverlays().clear();  
  157.                 mapView.getOverlays().add(getOverlay);  
  158.                 mapView.getOverlays().add(  
  159.                         new OverItemT(marker, LBSProjectActivity.this,  
  160.                                 res.geoPt, res.strAddr));  
  161.             }  
  162.   
  163.             public void onGetPoiResult(MKPoiResult res, int type, int error) {  
  164.                 if (res == null) {  
  165.                     Log.d("onGetPoiResult""the onGetPoiResult res is " + type  
  166.                             + "__" + error);  
  167.                 } else  
  168.                     Log.d("onGetPoiResult",  
  169.                             "the onGetPoiResult res is "  
  170.                                     + res.getCurrentNumPois() + "__"  
  171.                                     + res.getNumPages() + "__"  
  172.                                     + res.getNumPois() + "__" + type + "__"  
  173.                                     + error);  
  174.   
  175.                 // 错误号可参考MKEvent中的定义   
  176.                 if (error != 0 || res == null) {  
  177.                     Log.d("onGetPoiResult""the onGetPoiResult res 0 ");  
  178.                     Toast.makeText(LBSProjectActivity.this"抱歉,未找到结果",  
  179.                             Toast.LENGTH_LONG).show();  
  180.                     return;  
  181.                 }  
  182.   
  183.                 ArrayList<MKPoiResult> poiResult = res.getMultiPoiResult();  
  184.                 if (poiResult != null)  
  185.                     Log.d("onGetPoiResult""the onGetPoiResult res 1__"  
  186.                             + poiResult.size());  
  187.                 // 将地图移动到第一个POI中心点   
  188.                 if (res.getCurrentNumPois() > 0) {  
  189.                     Log.d("onGetPoiResult""the onGetPoiResult res 2");  
  190.                     // 将poi结果显示到地图上   
  191.                     PoiOverlay poiOverlay = new PoiOverlay(  
  192.                             LBSProjectActivity.this, mapView);  
  193.                     poiOverlay.setData(res.getAllPoi());  
  194.                     mapView.getOverlays().clear();  
  195.                     mapView.getOverlays().add(getOverlay);  
  196.                     mapView.getOverlays().add(poiOverlay);  
  197.                     mapView.invalidate();  
  198.                     mapView.getController().animateTo(res.getPoi(0).pt);  
  199.                 } else if (res.getCityListNum() > 0) {  
  200.                     Log.d("onGetPoiResult""the onGetPoiResult res 3");  
  201.                     String strInfo = "在";  
  202.                     for (int i = 0; i < res.getCityListNum(); i++) {  
  203.                         strInfo += res.getCityListInfo(i).city;  
  204.                         strInfo += ",";  
  205.                     }  
  206.                     strInfo += "找到结果";  
  207.                     Toast.makeText(LBSProjectActivity.this, strInfo,  
  208.                             Toast.LENGTH_LONG).show();  
  209.                 }  
  210.   
  211.                 Log.d("onGetPoiResult""the onGetPoiResult res 4");  
  212.   
  213.             }  
  214.   
  215.             public void onGetDrivingRouteResult(MKDrivingRouteResult res,  
  216.                     int error) {  
  217.             }  
  218.   
  219.             public void onGetTransitRouteResult(MKTransitRouteResult res,  
  220.                     int error) {  
  221.             }  
  222.   
  223.             public void onGetWalkingRouteResult(MKWalkingRouteResult res,  
  224.                     int error) {  
  225.             }  
  226.   
  227.         });  
  228.   
  229.     }  
  230.   
  231.     private void gotoLocate() {// 获取所在位置   
  232.         Drawable marker = getResources().getDrawable(R.drawable.iconmarka); // 得到需要标在地图上的资源   
  233.         marker.setBounds(00, marker.getIntrinsicWidth(),  
  234.                 marker.getIntrinsicHeight()); // 为maker定义位置和边界   
  235.         mapView.getOverlays().clear();  
  236.         mapView.getOverlays().add(getOverlay);  
  237.         mapView.getOverlays().add(  
  238.                 new OverItemT(marker, LBSProjectActivity.this, geoPoint, ""));  
  239.   
  240.         mapView.getController().animateTo(geoPoint);  
  241.         mapController = mapView.getController();  
  242.         // 设置地图的中心   
  243.         mapController.setCenter(geoPoint);  
  244.         // 设置地图默认的缩放级别   
  245.         mapController.setZoom(16);  
  246.     }  
  247.   
  248.     private void getInitLoc() {// 初始化时候获取坐标   
  249.         try {  
  250.   
  251.             LocationManager locationManager;  
  252.             String context = Context.LOCATION_SERVICE;  
  253.             locationManager = (LocationManager) getSystemService(context);  
  254.             // String provider = LocationManager.GPS_PROVIDER;   
  255.   
  256.             Criteria criteria = new Criteria();  
  257.             criteria.setAccuracy(Criteria.ACCURACY_FINE);  
  258.             criteria.setAltitudeRequired(false);  
  259.             criteria.setBearingRequired(false);  
  260.             criteria.setCostAllowed(true);  
  261.             criteria.setPowerRequirement(Criteria.POWER_LOW);  
  262.             String provider = locationManager.getBestProvider(criteria, true);  
  263.             Location location = locationManager.getLastKnownLocation(provider);  
  264.             geoPoint = new GeoPoint((int) (location.getLatitude() * 1e6),  
  265.                     (int) (location.getLongitude() * 1e6));  
  266.         } catch (Exception e) {  
  267.             // TODO: handle exception   
  268.         }  
  269.     }  
  270.   
  271.     @Override  
  272.     protected boolean isRouteDisplayed() {  
  273.         return false;  
  274.     }  
  275.   
  276.     @Override  
  277.     protected void onDestroy() {  
  278.         if (mapManager != null) {  
  279.             // 程序退出前需调用此方法   
  280.             mapManager.destroy();  
  281.             mapManager = null;  
  282.         }  
  283.         super.onDestroy();  
  284.     }  
  285.   
  286.     @Override  
  287.     protected void onPause() {  
  288.         if (mapManager != null) {  
  289.             // 终止百度地图API   
  290.             mapManager.getLocationManager().removeUpdates(mLocationListener);  
  291.             mLocationOverlay.disableMyLocation();  
  292.             mLocationOverlay.disableCompass(); // 关闭指南针   
  293.             mapManager.stop();  
  294.         }  
  295.         super.onPause();  
  296.     }  
  297.   
  298.     @Override  
  299.     protected void onResume() {  
  300.         if (mapManager != null) {  
  301.             // 开启百度地图API   
  302.             // 注册定位事件,定位后将地图移动到定位点   
  303.             mapManager.getLocationManager().requestLocationUpdates(  
  304.                     mLocationListener);  
  305.             mLocationOverlay.enableMyLocation();  
  306.             mLocationOverlay.enableCompass(); // 打开指南针   
  307.             mapManager.start();  
  308.         }  
  309.         super.onResume();  
  310.     }  
  311.   
  312.     /** 
  313.      * * 实现MKSearchListener接口,用于实现异步搜索服务 * @author liufeng 
  314.      */  
  315.     public class MySearchListener implements MKSearchListener {  
  316.           
  317.         public void onGetAddrResult(MKAddrInfo result, int iError) {  
  318.         }  
  319.   
  320.         public void onGetDrivingRouteResult(MKDrivingRouteResult result,  
  321.                 int iError) {  
  322.         }  
  323.   
  324.         /** 
  325.          * * POI搜索结果(范围检索、城市POI检索、周边检索) * * @param result 搜索结果 * @param type 
  326.          * 返回结果类型(11,12,21:poi列表 7:城市列表) * @param iError 错误号(0表示正确返回) 
  327.          */  
  328.         @Override  
  329.         public void onGetPoiResult(MKPoiResult result, int type, int iError) {  
  330.             if (result == null) {  
  331.                 return;  
  332.             }  
  333.             // PoiOverlay是baidu map api提供的用于显示POI的Overlay   
  334.             PoiOverlay poioverlay = new PoiOverlay(LBSProjectActivity.this,  
  335.                     mapView);  
  336.             // 设置搜索到的POI数据   
  337.             poioverlay.setData(result.getAllPoi());  
  338.             // 在地图上显示PoiOverlay(将搜索到的兴趣点标注在地图上)   
  339.             mapView.getOverlays().add(poioverlay);  
  340.         }  
  341.   
  342.       
  343.         public void onGetTransitRouteResult(MKTransitRouteResult result,  
  344.                 int iError) {  
  345.         }  
  346.   
  347.           
  348.         public void onGetWalkingRouteResult(MKWalkingRouteResult result,  
  349.                 int iError) {  
  350.         }  
  351.     }  
  352.   
  353.     class GetOverlay extends Overlay {  
  354.         GeoPoint geo;  
  355.   
  356.         @Override  
  357.         public void draw(Canvas canvas, MapView gmapView, boolean arg2) {  
  358.             super.draw(canvas, mapView, arg2);  
  359.             if (geo == null) {  
  360.                 return;  
  361.             }  
  362.             Log.i("11111111111111111111", arg2 + "-------draw--");  
  363.         }  
  364.   
  365.         @Override  
  366.         public boolean onTap(GeoPoint geo, MapView arg1) {  
  367.             geoPoint = geo;  
  368.             Drawable marker = getResources().getDrawable(R.drawable.iconmarka); // 得到需要标在地图上的资源   
  369.             marker.setBounds(00, marker.getIntrinsicWidth(),  
  370.                     marker.getIntrinsicHeight()); // 为maker定义位置和边界   
  371.             mapView.getOverlays().clear();  
  372.             mapView.getOverlays().add(getOverlay);  
  373.             mapView.getOverlays()  
  374.                     .add(new OverItemT(marker, LBSProjectActivity.this,  
  375.                             geoPoint, ""));  
  376.             Log.i("11111111111111111111", geo.getLongitudeE6() / 1E6  
  377.                     + "----------" + geo.getLatitudeE6() / 1E6);  
  378.             return super.onTap(geo, arg1);  
  379.         }  
  380.   
  381.     }  
  382.   
  383.     private static final int TOOLBAR0 = 0;  
  384.     private static final int TOOLBAR1 = 1;  
  385.     private static final int TOOLBAR2 = 2;  
  386.     private static final int TOOLBAR3 = 3;  
  387.   
  388.     public boolean onCreateOptionsMenu(Menu menu) {  
  389.         menu.add(0, TOOLBAR0, 1"KTV").setIcon(  
  390.                 android.R.drawable.ic_btn_speak_now);  
  391.         menu.add(0, TOOLBAR1, 2"学校").setIcon(  
  392.                 android.R.drawable.ic_menu_myplaces);  
  393.         menu.add(0, TOOLBAR2, 3"餐厅").setIcon(  
  394.                 android.R.drawable.ic_menu_my_calendar);  
  395.         menu.add(0, TOOLBAR3, 4"公园").setIcon(  
  396.                 android.R.drawable.ic_menu_gallery);  
  397.         return super.onCreateOptionsMenu(menu);  
  398.     }  
  399.   
  400.     @Override  
  401.     public boolean onOptionsItemSelected(MenuItem item) {  
  402.         switch (item.getItemId()) {  
  403.         case 0:  
  404.             mSearch.poiSearchNearBy("KTV", geoPoint, 5000);//搜索ktv   
  405.             break;  
  406.         case 1:  
  407.             mSearch.poiSearchNearBy("学校", geoPoint, 5000);//.搜索学校   
  408.             break;  
  409.         case 2:  
  410.             mSearch.poiSearchNearBy("餐厅", geoPoint, 5000);//搜索餐厅   
  411.             break;  
  412.         case 3:  
  413.             mSearch.poiSearchNearBy("公园", geoPoint, 5000);//搜索公园   
  414.             break;  
  415.         }  
  416.   
  417.         return super.onOptionsItemSelected(item);  
  418.     }  
  419.   
  420. }  

更多Android相关信息见Android 专题页面 http://www.bkjia.com/topicnews.aspx?tid=11

相关内容