Category: C#, Android, VS2015, Baidu map application; Creation date: 2016-02-04 3.4 Example 4--Layer display 1. Introduction 1. Map type Baidu Map Android SDK 3.7.1 provides two types For map resources (ordinary vector maps and satellite maps), developers can use the MapType attribute (C#) in BaiduMap to set the map type. C
Category: C#, Android, VS2015, Baidu map application; Creation date: 2016-02-04
1. Map type
Baidu Map Android SDK 3.7.1 provides two types of map resources (ordinary vector maps and satellite maps). Developers can use the MapType attribute (C#) in BaiduMap to set the map type. The C# core code is as follows:
mMapView = FindViewById<TextureMapView><span style="color: #000000;">(Resource.Id.bmapView); mBaiduMap </span>=<span style="color: #000000;"> mMapView.Map; </span><span style="color: #008000;">//</span><span style="color: #008000;">设置底图显示模式:普通地图</span> mBaiduMap.MapType =<span style="color: #000000;"> BaiduMap.MapTypeNormal; </span><span style="color: #008000;">//</span><span style="color: #008000;">设置底图显示模式:卫星地图</span> mBaiduMap.MapType = BaiduMap.MapTypeSatellite;
2. Real-time traffic map
Currently, real-time traffic query is supported in many cities across the country, and will be launched in other cities gradually.
Which cities currently have real-time traffic maps?
Currently (2016-01-27) 31 cities have been opened, namely Nanjing, Guangzhou, Chongqing, Dongguan, Changchun, Taizhou, Fuzhou, Jinhua, Beijing, Changzhou, Hangzhou, Wenzhou, Dalian, Nanchang, Ningbo, Shenyang, Zhongshan, Zhuhai, Foshan, Quanzhou, Shijiazhuang, Chengdu, Qingdao, Shenzhen, Wuhan, Urumqi, Changsha, Shanghai, Tianjin, Wuxi, Xiamen. Other cities will be opened in the future.
The C# core code to open real-time traffic conditions on the map is as follows:
mMapView = FindViewById<TextureMapView><span style="color: #000000;">(Resource.Id.bmapView); mBaiduMap </span>=<span style="color: #000000;"> mMapView.Map; </span><span style="color: #008000;">//</span><span style="color: #008000;">开启交通图</span> mBaiduMap.TrafficEnabled = <span style="color: #0000ff;">true</span>;
3. Baidu city heat map
After opening up the local drawing capabilities of heat maps to developers, Baidu Map SDK once again further opens up the city heat map layer of Baidu's own data to help developers build more diverse mobile applications.
The nature and use of Baidu City Heat Map is similar to that of real-time traffic map. It only requires simple interface calls to display Baidu City Heat Map with rich styles on the map.
The C# core code to open Baidu city heat map on the map is as follows:
mMapView = FindViewById<TextureMapView><span style="color: #000000;">(Resource.Id.bmapView); mBaiduMap </span>=<span style="color: #000000;"> mMapView.Map; </span><span style="color: #008000;">//</span><span style="color: #008000;">开启交通图</span> mBaiduMap.BaiduHeatMapEnabled = <span style="color: #0000ff;">true</span>;
Based on the example in the previous section, you only need to add the following steps.
2. Add demo04_layers.axml file
<?xml version=<span style="color: #800000;">"</span><span style="color: #800000;">1.0</span><span style="color: #800000;">"</span> encoding=<span style="color: #800000;">"</span><span style="color: #800000;">utf-8</span><span style="color: #800000;">"</span>?> <LinearLayout xmlns:android=<span style="color: #800000;">"</span><span style="color: #800000;">http://schemas.android.com/apk/res/android</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_width</span>=<span style="color: #800000;">"</span><span style="color: #800000;">fill_parent</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_height</span>=<span style="color: #800000;">"</span><span style="color: #800000;">fill_parent</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:orientation</span>=<span style="color: #800000;">"</span><span style="color: #800000;">vertical</span><span style="color: #800000;">"</span> > <<span style="color: #000000;">LinearLayout android:layout_width</span>=<span style="color: #800000;">"</span><span style="color: #800000;">fill_parent</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_height</span>=<span style="color: #800000;">"</span><span style="color: #800000;">wrap_content</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:orientation</span>=<span style="color: #800000;">"</span><span style="color: #800000;">horizontal</span><span style="color: #800000;">"</span> > <<span style="color: #000000;">RadioGroup android:id</span>=<span style="color: #800000;">"</span><span style="color: #800000;">@+id/RadioGroup</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_width</span>=<span style="color: #800000;">"</span><span style="color: #800000;">wrap_content</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_height</span>=<span style="color: #800000;">"</span><span style="color: #800000;">wrap_content</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_weight</span>=<span style="color: #800000;">"</span><span style="color: #800000;">2</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:orientation</span>=<span style="color: #800000;">"</span><span style="color: #800000;">horizontal</span><span style="color: #800000;">"</span> > <<span style="color: #000000;">RadioButton android:id</span>=<span style="color: #800000;">"</span><span style="color: #800000;">@+id/normal</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_width</span>=<span style="color: #800000;">"</span><span style="color: #800000;">wrap_content</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_height</span>=<span style="color: #800000;">"</span><span style="color: #800000;">wrap_content</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_weight</span>=<span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:</span><span style="color: #0000ff;">checked</span>=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:text</span>=<span style="color: #800000;">"</span><span style="color: #800000;">普通图</span><span style="color: #800000;">"</span> /> <<span style="color: #000000;">RadioButton android:id</span>=<span style="color: #800000;">"</span><span style="color: #800000;">@+id/statellite</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_width</span>=<span style="color: #800000;">"</span><span style="color: #800000;">wrap_content</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_height</span>=<span style="color: #800000;">"</span><span style="color: #800000;">wrap_content</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_weight</span>=<span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:text</span>=<span style="color: #800000;">"</span><span style="color: #800000;">卫星图</span><span style="color: #800000;">"</span> /> </RadioGroup> <<span style="color: #000000;">CheckBox android:id</span>=<span style="color: #800000;">"</span><span style="color: #800000;">@+id/traffice</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_width</span>=<span style="color: #800000;">"</span><span style="color: #800000;">wrap_content</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_height</span>=<span style="color: #800000;">"</span><span style="color: #800000;">wrap_content</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_weight</span>=<span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:</span><span style="color: #0000ff;">checked</span>=<span style="color: #800000;">"</span><span style="color: #800000;">false</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:text</span>=<span style="color: #800000;">"</span><span style="color: #800000;">交通图</span><span style="color: #800000;">"</span> /> <<span style="color: #000000;">CheckBox android:id</span>=<span style="color: #800000;">"</span><span style="color: #800000;">@+id/baiduHeatMap</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_width</span>=<span style="color: #800000;">"</span><span style="color: #800000;">wrap_content</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_height</span>=<span style="color: #800000;">"</span><span style="color: #800000;">wrap_content</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_weight</span>=<span style="color: #800000;">"</span><span style="color: #800000;">1</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:</span><span style="color: #0000ff;">checked</span>=<span style="color: #800000;">"</span><span style="color: #800000;">false</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:text</span>=<span style="color: #800000;">"</span><span style="color: #800000;">百度城市热力图</span><span style="color: #800000;">"</span> /> </LinearLayout> <<span style="color: #000000;">com.baidu.mapapi.map.TextureMapView android:id</span>=<span style="color: #800000;">"</span><span style="color: #800000;">@+id/bmapView</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_width</span>=<span style="color: #800000;">"</span><span style="color: #800000;">fill_parent</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:layout_height</span>=<span style="color: #800000;">"</span><span style="color: #800000;">fill_parent</span><span style="color: #800000;">"</span><span style="color: #000000;"> android:clickable</span>=<span style="color: #800000;">"</span><span style="color: #800000;">true</span><span style="color: #800000;">"</span> /> </LinearLayout>
2. Add the Demo04Layers.cs file
Add this file under the SrcSdkDemos folder.
<span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.App; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.Content.PM; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.OS; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Android.Widget; </span><span style="color: #0000ff;">using</span><span style="color: #000000;"> Com.Baidu.Mapapi.Map; </span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> BdMapV371Demos.SrcSdkDemos { </span><span style="color: #808080;">///</span> <span style="color: #808080;"><summary></span> <span style="color: #808080;">///</span><span style="color: #008000;"> 演示地图图层显示的控制方法 </span><span style="color: #808080;">///</span> <span style="color: #808080;"></summary></span> [Activity(Label = <span style="color: #800000;">"</span><span style="color: #800000;">@string/demo_name_layers</span><span style="color: #800000;">"</span><span style="color: #000000;">, ConfigurationChanges </span>= ConfigChanges.Orientation |<span style="color: #000000;"> ConfigChanges.KeyboardHidden, ScreenOrientation </span>=<span style="color: #000000;"> ScreenOrientation.Sensor)] </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Demo04Layers : Activity { </span><span style="color: #008000;">//</span><span style="color: #008000;">TextureMapView 是地图主控件</span> <span style="color: #0000ff;">PRivate</span><span style="color: #000000;"> TextureMapView mMapView; </span><span style="color: #0000ff;">private</span><span style="color: #000000;"> BaiduMap mBaiduMap; </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnCreate(Bundle savedInstanceState) { </span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnCreate(savedInstanceState); SetContentView(Resource.Layout.demo04_layers); mMapView </span>= FindViewById<TextureMapView><span style="color: #000000;">(Resource.Id.bmapView); mBaiduMap </span>=<span style="color: #000000;"> mMapView.Map; mBaiduMap.SetMapStatus(MapStatusUpdateFactory.NewLatLng(MainActivity.HeNanUniversity)); </span><span style="color: #008000;">//</span><span style="color: #008000;">设置底图显示模式:普通图</span> <span style="color: #0000ff;">var</span> normal = FindViewById<RadioButton><span style="color: #000000;">(Resource.Id.normal); normal.Click </span>+= <span style="color: #0000ff;">delegate</span><span style="color: #000000;"> { mBaiduMap.MapType </span>=<span style="color: #000000;"> BaiduMap.MapTypeNormal; }; </span><span style="color: #008000;">//</span><span style="color: #008000;">设置底图显示模式:卫星图</span> <span style="color: #0000ff;">var</span> statellite = FindViewById<RadioButton><span style="color: #000000;">(Resource.Id.statellite); statellite.Click </span>+= <span style="color: #0000ff;">delegate</span><span style="color: #000000;"> { mBaiduMap.MapType </span>=<span style="color: #000000;"> BaiduMap.MapTypeSatellite; }; </span><span style="color: #008000;">//</span><span style="color: #008000;">是否显示交通图</span> <span style="color: #0000ff;">var</span> traffice = FindViewById<CheckBox><span style="color: #000000;">(Resource.Id.traffice); traffice.CheckedChange </span>+= (s, e) =><span style="color: #000000;"> { mBaiduMap.TrafficEnabled </span>=<span style="color: #000000;"> e.IsChecked; }; </span><span style="color: #008000;">//</span><span style="color: #008000;">是否显示热力图</span> <span style="color: #0000ff;">var</span> baiduHeatMap = FindViewById<CheckBox><span style="color: #000000;">(Resource.Id.baiduHeatMap); traffice.CheckedChange </span>+= (s, e) =><span style="color: #000000;"> { mBaiduMap.BaiduHeatMapEnabled </span>=<span style="color: #000000;"> e.IsChecked; }; } </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnPause() { mMapView.OnPause(); </span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnPause(); } </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnResume() { mMapView.OnResume(); </span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnResume(); } </span><span style="color: #0000ff;">protected</span> <span style="color: #0000ff;">override</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> OnDestroy() { mMapView.OnDestroy(); </span><span style="color: #0000ff;">base</span><span style="color: #000000;">.OnDestroy(); } } }</span>
4. Modify the MainActivity.cs file
Add the following code in the demos field definition of the MainActivity.cs file.
<span style="color: #008000;"> //</span><span style="color: #008000;">示例4--图层展示</span> <span style="color: #0000ff;">new</span> DemoInfo<Activity><span style="color: #000000;">(Resource.String.demo_title_layers, Resource.String.demo_desc_layers, </span><span style="color: #0000ff;">new</span> Demo04Layers()),
Run.