【Android】3.10 熱力圖功能


分類:C#、Android、VS2015、百度地圖應用; 創建日期:2016-02-04

一、簡介

熱力圖是用不同顏色的區塊疊加在地圖上描述人群分布、密度和變化趨勢的一個產品,可利用自有數據,構建屬於自己的熱力圖,為用戶提供豐富的展示效果。

二、運行截圖

簡介:繪制自有數據熱力圖

詳述:

(1)設置熱力圖顏色;

(2)准備數據、生成熱力圖;

(3)刪除熱力圖;

本示例運行截圖如下:

注意:由於.json文件包含的是全國范圍的數據,單線程的加載過程非常慢(約10分鍾左右),需要耐心等待。一旦數據加載完畢,再進行添加、刪除操作就很快了。

當【添加】按鈕變為可用時,單擊它即可看到截圖的效果。

image

三、設計步驟

1、添加demo10_heatmap.xml文件

在layout文件夾下添加該文件,然后將代碼改為下面的內容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://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" >

        <Button
            android:id="@+id/add"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip"
            android:layout_marginTop="2dip"
            android:layout_weight="1"
            android:padding="10dip"
            android:text="添加" />

        <Button
            android:id="@+id/remove"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="2dip"
            android:layout_marginRight="2dip"
            android:layout_marginTop="2dip"
            android:layout_weight="1"
            android:text="刪除" />
    </LinearLayout>

    <com.baidu.mapapi.map.TextureMapView
        android:id="@+id/mapview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

 

2、添加Demo10HeatMap.cs文件

在SrcSdkDemos文件夾下添加該文件,然后將代碼改為下面的內容:

using Android.App;
using Android.Content.PM;
using Android.OS;
using Android.Widget;
using Com.Baidu.Mapapi.Map;
using Com.Baidu.Mapapi.Model;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BdMapV371Demos.SrcSdkDemos
{
    /// <summary>
    /// 熱力圖功能demo
    /// </summary>
    [Activity(Label = "@string/demo_name_heatmap",
        ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden,
        ScreenOrientation = ScreenOrientation.Sensor)]
    public class Demo10HeatMap : Activity
    {
        private TextureMapView mMapView;
        private BaiduMap mBaiduMap;
        private HeatMap heatmap;
        private Button mAdd;
        private Button mRemove;
        private bool isDestroy;

        private List<LatLng> data;

        protected async override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.demo10_heatmap);

            mMapView = FindViewById<TextureMapView>(Resource.Id.mapview);
            mBaiduMap = mMapView.Map;
            mBaiduMap.SetMapStatus(MapStatusUpdateFactory.ZoomTo(5));

            mAdd = FindViewById<Button>(Resource.Id.add);
            mRemove = FindViewById<Button>(Resource.Id.remove);
            mAdd.Enabled = false;
            mRemove.Enabled = false;
            mAdd.Click += delegate
            {
                AddHeatMap();
            };
            mRemove.Click += delegate
            {
                heatmap.RemoveHeatMap();
                mAdd.Enabled = true;
                mRemove.Enabled = false;
            };

            await Task.Run(() =>
            {
                data = new List<LatLng>();
                var stream = Resources.OpenRawResource(Resource.Raw.locations);
                string json = new Java.Util.Scanner(stream).UseDelimiter("\\A").Next();
                try
                {
                    Org.Json.JSONArray array = new Org.Json.JSONArray(json);
                    for (int i = 0; i < array.Length(); i++)
                    {
                        Org.Json.JSONObject obj = array.GetJSONObject(i);
                        double lat = obj.GetDouble("lat");
                        double lng = obj.GetDouble("lng");
                        data.Add(new LatLng(lat, lng));
                    }
                }
                catch (Java.Lang.Exception e)
                {
                    e.PrintStackTrace();
                }
            });
            mAdd.Enabled = true;
        }

        private void AddHeatMap()
        {
            mAdd.Enabled = false;
            heatmap = new HeatMap.Builder().Data(data).Build();
            if (!isDestroy)
            {
                mBaiduMap.AddHeatMap(heatmap);
            }
            mRemove.Enabled = true;
        }

        protected override void OnPause()
        {
            base.OnPause();
            mMapView.OnPause();
        }

        protected override void OnResume()
        {
            base.OnResume();
            mMapView.OnResume();
        }

        protected override void OnDestroy()
        {
            base.OnDestroy();
            isDestroy = true;
            mMapView.OnDestroy();
        }
    }
}

3、修改MainActivity.cs

在MainActivity.cs文件的demos字段定義中,去掉【示例10】下面的注釋。

運行觀察結果。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM