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

一個頁面放四張地圖?Android 百度地圖多地圖展示實作

編輯:關於 Android 程式設計

你可能想過:同一個畫面裡能不能同時顯示好幾張地圖,各自定位到不同城市?好消息是,從 v2.3.5 版起,百度地圖控件就支援多實例了——你可以在一個頁面裡建立多個地圖物件,分別操作且互不干擾。

一、先看成果

本範例(Demo05MultiMapView.cs)會在同一個介面內並排建立四個 TextureMapView,各自指向不同城市。在 x86 模擬器上的運行效果如下:

做法很簡單,只需在前一節範例的基礎上再加幾個步驟即可。

二、實作步驟

1. 新增 demo05_multimap.axml 版面配置

在 layout 資料夾下新增此檔,用巢狀的 LinearLayout 把畫面切成上下兩列、每列兩欄,每格放一個 TextureMapFragment:

 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:map="https://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="5dp" android:layout_weight="1" android:orientation="horizontal" > <fragment android:id="@+id/map1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="5dp" android:layout_weight="1" class="com.baidu.mapapi.map.TextureMapFragment" /> <fragment android:id="@+id/map2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" class="com.baidu.mapapi.map.TextureMapFragment" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal" > <fragment android:id="@+id/map3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="5dp" android:layout_weight="1" class="com.baidu.mapapi.map.TextureMapFragment" /> <fragment android:id="@+id/map4" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" class="com.baidu.mapapi.map.TextureMapFragment" /> </LinearLayout> </LinearLayout> 

2. 新增 Demo05MultiMapView.cs

在 SdkDemos 資料夾下新增此檔。核心邏輯是用 FindFragmentById 抓到四個 Fragment,再逐一用 SetMapStatus 把它們定位到北京、上海、廣州、深圳:

 using Android.App; using Android.Content.PM; using Android.OS; using Com.Baidu.Mapapi.Map; using Com.Baidu.Mapapi.Model; namespace BdMapV371Demos.SrcSdkDemos { /// <summary> /// 在一個 Activity 中展示多個地圖 /// </summary> [Activity(Label = "@string/demo_name_multimap", ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden, ScreenOrientation = ScreenOrientation.Sensor)] public class Demo05MutiMapView : Activity { private readonly LatLng Geo_BeiJing = new LatLng(39.945, 116.404); private readonly LatLng Geo_ShangHai = new LatLng(31.227, 121.481); private readonly LatLng Geo_GuangZhou = new LatLng(23.155, 113.264); private readonly LatLng Geo_ShenZhen = new LatLng(22.560, 114.064); protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.demo05_multimap); InitMap(); } private void InitMap() { MapStatusUpdate u1 = MapStatusUpdateFactory.NewLatLng(Geo_BeiJing); TextureMapFragment map1 = FragmentManager.FindFragmentById<TextureMapFragment>(Resource.Id.map1); map1.BaiduMap.SetMapStatus(u1); MapStatusUpdate u2 = MapStatusUpdateFactory.NewLatLng(Geo_ShangHai); TextureMapFragment map2 = FragmentManager.FindFragmentById<TextureMapFragment>(Resource.Id.map2); map2.BaiduMap.SetMapStatus(u2); MapStatusUpdate u3 = MapStatusUpdateFactory.NewLatLng(Geo_GuangZhou); TextureMapFragment map3 = FragmentManager.FindFragmentById<TextureMapFragment>(Resource.Id.map3); map3.BaiduMap.SetMapStatus(u3); MapStatusUpdate u4 = MapStatusUpdateFactory.NewLatLng(Geo_ShenZhen); TextureMapFragment map4 = FragmentManager.FindFragmentById<TextureMapFragment>(Resource.Id.map4); map4.BaiduMap.SetMapStatus(u4); } } } 

3. 修改 MainActivity.cs

在 demos 欄位定義中補上這一段,讓範例清單裡多出「多地圖展示」這一項:

 //示例5--多地圖展示 new DemoInfo<Activity>(Resource.String.demo_title_multimap, Resource.String.demo_desc_multimap, new Demo05MutiMapView()), 

完成後運行,就能一次看到四張各自定位的地圖同框呈現了。

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