02.(最新版)百度地圖§基礎底圖BaiduMap

轉載請標明:http://blog.csdn.net/u012637501

一、基礎地圖SDK簡介    

    目前百度地圖SDK所提供的地圖等級爲3-19級,所包含的信息有建築物、道路、河流、學校、公園等內容。所有疊加或覆蓋到地圖的內容,我們統稱爲地圖覆蓋物。如標註、矢量圖形元素(包括:折線、多邊形和圓等)、定位圖標等。覆蓋物擁有自己的地理座標,當您拖動或縮放地圖時,它們會相應的移動。百度地圖SDK爲廣大開發者提供的基礎地圖和上面的各種覆蓋物元素,具有一定的層級壓蓋關係,具體如下(從下至上的順序):

1、基礎底圖(包括底圖、底圖道路、衛星圖等);

2、地形圖圖層(GroundOverlay);

3、熱力圖圖層(HeatMap);

4、實時路況圖圖層(BaiduMap.setTrafficEnabled(true););

5、百度城市熱力圖(BaiduMap.setBaiduHeatMapEnabled(true););

6、底圖標註(指的是底圖上面自帶的那些POI元素);

7、幾何圖形圖層(點、折線、弧線、圓、多邊形);

8、標註圖層(Marker),文字繪製圖層(Text);

9、指南針圖層(當地圖發生旋轉和視角變化時,默認出現在左上角的指南針);

10、定位圖層(BaiduMap.setMyLocationEnabled(true););

11、彈出窗圖層(InfoWindow);

12、自定義View(MapView.addView(View);)

二、BaiduMap類

1.作用:地圖控制器,定義BaiduMap地圖對象的操作方法與接口;

2.獲取BaiduMap實例

    BaiduMap mBaiduMap=null;

    mBaiduMap = mapView.getMap();

3.嵌套類(部分)與靜態常量

static BaiduMap.OnMapClickListener :地圖單擊事件監聽接口 

static BaiduMap.OnMapDoubleClickListener :地圖雙擊事件監聽接口 

static BaiduMap.OnMapLoadedCallback :地圖加載完成回調接口 

static BaiduMap.OnMapLongClickListener :地圖長按事件監聽接口 

static BaiduMap.OnMapTouchListener :用戶觸摸地圖時回調接口 

static BaiduMap.OnMarkerClickListener :地圖 Marker 覆蓋物點擊事件監聽接口 

static BaiduMap.OnMarkerDragListener :地圖 Marker 覆蓋物拖拽事件監聽接口 

static BaiduMap.OnMyLocationClickListener :地圖定位圖標點擊事件監聽接口 

static int MAP_TYPE_NORMAL:普通地圖模式常量, setMapType(int) 

static int MAP_TYPE_SATELLITE:衛星圖模式常量,setMapType(int)

4.常用方法

Overlay addOverlay(OverlayOptions options):向地圖添加一個 Overlay

void clear():清空地圖所有的 Overlay 覆蓋物以及 InfoWindow

MyLocationData getLocationData():獲取定位數據

void setMapType(int type):設置地圖類型 MAP_TYPE_NORMAL 普通圖; MAP_TYPE_SATELLITE 衛星圖

void setMaxAndMinZoomLevel(float max, float min):設置地圖最大以及最小縮放級別,地圖支持的最大最小級別分別爲[3-20]

void setOnMapClickListener(BaiduMap.OnMapClickListener listener):設置地圖單擊事件監聽者

void setTrafficEnabled(boolean enabled):設置是否打開交通圖層

void snapshot(BaiduMap.SnapshotReadyCallback callback):發起截圖請求

三、基礎地圖開發方法

1.地圖類型

    百度地圖Android SDK爲您提供了兩種類型的地圖資源(普通矢量地圖和衛星圖),我們可以利用BaiduMap中的mapType()方法來設置地圖類型。核心代碼如下:

  1. BaiduMap mBaiduMap = null;    
  2. mMapView = (MapView) findViewById(R.id.bmapView);  
  3. mBaiduMap = mMapView.getMap();                                       //獲取地圖控制器  
  4. mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);    //普通地圖  
  5. mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);//衛星地圖 

效果如下:


2.實時交通圖

當前,全國範圍內已支持多個城市實時路況查詢,且會陸續開通其他城市。在地圖上打開實時路況的核心代碼如下:

  1. BaiduMap mBaiduMap = null;
  2. mMapView = (MapView) findViewById(R.id.bmapView);  
  3. mBaiduMap = mMapView.getMap();  
  4. mBaiduMap.setTrafficEnabled(true);    //開啓交通圖

3.百度城市熱力圖

  1. BaiduMap mBaiduMap = null;
  2. mMapView = (MapView) findViewById(R.id.bmapView);  
  3. mBaiduMap = mMapView.getMap();  
  4. mBaiduMap.setBaiduHeatMapEnabled(true);//開啓交通圖  

效果如下:


四、源碼實現

(1)MainActivity.java

  1. package base.baidu.oncampus;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7. import android.widget.RadioButton;  
  8. import com.baidu.mapapi.SDKInitializer;  
  9. import com.baidu.mapapi.map.BaiduMap;  
  10. import com.baidu.mapapi.map.MapView;  
  11. public class MainActivity extends Activity {  
  12.     private MapView mapView = null;  
  13.     private RadioButton normalMap = null;  
  14.     private RadioButton sateliteMap = null;  
  15.     private Button rtMap = null;  
  16.     private Button heatMap = null;  
  17.     private BaiduMap baiduMap = null;  
  18.     private boolean rtBtnFlag = false;   //實時地圖按鈕標誌  
  19.     private boolean heatBtnFlag = false;     //城市熱力圖地圖按鈕標誌  
  20.     //初始化組件  
  21.     private void init()  
  22.     {  
  23.      mapView = (MapView)findViewById(R.id.bmapView);     //地圖控件引用  
  24.      normalMap = (RadioButton)findViewById(R.id.normalMap);  
  25.      sateliteMap = (RadioButton)findViewById(R.id.sateliteMap);  
  26.      rtMap = (Button)findViewById(R.id.rtMap);  
  27.      heatMap = (Button)findViewById(R.id.heatMap);  
  28.     }  
  29.  protected void onCreate(Bundle savedInstanceState) {  
  30.         super.onCreate(savedInstanceState);  
  31.         SDKInitializer.initialize(getApplicationContext()); //在使用SDK各組件之前初始化context信息,傳入AplicationContext  
  32.         setContentView(R.layout.main);  
  33.          this.init();  
  34.          baiduMap = mapView.getMap();    //獲取地圖控制器  
  35.          //1.使用普通矢量地圖  
  36.          normalMap.setOnClickListener(new OnClickListener() {  
  37.    public void onClick(View v) {  
  38.     baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);  
  39.    }  
  40.   });  
  41.          //2.使用衛星地圖  
  42.          sateliteMap.setOnClickListener(new OnClickListener() {  
  43.    public void onClick(View v) {  
  44.      baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);  
  45.    }  
  46.   });  
  47.          //3.顯示實時交通圖  
  48.          rtMap.setOnClickListener(new OnClickListener() {  
  49.    public void onClick(View v) {  
  50.     if(rtBtnFlag == false)  
  51.     {  
  52.      baiduMap.setTrafficEnabled(true);  
  53.      rtBtnFlag=true;  
  54.     }  
  55.     else  
  56.     {  
  57.      baiduMap.setTrafficEnabled(false);  
  58.      rtBtnFlag=false;  
  59.     }  
  60.    }  
  61.   });  
  62.            
  63.    //4.城市熱力圖  
  64.    heatMap.setOnClickListener(new OnClickListener() {  
  65.    public void onClick(View v) {  
  66.     if(heatBtnFlag == false)  
  67.     {  
  68.      baiduMap.setBaiduHeatMapEnabled(true);  
  69.      heatBtnFlag=true;  
  70.     }  
  71.     else  
  72.     {  
  73.      baiduMap.setBaiduHeatMapEnabled(false);  
  74.      heatBtnFlag=false;  
  75.     }  
  76.    }  
  77.   });  
  78.  }  
  79.  @Override  
  80.  protected void onDestroy() {  
  81.   mapView.onDestroy();   //關閉百度地圖  
  82.   super.onDestroy();  
  83.  }  
  84.  @Override  
  85.  protected void onPause() {  
  86.   mapView.onPause();     //暫停使用百度地圖  
  87.   super.onPause();  
  88.  }  
  89.  @Override  
  90.  protected void onResume() {  
  91.   mapView.onResume();    //恢復使用百度地圖  
  92.   super.onResume();  
  93.  }  
  94.    
  95.  

(2)AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="base.baidu.oncampus"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.     <!-- 使用權限 -->  
  7.  <uses-permission android:name="android.permission.GET_ACCOUNTS" />  
  8.  <uses-permission android:name="android.permission.USE_CREDENTIALS" />  
  9.  <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />  
  10.  <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />  
  11.  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  12.  <uses-permission android:name="android.permission.INTERNET" />  
  13.  <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />  
  14.  <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  
  15.  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  16.  <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  17.  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  18.  <uses-permission android:name="android.permission.BROADCAST_STICKY" />  
  19.  <uses-permission android:name="android.permission.WRITE_SETTINGS" />  
  20.    <!-- 添加對屏幕的支持 -->  
  21.  <supports-screens  
  22.          android:largeScreens="true"  
  23.    android:normalScreens="true" android:smallScreens="true"  
  24.    android:resizeable="true" android:anyDensity="true"/>  
  25.     <uses-sdk  
  26.         android:minSdkVersion="8"  
  27.         android:targetSdkVersion="19" />  
  28.     <application  
  29.         android:allowBackup="true"  
  30.         android:icon="@drawable/ic_launcher"  
  31.         android:label="@string/app_name"  
  32.         android:theme="@style/AppTheme" >  
  33.         <!-- 開發密匙 -->  
  34.         <meta-data  
  35.         android:name="com.baidu.lbsapi.API_KEY"  
  36.         android:value="RtDd41VBHQLPeFIS5YjNK1y7" />  
  37.          
  38.         <activity  
  39.             android:name=".MainActivity"  
  40.             android:label="@string/app_name" >  
  41.             <intent-filter>  
  42.                 <action android:name="android.intent.action.MAIN" />  
  43.                 <category android:name="android.intent.category.LAUNCHER" />  
  44.             </intent-filter>  
  45.         </activity>  
  46.     </application>  
  47. </manifest>  

(3)佈局文件main.xml

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.     <LinearLayout  
  7.         android:layout_width="match_parent"  
  8.         android:layout_height="wrap_content"  
  9.         android:layout_marginTop="5dp"  
  10.         android:layout_marginBottom="5dp"  
  11.         android:orientation="horizontal">  
  12.         <RadioGroup  
  13.             android:layout_width="wrap_content"  
  14.             android:layout_height="match_parent"  
  15.             android:gravity="start|center_vertical"  
  16.             android:orientation="vertical">  
  17.         <RadioButton  
  18.             android:id="@+id/normalMap"  
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:checked="true"  
  22.             android:textSize="14sp"  
  23.             android:text="普通地圖"/>  
  24.         <RadioButton  
  25.             android:id="@+id/sateliteMap"  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:textSize="14sp"  
  29.             android:checked="false"  
  30.             android:text="衛星地圖" />  
  31.         </RadioGroup>  
  32.          
  33.         <Button  
  34.             android:id="@+id/rtMap"  
  35.             android:layout_width="wrap_content"  
  36.             android:layout_height="wrap_content"  
  37.             android:layout_marginStart="5dp"  
  38.             android:layout_marginLeft="5dp"  
  39.             android:textSize="12sp"  
  40.             android:text="實時交通"/>  
  41.          <Button  
  42.             android:id="@+id/heatMap"  
  43.             android:layout_width="wrap_content"  
  44.             android:layout_height="wrap_content"  
  45.             android:layout_marginStart="5dp"  
  46.             android:layout_marginLeft="5dp"  
  47.             android:textSize="12sp"  
  48.             android:text="城市熱力圖"/>  
  49.     </LinearLayout>  
  50.      
  51.  <com.baidu.mapapi.map.MapView  
  52.      android:id="@+id/bmapView"  
  53.      android:layout_width="fill_parent"  
  54.      android:layout_height="fill_parent"  
  55.      android:clickable="true"/>  
  56. </LinearLayout>  
發佈了142 篇原創文章 · 獲贊 19 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章