編輯:關於 Android 程式設計
一張地圖不只有「地圖」這一種樣貌。切換衛星影像、疊加實時路況、顯示城市熱力分布——這些效果在百度地圖 Android SDK 裡都只要一行屬性設定就能搞定。本文帶你看懂 3.7.1 版提供的三種圖層能力。
3.7.1 版提供兩種底圖資源:普通矢量地圖與衛星圖。透過 BaiduMap 的 MapType 屬性即可切換:
mMapView = FindViewById<TextureMapView>(Resource.Id.bmapView); mBaiduMap = mMapView.Map; //設置底圖顯示模式:普通地圖 mBaiduMap.MapType = BaiduMap.MapTypeNormal; //設置底圖顯示模式:衛星地圖 mBaiduMap.MapType = BaiduMap.MapTypeSatellite;
截至當時,全國已有 31 個城市開通了實時路況查詢,包含南京、廣州、重慶、東莞、長春、台州、福州、金華、北京、常州、杭州、溫州、大連、南昌、寧波、沈陽、中山、珠海、佛山、泉州、石家莊、成都、青島、深圳、武漢、烏魯木齊、長沙、上海、天津、無錫、廈門,後續還會持續擴充。開啟路況只需:
mMapView = FindViewById<TextureMapView>(Resource.Id.bmapView); mBaiduMap = mMapView.Map; //開啟交通圖 mBaiduMap.TrafficEnabled = true;
百度在開放熱力圖本地繪製能力之後,進一步把自有數據的城市熱力圖層也開放給開發者,讓 App 能呈現更豐富的視覺效果。它的用法與實時交通圖類似,一行接口呼叫即可開啟:
mMapView = FindViewById<TextureMapView>(Resource.Id.bmapView); mBaiduMap = mMapView.Map; //開啟熱力圖 mBaiduMap.BaiduHeatMapEnabled = true;

在前一節範例的基礎上,我們加上一組單選按鈕與核取方塊,讓使用者能自由切換圖層。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioGroup android:id="@+id/RadioGroup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="2" android:orientation="horizontal" > <RadioButton android:id="@+id/normal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="true" android:text="普通圖" /> <RadioButton android:id="@+id/statellite" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="衛星圖" /> </RadioGroup> <CheckBox android:id="@+id/traffice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="交通圖" /> <CheckBox android:id="@+id/baiduHeatMap" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="百度城市熱力圖" /> </LinearLayout> <com.baidu.mapapi.map.TextureMapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" /> </LinearLayout>
在 SrcSdkDemos 資料夾下加入此檔,把各控制項的事件綁到地圖屬性上:
using Android.App; using Android.Content.PM; using Android.OS; using Android.Widget; using Com.Baidu.Mapapi.Map; namespace BdMapV371Demos.SrcSdkDemos { /// <summary> /// 演示地圖圖層顯示的控制方法 /// </summary> [Activity(Label = "@string/demo_name_layers", ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden, ScreenOrientation = ScreenOrientation.Sensor)] public class Demo04Layers : Activity { private TextureMapView mMapView; private BaiduMap mBaiduMap; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.demo04_layers); mMapView = FindViewById<TextureMapView>(Resource.Id.bmapView); mBaiduMap = mMapView.Map; mBaiduMap.SetMapStatus(MapStatusUpdateFactory.NewLatLng(MainActivity.HeNanUniversity)); var normal = FindViewById<RadioButton>(Resource.Id.normal); normal.Click += delegate { mBaiduMap.MapType = BaiduMap.MapTypeNormal; }; var statellite = FindViewById<RadioButton>(Resource.Id.statellite); statellite.Click += delegate { mBaiduMap.MapType = BaiduMap.MapTypeSatellite; }; var traffice = FindViewById<CheckBox>(Resource.Id.traffice); traffice.CheckedChange += (s, e) => { mBaiduMap.TrafficEnabled = e.IsChecked; }; var baiduHeatMap = FindViewById<CheckBox>(Resource.Id.baiduHeatMap); baiduHeatMap.CheckedChange += (s, e) => { mBaiduMap.BaiduHeatMapEnabled = e.IsChecked; }; } protected override void OnPause() { mMapView.OnPause(); base.OnPause(); } protected override void OnResume() { mMapView.OnResume(); base.OnResume(); } protected override void OnDestroy() { mMapView.OnDestroy(); base.OnDestroy(); } } }
在 demos 欄位中加入 this 範例的入口:
//示例4--圖層展示 new DemoInfo<Activity>(Resource.String.demo_title_layers, Resource.String.demo_desc_layers, new Demo04Layers()),
運行後,就能透過勾選與點擊即時切換底圖、路況與熱力圖了。
Android 遊戲源碼分享:2048 的實作解析
2048 是一款規則極簡單的數字合併遊戲:在 4×4 的棋盤上滑動,相同數字相撞即合併,目標是湊出 2048。正因規則簡單,它很適合作為 Android 遊戲開發的練習題
Android Binder 機制全解:從 Framework 到 Linux 驅動
一、Binder 概述 Binder 是 Android 自研的一種 IPC(進程間通信)機制。 傳統 Linux 系統提供了多種進程間通信方式,例如: Message
Android 自定義 RiseNumberTextView:打造資產數字增長動畫
從支付寶資產頁得到的靈感許多金融類 App 在進入資產頁面時,會讓金額從一個起始值平滑增長到最終數字,這種動態效果能提升頁面的儀式感與可讀性。本文以自定義 TextVie
Android 圖像處理:黑白濾鏡實作
黑白濾鏡是最經典的圖像效果之一。它會去除色彩資訊,只保留明暗層次,使照片呈現出紀錄片、街拍或復古影像般的氛圍。在 Android 中,這類效果同樣可以透過 Bitmap