Android學習筆記之百度地圖(周邊檢索poiSearchNearBy跳轉頁面並輸出搜尋結果)
阿新 • • 發佈:2018-12-22
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只支援一個,以最後一次設定為準 //搜尋山東科技大學附近(5000)的KTV if (mKSearch.poiSearchNearBy("KTV", new GeoPoint( (int) (36.001618315221194 * 1E6), (int) (120.11934041976929 * 1E6)), 5000) == -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(); }}