百度地圖-實現POI的搜索(搜索周邊) :
- package com.lbsproject;
-
- import java.util.ArrayList;
-
- import Android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Point;
- import android.graphics.drawable.Drawable;
- import android.location.Criteria;
- import android.location.Location;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
-
- import com.baidu.mapapi.BMapManager;
- import com.baidu.mapapi.GeoPoint;
- import com.baidu.mapapi.LocationListener;
- 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;
- import com.baidu.mapapi.MyLocationOverlay;
- import com.baidu.mapapi.Overlay;
- import com.baidu.mapapi.PoiOverlay;
- import com.baidu.mapapi.Projection;
- import com.lbsproject.LBSProjectActivity.GetOverlay;
-
- public class LBSProjectActivity extends MapActivity {
- protected static final String TAG = null;
- private BMapManager mapManager;
- private MapView mapView;
- private MapController mapController;
- private MKSearch mSearch = null;
- private GeoPoint geoPoint = new GeoPoint(122084095, 37422006);;
- LocationListener mLocationListener = null;
- MyLocationOverlay mLocationOverlay = null;
- private EditText editCity;
- private EditText editGeoCodeKey;
- private EditText areaText;
- private GetOverlay getOverlay;
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
-
- getInitLoc();// 程序打開時候獲取當前位置 顯示地圖上
- mapManager = new BMapManager(getApplication());
-
- // init方法的第一個參數需填入申請的APIKey
- mapManager.init("2150650BE0DCF874994B845CEC7B60A0518E6AFE", null);
- super.initMapActivity(mapManager);
- mapView = (MapView) findViewById(R.id.mapView);
- mapView.setBuiltInZoomControls(true);
- mapView.setDrawOverlayWhenZooming(true);
-
- // 添加定位圖層
- mLocationOverlay = new MyLocationOverlay(this, mapView);
- mapView.getOverlays().add(mLocationOverlay);
- getOverlay = new GetOverlay();
- mapView.getOverlays().add(getOverlay);
-
- gotoLocate();
-
- Button buttonGetPos = (Button) findViewById(R.id.buttonGetPos);//獲取"我的位置的方法"
- buttonGetPos.setOnClickListener(new View.OnClickListener() {
-
- public void onClick(View v) {
- getInitLoc();
- gotoLocate();
- }
- });
-
- editCity = (EditText) findViewById(R.id.editTextCity);
- editGeoCodeKey = (EditText) findViewById(R.id.editTextAera);
- Button buttonSearchCity = (Button) findViewById(R.id.buttonSearchCity);
- buttonSearchCity.setOnClickListener(new View.OnClickListener() {//點擊按鈕定位需要去的地方
-
- @Override
- public void onClick(View v) {
-
- mSearch.geocode(editGeoCodeKey.getText().toString(), editCity
- .getText().toString());
- // mapView.getOverlays().add(getOverlay);
-
- }
- });
- areaText = (EditText) findViewById(R.id.searchKeywords);
- Button buttonSearchArea = (Button) findViewById(R.id.buttonSearchArea);
- buttonSearchArea.setOnClickListener(new View.OnClickListener() {
-
- @Override
- public void onClick(View v) {
-
-
- mSearch.poiSearchNearBy(areaText.getText().toString(),// 搜索XX附近5000米范圍的XXX
- geoPoint, 5000);
-
- }
- });
-
- // 注冊定位事件
- mLocationListener = new LocationListener() {
- public void onLocationChanged(Location location) {
- if (location != null) {
- Log.i(TAG, "" + location.getLatitude());
- Log.i(TAG, "" + location.getLongitude());
- geoPoint = new GeoPoint(
- (int) (location.getLatitude() * 1e6),
- (int) (location.getLongitude() * 1e6));
- gotoLocate();
- }
- }
-
- };
-
- mSearch = new MKSearch();//搜索服務類
- mSearch.init(mapManager, new MKSearchListener() {
- public void onGetAddrResult(MKAddrInfo res, int error) {
- if (error != 0) {
- String str = String.format("錯誤號:%d", error);
- Toast.makeText(LBSProjectActivity.this, str,
- Toast.LENGTH_LONG).show();
- return;
- }
-
- mapView.getController().animateTo(res.geoPt);
-
- // String strInfo = String.format("緯度:%f 經度:%f\r\n",
- // res.geoPt.getLatitudeE6() / 1e6,
- // res.geoPt.getLongitudeE6() / 1e6);
-
- geoPoint = res.geoPt;
-
- // Toast.makeText(LBSProjectActivity.this, strInfo,
- // Toast.LENGTH_LONG).show();
- Drawable marker = getResources().getDrawable(
- R.drawable.iconmarka); // 得到需要標在地圖上的資源
- marker.setBounds(0, 0, marker.getIntrinsicWidth(),
- marker.getIntrinsicHeight()); // 為maker定義位置和邊界
- mapView.getOverlays().clear();
- mapView.getOverlays().add(getOverlay);
- mapView.getOverlays().add(
- new OverItemT(marker, LBSProjectActivity.this,
- res.geoPt, res.strAddr));
- }
-
- public void onGetPoiResult(MKPoiResult res, int type, int error) {
- if (res == null) {
- Log.d("onGetPoiResult", "the onGetPoiResult res is " + type
- + "__" + error);
- } else
- Log.d("onGetPoiResult",
- "the onGetPoiResult res is "
- + res.getCurrentNumPois() + "__"
- + res.getNumPages() + "__"
- + res.getNumPois() + "__" + type + "__"
- + error);
-
- // 錯誤號可參考MKEvent中的定義
- if (error != 0 || res == null) {
- Log.d("onGetPoiResult", "the onGetPoiResult res 0 ");
- Toast.makeText(LBSProjectActivity.this, "抱歉,未找到結果",
- Toast.LENGTH_LONG).show();
- return;
- }
-
- ArrayList<MKPoiResult> poiResult = res.getMultiPoiResult();
- if (poiResult != null)
- Log.d("onGetPoiResult", "the onGetPoiResult res 1__"
- + poiResult.size());
- // 將地圖移動到第一個POI中心點
- if (res.getCurrentNumPois() > 0) {
- Log.d("onGetPoiResult", "the onGetPoiResult res 2");
- // 將poi結果顯示到地圖上
- PoiOverlay poiOverlay = new PoiOverlay(
- LBSProjectActivity.this, mapView);
- poiOverlay.setData(res.getAllPoi());
- mapView.getOverlays().clear();
- mapView.getOverlays().add(getOverlay);
- mapView.getOverlays().add(poiOverlay);
- mapView.invalidate();
- mapView.getController().animateTo(res.getPoi(0).pt);
- } else if (res.getCityListNum() > 0) {
- Log.d("onGetPoiResult", "the onGetPoiResult res 3");
- String strInfo = "在";
- for (int i = 0; i < res.getCityListNum(); i++) {
- strInfo += res.getCityListInfo(i).city;
- strInfo += ",";
- }
- strInfo += "找到結果";
- Toast.makeText(LBSProjectActivity.this, strInfo,
- Toast.LENGTH_LONG).show();
- }
-
- Log.d("onGetPoiResult", "the onGetPoiResult res 4");
-
- }
-
- public void onGetDrivingRouteResult(MKDrivingRouteResult res,
- int error) {
- }
-
- public void onGetTransitRouteResult(MKTransitRouteResult res,
- int error) {
- }
-
- public void onGetWalkingRouteResult(MKWalkingRouteResult res,
- int error) {
- }
-
- });
-
- }
-
- private void gotoLocate() {// 獲取所在位置
- Drawable marker = getResources().getDrawable(R.drawable.iconmarka); // 得到需要標在地圖上的資源
- marker.setBounds(0, 0, marker.getIntrinsicWidth(),
- marker.getIntrinsicHeight()); // 為maker定義位置和邊界
- mapView.getOverlays().clear();
- mapView.getOverlays().add(getOverlay);
- mapView.getOverlays().add(
- new OverItemT(marker, LBSProjectActivity.this, geoPoint, ""));
-
- mapView.getController().animateTo(geoPoint);
- mapController = mapView.getController();
- // 設置地圖的中心
- mapController.setCenter(geoPoint);
- // 設置地圖默認的縮放級別
- mapController.setZoom(16);
- }
-
- private void getInitLoc() {// 初始化時候獲取坐標
- try {
-
- LocationManager locationManager;
- String context = Context.LOCATION_SERVICE;
- locationManager = (LocationManager) getSystemService(context);
- // String provider = LocationManager.GPS_PROVIDER;
-
- Criteria criteria = new Criteria();
- criteria.setAccuracy(Criteria.ACCURACY_FINE);
- criteria.setAltitudeRequired(false);
- criteria.setBearingRequired(false);
- criteria.setCostAllowed(true);
- criteria.setPowerRequirement(Criteria.POWER_LOW);
- String provider = locationManager.getBestProvider(criteria, true);
- Location location = locationManager.getLastKnownLocation(provider);
- geoPoint = new GeoPoint((int) (location.getLatitude() * 1e6),
- (int) (location.getLongitude() * 1e6));
- } catch (Exception e) {
- // TODO: handle exception
- }
- }
-
- @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) {
- // 終止百度地圖API
- mapManager.getLocationManager().removeUpdates(mLocationListener);
- mLocationOverlay.disableMyLocation();
- mLocationOverlay.disableCompass(); // 關閉指南針
- mapManager.stop();
- }
- super.onPause();
- }
-
- @Override
- protected void onResume() {
- if (mapManager != null) {
- // 開啟百度地圖API
- // 注冊定位事件,定位後將地圖移動到定位點
- mapManager.getLocationManager().requestLocationUpdates(
- mLocationListener);
- mLocationOverlay.enableMyLocation();
- mLocationOverlay.enableCompass(); // 打開指南針
- mapManager.start();
- }
- super.onResume();
- }
-
- /**
- * * 實現MKSearchListener接口,用於實現異步搜索服務 * @author liufeng
- */
- public class MySearchListener implements MKSearchListener {
-
- public void onGetAddrResult(MKAddrInfo result, int iError) {
- }
-
- public void onGetDrivingRouteResult(MKDrivingRouteResult result,
- int iError) {
- }
-
- /**
- * * POI搜索結果(范圍檢索、城市POI檢索、周邊檢索) * * @param result 搜索結果 * @param type
- * 返回結果類型(11,12,21:poi列表 7:城市列表) * @param iError 錯誤號(0表示正確返回)
- */
- @Override
- public void onGetPoiResult(MKPoiResult result, int type, int iError) {
- if (result == null) {
- return;
- }
- // PoiOverlay是baidu map api提供的用於顯示POI的Overlay
- PoiOverlay poioverlay = new PoiOverlay(LBSProjectActivity.this,
- mapView);
- // 設置搜索到的POI數據
- poioverlay.setData(result.getAllPoi());
- // 在地圖上顯示PoiOverlay(將搜索到的興趣點標注在地圖上)
- mapView.getOverlays().add(poioverlay);
- }
-
-
- public void onGetTransitRouteResult(MKTransitRouteResult result,
- int iError) {
- }
-
-
- public void onGetWalkingRouteResult(MKWalkingRouteResult result,
- int iError) {
- }
- }
-
- class GetOverlay extends Overlay {
- GeoPoint geo;
-
- @Override
- public void draw(Canvas canvas, MapView gmapView, boolean arg2) {
- super.draw(canvas, mapView, arg2);
- if (geo == null) {
- return;
- }
- Log.i("11111111111111111111", arg2 + "-------draw--");
- }
-
- @Override
- public boolean onTap(GeoPoint geo, MapView arg1) {
- geoPoint = geo;
- Drawable marker = getResources().getDrawable(R.drawable.iconmarka); // 得到需要標在地圖上的資源
- marker.setBounds(0, 0, marker.getIntrinsicWidth(),
- marker.getIntrinsicHeight()); // 為maker定義位置和邊界
- mapView.getOverlays().clear();
- mapView.getOverlays().add(getOverlay);
- mapView.getOverlays()
- .add(new OverItemT(marker, LBSProjectActivity.this,
- geoPoint, ""));
- Log.i("11111111111111111111", geo.getLongitudeE6() / 1E6
- + "----------" + geo.getLatitudeE6() / 1E6);
- return super.onTap(geo, arg1);
- }
-
- }
-
- private static final int TOOLBAR0 = 0;
- private static final int TOOLBAR1 = 1;
- private static final int TOOLBAR2 = 2;
- private static final int TOOLBAR3 = 3;
-
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add(0, TOOLBAR0, 1, "KTV").setIcon(
- android.R.drawable.ic_btn_speak_now);
- menu.add(0, TOOLBAR1, 2, "學校").setIcon(
- android.R.drawable.ic_menu_myplaces);
- menu.add(0, TOOLBAR2, 3, "餐廳").setIcon(
- android.R.drawable.ic_menu_my_calendar);
- menu.add(0, TOOLBAR3, 4, "公園").setIcon(
- android.R.drawable.ic_menu_gallery);
- return super.onCreateOptionsMenu(menu);
- }
-
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- switch (item.getItemId()) {
- case 0:
- mSearch.poiSearchNearBy("KTV", geoPoint, 5000);//搜索ktv
- break;
- case 1:
- mSearch.poiSearchNearBy("學校", geoPoint, 5000);//.搜索學校
- break;
- case 2:
- mSearch.poiSearchNearBy("餐廳", geoPoint, 5000);//搜索餐廳
- break;
- case 3:
- mSearch.poiSearchNearBy("公園", geoPoint, 5000);//搜索公園
- break;
- }
-
- return super.onOptionsItemSelected(item);
- }
-
- }
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11