分类:C#、Android、VS2015、百度地图应用; 创建日期:2016-02-04 3.4 示例4--图层展示 一、简介 1、地图类型 百度地图Android SDK 3.7.1提供了两种类型的地图资源(普通矢量地图和卫星图),开发者可以利用BaiduMap中的MapType属性(C#)来设置地图类型。C
分类:C#、Android、VS2015、百度地图应用; 创建日期:2016-02-04
1、地图类型
百度地图Android SDK 3.7.1提供了两种类型的地图资源(普通矢量地图和卫星图),开发者可以利用BaiduMap中的MapType属性(C#)来设置地图类型。C#核心代码如下:
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;</texturemapview>
2、实时交通图
当前,全国范围内已支持多个城市实时路况查询,且会陆续开通其他城市。
目前有哪些城市具有实时交通图?
目前(2016-01-27)已有31个城市开通,分别为南京,广州,重庆,东莞,长春,台州,福州,金华,北京,常州,杭州,温州,大连,南昌,宁波,沈阳,中山,珠海,佛山,泉州,石家庄,成都,青岛,深圳,武汉,乌鲁木齐,长沙,上海,天津,无锡,厦门。之后其他城市还会陆续开通。
在地图上打开实时路况的C#核心代码如下:
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>;</texturemapview>
3、百度城市热力图
百度地图SDK继为广大开发者开放热力图本地绘制能力之后,再次进一步开放百度自有数据的城市热力图层,帮助开发者构建形式更加多样的移动端应用。
百度城市热力图的性质及使用与实时交通图类似,只需要简单的接口调用,即可在地图上展现样式丰富的百度城市热力图。
在地图上开启百度城市热力图的C#核心代码如下:
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>;</texturemapview>
在上一节例子的基础上,只需要再增加下面的步骤即可。
2、添加demo04_layers.axml文件
<?xml version=<span style="color: #800000;">"<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 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> > LinearLayout android:layout_width=<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> > RadioGroup android:id=<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> > RadioButton android:id=<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> /> RadioButton android:id=<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> /> CheckBox android:id=<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> /> CheckBox android:id=<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> com.baidu.mapapi.map.TextureMapView android:id=<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> />
2、添加Demo04Layers.cs文件
在SrcSdkDemos文件夹下添加该文件。
<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></summary></span> <span style="color: #808080;">///</span><span style="color: #008000;"> 演示地图图层显示的控制方法 </span><span style="color: #808080;">///</span> <span style="color: #808080;"></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></checkbox></checkbox></radiobutton></radiobutton></texturemapview>
4、修改MainActivity.cs文件
在MainActivity.cs文件的demos字段定义中添加下面的代码。
<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()),</activity>
运行。