Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

百度地圖圖層玩法:普通圖、衛星圖、實時路況與城市熱力圖

編輯:關於 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; 

四、動手做一個圖層切換器

在前一節範例的基礎上,我們加上一組單選按鈕與核取方塊,讓使用者能自由切換圖層。

1. 新增 demo04_layers.axml

 <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> 

2. 新增 Demo04Layers.cs

在 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(); } } } 

3. 修改 MainActivity.cs

在 demos 欄位中加入 this 範例的入口:

 //示例4--圖層展示 new DemoInfo<Activity>(Resource.String.demo_title_layers, Resource.String.demo_desc_layers, new Demo04Layers()),

運行後,就能透過勾選與點擊即時切換底圖、路況與熱力圖了。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved