在上篇文章中給大家簡單的搭建了百度地圖開發的基本環境,今天給大家來介紹介紹如何在地圖上面添加標註物
如對這篇文章有看不懂的地方,請轉戰到上一篇文章-->飛機直達
在正式開始之前先請大家注意,在轉載部落格的時候注意說明出處
今天給大家帶來四個方面的結束,
第一個:就是介紹地圖顯示交通訊息我們只需要添加一個代碼就可以完成
mapView.setTraffic(true);
第二個:給地圖顯示衛星地圖,同樣也很簡單,同樣只需要一句代碼
mapView.setSatellite(true);
第三個:給地圖設定一個標註物
這裡用到的是百度地圖提供的 Overlay 對象 -->
移步百度地圖官方API
首先寫一個內部類繼承自 Overlay 對象
//標註一個遮蓋物 public class MyOverlay extends Overlay{ //聲明一個地點 private GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6)); //聲明一個畫筆工具 private Paint paint = new Paint(); @Override public void draw(Canvas arg0, MapView arg1, boolean arg2) { super.draw(arg0, arg1, arg2); Point point = mapView.getProjection().toPixels(geoPoint, null); arg0.drawText("這裡是天安門", point.x,point.y,paint); } }
然後在onCreate方法中對該類進行調用
//v2.00 //給地圖對象設定標註物 //mapView.getOverlays().add(new MyOverlay()); //mapController.setZoom(12);
這樣就可以很簡單的使用標註物啦。
第四個:是如何給百度地圖設定多個標註物同樣的道理這裡我們用到的是百度地圖API裡面提供的 ItemizedOverlay 對象
同樣寫一個內部類繼承自 ItemizedOverlay 對象
//標註多個遮蓋物 public class MyOverLayItem extends ItemizedOverlay<OverlayItem>{ private List<OverlayItem> overlayItem = new ArrayList<OverlayItem>(); //定義一組座標 private double mLat1 = 39.90923;//經 private double mLot1 = 116.397428;//緯 private double mLat2 = 39.92923;//經 private double mLot2 = 116.377428;//緯 private double mLat3 = 39.94923;//經 private double mLot3 = 116.357428;//緯 private double mLat4 = 39.96923;//經 private double mLot4 = 116.337428;//緯 //用於在地圖上標識座標,用一個圖片標註public MyOverLayItem(Drawable drawable) {super(drawable);GeoPoint geoPoint1 = new GeoPoint((int)(mLat1*1E6),(int)(mLot1*1E6));GeoPoint geoPoint2 = new GeoPoint((int)(mLat2*1E6),(int)(mLot2*1E6));GeoPoint geoPoint3 = new GeoPoint((int)(mLat3*1E6),(int)(mLot3*1E6));GeoPoint geoPoint4 = new GeoPoint((int)(mLat4*1E6),(int)(mLot4*1E6));overlayItem.add(new OverlayItem(geoPoint1, "A", "這是第一個"));overlayItem.add(new OverlayItem(geoPoint2, "B", "這是第二個"));overlayItem.add(new OverlayItem(geoPoint3, "C", "這是第三個"));overlayItem.add(new OverlayItem(geoPoint4, "D", "這是第四個"));//重新整理地圖populate();}//返回指定的List集合中每一個座標@Overrideprotected OverlayItem createItem(int arg0) {return overlayItem.get(arg0);}@Overridepublic int size() {return overlayItem.size();} //當標註物被點擊的時候@Overridepublic boolean onTap(int i) {Toast.makeText(BaiDu_SuYiActivity.this, overlayItem.get(i).getSnippet(), 2).show();return true;} }
然後在onCreate方法中對該類進行調用
//v3.00 //給地圖設定多個標註物 //顯示標註的表徵圖 Drawable drawable = getResources().getDrawable(R.drawable.iconmarka); mapView.getOverlays().add(new MyOverLayItem(drawable));
資源全部類代碼
package com.shuaiyin.baidu;import java.util.ArrayList;import java.util.List;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Point;import android.graphics.drawable.Drawable;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.MKGeneralListener;import com.baidu.mapapi.MapActivity;import com.baidu.mapapi.MapController;import com.baidu.mapapi.MapView;import com.baidu.mapapi.Overlay;import com.baidu.mapapi.OverlayItem;/** * 讓百度地圖繼承MapActivity * @author shuaiyin * */public class BaiDu_SuYiActivity extends MapActivity {//添加百度地圖的相關控制項private MapView mapView;//載入百度地圖的引起private BMapManager bMapManager;//定義百度地圖的KEYprivate String key = "*我處理了*94B0429A4BEE30797E04D91B0211C4";//在百度地圖上添加相應的控制項private MapController mapController; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //首先執行個體化mapView mapView = (MapView) this.findViewById(R.id.bmapView); bMapManager = new BMapManager(BaiDu_SuYiActivity.this); //調用百度地圖載入KEY bMapManager.init(key, new MKGeneralListener() {@Overridepublic void onGetPermissionState(int arg0) {if(arg0 == 300){Toast.makeText(BaiDu_SuYiActivity.this, "您輸入的KEY有問題,請核實", 2).show();}}@Overridepublic void onGetNetworkState(int arg0) {}}); this.initMapActivity(bMapManager); //表示可以設定縮放功能 mapView.setBuiltInZoomControls(true); mapController = mapView.getController(); //V1.00 //在百度地圖上標註一個中心點 //GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6)); //給地圖對象設定一個中心點 //mapController.setCenter(geoPoint); //設定地圖的縮放層級 //mapController.setZoom(12); //顯示交通地圖 //mapView.setTraffic(true); //顯示衛星地圖 //mapView.setSatellite(true); //v2.00 //給地圖對象設定標註物 //mapView.getOverlays().add(new MyOverlay()); //mapController.setZoom(12); //v3.00 //給地圖設定多個標註物 //顯示標註的表徵圖 Drawable drawable = getResources().getDrawable(R.drawable.iconmarka); mapView.getOverlays().add(new MyOverLayItem(drawable)); } //標註一個遮蓋物 public class MyOverlay extends Overlay{ //聲明一個地點 private GeoPoint geoPoint = new GeoPoint((int)(39.915*1E6),(int)(116.404*1E6)); //聲明一個畫筆工具 private Paint paint = new Paint(); @Override public void draw(Canvas arg0, MapView arg1, boolean arg2) { super.draw(arg0, arg1, arg2); Point point = mapView.getProjection().toPixels(geoPoint, null); arg0.drawText("這裡是天安門", point.x,point.y,paint); } } //標註多個遮蓋物 public class MyOverLayItem extends ItemizedOverlay<OverlayItem>{ private List<OverlayItem> overlayItem = new ArrayList<OverlayItem>(); //定義一組座標 private double mLat1 = 39.90923;//經 private double mLot1 = 116.397428;//緯 private double mLat2 = 39.92923;//經 private double mLot2 = 116.377428;//緯 private double mLat3 = 39.94923;//經 private double mLot3 = 116.357428;//緯 private double mLat4 = 39.96923;//經 private double mLot4 = 116.337428;//緯 //用於在地圖上標識座標,用一個圖片標註public MyOverLayItem(Drawable drawable) {super(drawable);GeoPoint geoPoint1 = new GeoPoint((int)(mLat1*1E6),(int)(mLot1*1E6));GeoPoint geoPoint2 = new GeoPoint((int)(mLat2*1E6),(int)(mLot2*1E6));GeoPoint geoPoint3 = new GeoPoint((int)(mLat3*1E6),(int)(mLot3*1E6));GeoPoint geoPoint4 = new GeoPoint((int)(mLat4*1E6),(int)(mLot4*1E6));overlayItem.add(new OverlayItem(geoPoint1, "A", "這是第一個"));overlayItem.add(new OverlayItem(geoPoint2, "B", "這是第二個"));overlayItem.add(new OverlayItem(geoPoint3, "C", "這是第三個"));overlayItem.add(new OverlayItem(geoPoint4, "D", "這是第四個"));//重新整理地圖populate();}//返回指定的List集合中每一個座標@Overrideprotected OverlayItem createItem(int arg0) {return overlayItem.get(arg0);}@Overridepublic int size() {return overlayItem.size();} //當標註物被點擊的時候@Overridepublic boolean onTap(int i) {Toast.makeText(BaiDu_SuYiActivity.this, overlayItem.get(i).getSnippet(), 2).show();return true;} } @Override protected void onDestroy() { super.onDestroy(); if(bMapManager != null){ bMapManager.destroy(); bMapManager = null; } } @Override protected void onResume() { super.onResume(); if(bMapManager != null){ bMapManager.start(); } } @Override protected void onPause() { super.onPause(); if(bMapManager != null){ bMapManager.stop(); } } @Overrideprotected boolean isRouteDisplayed() {return false;}}
最後看看上面四個東西的
希望大家在看了我的部落格後,能跟我一起進步,大家加油,好好學習,天天向上。