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

用 C# 編寫百度地圖 Android 應用(第 5 講):一頁展示多個地圖

編輯:關於 Android 開發

用 C# 編寫百度地圖 Android 應用程序(第 5 講)

分類:C#、Android;日期:2016-02-04

3.5 示例 5 —— 多地圖展示

一、簡介

自 v2.3.5 起,百度地圖控件支持多實例:開發者可以在同一頁面創建多個地圖對象,分別操作而不互相干擾。本示例在一個界面內並排放置四個 TextureMapView,各自定位到不同城市。

文件名:Demo04MultiMapView.cs

二、示例步驟

在上一節示例的基礎上,只需再增加以下三步。運行效果如下:

1. 添加 demo05_multimap.axml 布局文件

放在 layout 文件夾下,用兩層 LinearLayout 把四個地圖 Fragment 排成 2×2:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    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 文件夾下新建該文件:

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

        /// <summary>
        /// 初始化各個地圖的中心點
        /// </summary>
        private void InitMap()
        {
            TextureMapFragment map1 = FragmentManager
                .FindFragmentById<TextureMapFragment>(Resource.Id.map1);
            map1.BaiduMap.SetMapStatus(MapStatusUpdateFactory.NewLatLng(Geo_BeiJing));

            TextureMapFragment map2 = FragmentManager
                .FindFragmentById<TextureMapFragment>(Resource.Id.map2);
            map2.BaiduMap.SetMapStatus(MapStatusUpdateFactory.NewLatLng(Geo_ShangHai));

            TextureMapFragment map3 = FragmentManager
                .FindFragmentById<TextureMapFragment>(Resource.Id.map3);
            map3.BaiduMap.SetMapStatus(MapStatusUpdateFactory.NewLatLng(Geo_GuangZhou));

            TextureMapFragment map4 = FragmentManager
                .FindFragmentById<TextureMapFragment>(Resource.Id.map4);
            map4.BaiduMap.SetMapStatus(MapStatusUpdateFactory.NewLatLng(Geo_ShenZhen));
        }
    }
}

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