轉載請標明: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()方法來設置地圖類型。核心代碼如下:
- BaiduMap mBaiduMap = null;
- mMapView = (MapView) findViewById(R.id.bmapView);
- mBaiduMap = mMapView.getMap(); //獲取地圖控制器
- mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); //普通地圖
- mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);//衛星地圖
效果如下:
2.實時交通圖
當前,全國範圍內已支持多個城市實時路況查詢,且會陸續開通其他城市。在地圖上打開實時路況的核心代碼如下:
- BaiduMap mBaiduMap = null;
- mMapView = (MapView) findViewById(R.id.bmapView);
- mBaiduMap = mMapView.getMap();
- mBaiduMap.setTrafficEnabled(true); //開啓交通圖
3.百度城市熱力圖
- BaiduMap mBaiduMap = null;
- mMapView = (MapView) findViewById(R.id.bmapView);
- mBaiduMap = mMapView.getMap();
- mBaiduMap.setBaiduHeatMapEnabled(true);//開啓交通圖
效果如下:
四、源碼實現
(1)MainActivity.java
- package base.baidu.oncampus;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.RadioButton;
- import com.baidu.mapapi.SDKInitializer;
- import com.baidu.mapapi.map.BaiduMap;
- import com.baidu.mapapi.map.MapView;
- public class MainActivity extends Activity {
- private MapView mapView = null;
- private RadioButton normalMap = null;
- private RadioButton sateliteMap = null;
- private Button rtMap = null;
- private Button heatMap = null;
- private BaiduMap baiduMap = null;
- private boolean rtBtnFlag = false; //實時地圖按鈕標誌
- private boolean heatBtnFlag = false; //城市熱力圖地圖按鈕標誌
- //初始化組件
- private void init()
- {
- mapView = (MapView)findViewById(R.id.bmapView); //地圖控件引用
- normalMap = (RadioButton)findViewById(R.id.normalMap);
- sateliteMap = (RadioButton)findViewById(R.id.sateliteMap);
- rtMap = (Button)findViewById(R.id.rtMap);
- heatMap = (Button)findViewById(R.id.heatMap);
- }
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- SDKInitializer.initialize(getApplicationContext()); //在使用SDK各組件之前初始化context信息,傳入AplicationContext
- setContentView(R.layout.main);
- this.init();
- baiduMap = mapView.getMap(); //獲取地圖控制器
- //1.使用普通矢量地圖
- normalMap.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
- }
- });
- //2.使用衛星地圖
- sateliteMap.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE);
- }
- });
- //3.顯示實時交通圖
- rtMap.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- if(rtBtnFlag == false)
- {
- baiduMap.setTrafficEnabled(true);
- rtBtnFlag=true;
- }
- else
- {
- baiduMap.setTrafficEnabled(false);
- rtBtnFlag=false;
- }
- }
- });
- //4.城市熱力圖
- heatMap.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- if(heatBtnFlag == false)
- {
- baiduMap.setBaiduHeatMapEnabled(true);
- heatBtnFlag=true;
- }
- else
- {
- baiduMap.setBaiduHeatMapEnabled(false);
- heatBtnFlag=false;
- }
- }
- });
- }
- @Override
- protected void onDestroy() {
- mapView.onDestroy(); //關閉百度地圖
- super.onDestroy();
- }
- @Override
- protected void onPause() {
- mapView.onPause(); //暫停使用百度地圖
- super.onPause();
- }
- @Override
- protected void onResume() {
- mapView.onResume(); //恢復使用百度地圖
- super.onResume();
- }
- }
(2)AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="base.baidu.oncampus"
- android:versionCode="1"
- android:versionName="1.0" >
- <!-- 使用權限 -->
- <uses-permission android:name="android.permission.GET_ACCOUNTS" />
- <uses-permission android:name="android.permission.USE_CREDENTIALS" />
- <uses-permission android:name="android.permission.MANAGE_ACCOUNTS" />
- <uses-permission android:name="android.permission.AUTHENTICATE_ACCOUNTS" />
- <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="com.android.launcher.permission.READ_SETTINGS" />
- <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
- <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
- <uses-permission android:name="android.permission.READ_PHONE_STATE" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
- <uses-permission android:name="android.permission.BROADCAST_STICKY" />
- <uses-permission android:name="android.permission.WRITE_SETTINGS" />
- <!-- 添加對屏幕的支持 -->
- <supports-screens
- android:largeScreens="true"
- android:normalScreens="true" android:smallScreens="true"
- android:resizeable="true" android:anyDensity="true"/>
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="19" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <!-- 開發密匙 -->
- <meta-data
- android:name="com.baidu.lbsapi.API_KEY"
- android:value="RtDd41VBHQLPeFIS5YjNK1y7" />
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
(3)佈局文件main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_marginTop="5dp"
- android:layout_marginBottom="5dp"
- android:orientation="horizontal">
- <RadioGroup
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:gravity="start|center_vertical"
- android:orientation="vertical">
- <RadioButton
- android:id="@+id/normalMap"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:checked="true"
- android:textSize="14sp"
- android:text="普通地圖"/>
- <RadioButton
- android:id="@+id/sateliteMap"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="14sp"
- android:checked="false"
- android:text="衛星地圖" />
- </RadioGroup>
- <Button
- android:id="@+id/rtMap"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="5dp"
- android:layout_marginLeft="5dp"
- android:textSize="12sp"
- android:text="實時交通"/>
- <Button
- android:id="@+id/heatMap"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="5dp"
- android:layout_marginLeft="5dp"
- android:textSize="12sp"
- android:text="城市熱力圖"/>
- </LinearLayout>
- <com.baidu.mapapi.map.MapView
- android:id="@+id/bmapView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:clickable="true"/>
- </LinearLayout>