zoukankan      html  css  js  c++  java
  • 【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】下面的注释。

    运行观察结果。

  • 相关阅读:
    HearthBuddy投降插件2019-11-01的使用
    正则表达式在线分析 regex online analyzer
    Tips to write better Conditionals in JavaScript
    The fileSyncDll.ps1 is not digitally signed. You cannot run this script on the current system.
    Cannot capture jmeter traffic in fiddler
    JMETER + POST + anti-forgery token
    input type color
    HearthBuddy修改系统时间
    What are all the possible values for HTTP “Content-Type” header?
    UDK性能优化
  • 原文地址:https://www.cnblogs.com/rainmj/p/5181468.html
Copyright © 2011-2022 走看看