[java] package com.baidu.map; import java.util.ArrayList; import java.util.List; 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.Location; import android.location.LocationManager; import android.os.Bundle; import android.widget.Toast; import com.baidu.mapapi.BMapManager; import com.baidu.mapapi.GeoPoint; import com.baidu.mapapi.ItemizedOverlay; import com.baidu.mapapi.LocationListener; import com.baidu.mapapi.MKAddrInfo; import com.baidu.mapapi.MKDrivingRouteResult; import com.baidu.mapapi.MKPoiResult; 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.OverlayItem; import com.baidu.mapapi.PoiOverlay; import com.baidu.mapapi.Projection; public class BmapActivity extends MapActivity { // 定義地圖引擎管理類 private BMapManager mapManager;// 定義搜尋服務類 private MapView mapView; private MapController mapController; LocationListener mLocationListener = null;// onResume時註冊此listener,onPause時需要Remove MyLocationOverlay mLocationOverlay = null; // 定位元影像層 private double dLat; private double dLon; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 初始化MapActivity mapManager = new BMapManager(getApplication()); // init方法的第一個參數需填入申請的APIKey mapManager.init("DE7BC8B377AFA895ED0C16F504B5C4FC5E3E149B", null); super.initMapActivity(mapManager); mapView = (MapView) findViewById(R.id.map_View); // 設定地圖模式為交通地圖 // mapView.setTraffic(true); // 設定啟用內建的縮放控制項 mapView.setBuiltInZoomControls(true); mapView.displayZoomControls(true); // 設定在縮放動畫過程中也顯示overlay,預設為不繪製 mapView.setDrawOverlayWhenZooming(true); mapController = mapView.getController(); mapController.setZoom(16); //定位自己的位置 myself(); // 添加定位元影像層 mLocationOverlay = new MyLocationOverlay(this, mapView); mapView.getOverlays().add(mLocationOverlay); Drawable marker = getResources().getDrawable(R.drawable.da_marker_red); marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());// Intrinsic固有 mapView.getOverlays().add(new MyItemizedOverlay(marker, this)); } // 同一類型覆蓋物的繪製 class MyItemizedOverlay extends ItemizedOverlay<OverlayItem> { // 屬性 private Drawable marker; private Context mContext; private List<OverlayItem> geoList = new ArrayList<OverlayItem>(); // 構造方法 public MyItemizedOverlay(Drawable marker, Context context) { super(boundCenterBottom(marker)); this.marker = marker; this.mContext = context; // 構造地理座標 GeoPoint p1 = new GeoPoint((int) (dLat * 1E6), (int) (dLon * 1E6)); geoList.add(new OverlayItem(p1, "P1", "這是我的當前位置")); populate();// 執行填充方法 } // 繪製方法 public void draw(Canvas canvas, MapView mapView, boolean shadow) { // 投影,用於螢幕像素點座標系統與地球經緯度點座標系統的轉換 Projection projection = mapView.getProjection(); for (int index = size() - 1; index >= 0; index--) { OverlayItem overlayItem = this.getItem(index); String title = overlayItem.getTitle(); Point point = projection.toPixels(overlayItem.getPoint(), null); Paint painttext = new Paint(); painttext.setColor(Color.BLACK); painttext.setTextSize(15); canvas.drawText(title, point.x - 30, point.y - 25, painttext); } super.draw(canvas, mapView, shadow); boundCenterBottom(marker); } // 新增成員方法 @Override protected OverlayItem createItem(int i) { return geoList.get(i); } @Override public int size() { return geoList.size(); } // 添加點擊事件 public boolean onTap(int i) { setFocus(geoList.get(i)); Toast.makeText(this.mContext, geoList.get(i).getSnippet(), Toast.LENGTH_LONG).show();// snippet片段 return true; } public boolean onTap(GeoPoint point, MapView mapView) { return super.onTap(point, mapView); } } // 定位到自己的位置 public void myself() { LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE); Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); if (l == null) { Toast.makeText(BmapActivity.this, "無法擷取自己的位置", Toast.LENGTH_SHORT) .show(); /* 預設 的位置 */ dLat = 34.824289; dLon = 113.689044; GeoPoint geoPoint = new GeoPoint((int) (dLat * 1e6), (int) (dLon * 1e6)); mapController.setCenter(geoPoint); } else { dLat = (int) (l.getLatitude() * 1E6); dLon = (int) (l.getLongitude() * 1E6); } } @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 { /** * * 根據經緯度搜尋地址資訊結果 * * @param result * 搜尋結果 * @param iError * 錯誤號碼 (0表示正確返回) */ public void onGetAddrResult(MKAddrInfo result, int iError) { } /** * 駕車路線搜尋結果 * * @param result * 搜尋結果 * @param iError * 錯誤號碼 */ public void onGetDrivingRouteResult(MKDrivingRouteResult result, int iError) { } /** * * POI搜尋結果(範圍檢索、城市POI檢索、周邊檢索) * @param result 搜尋結果 * * @param type * 返回結果類型(11,12,21:poi列表 7:城市列表) * @param iError * 錯誤號碼(0表示正確返回) */ public void onGetPoiResult(MKPoiResult result, int type, int iError) { if (result == null) { return; } // PoiOverlay是baidu map api提供的用於顯示POI的Overlay PoiOverlay poioverlay = new PoiOverlay(BmapActivity.this, mapView); // 設定搜尋到的POI資料 poioverlay.setData(result.getAllPoi()); // 在地圖上顯示PoiOverlay(將搜尋到的興趣點標註在地圖上) mapView.getOverlays().add(poioverlay); } /** * 公交轉乘路線搜尋結果 * * @param result 搜尋結果 * * * @param iError * 錯誤號碼(0表示正確返回) * */ public void onGetTransitRouteResult(MKTransitRouteResult result, int iError) { } /** * * 步行路線搜尋結果 * * * @param result * 搜尋結果 * * @param iError * 錯誤號碼(0表示正確返回) * */ public void onGetWalkingRouteResult(MKWalkingRouteResult result, int iError) { } } }