分类:C#、Android、百度地图应用; 日期:2016-02-04
调启百度地图是指:利用SDK接口,可直接在本地打开百度地图客户端或WebApp实现地图功能。 目前支持调启的功能有:POI周边检索、POI详情页面、步行线路规划、驾车线路规划、公交线路规划、驾车导航、步行导航、骑行导航(步行、骑行需百度地图App 8.6.6以上版本才支持)。
简介:介绍如果利用简单接口调用,开启百度地图客户端实现复杂逻辑业务。
详述:
(1)调启百度地图导航(Native);
(2)调启百度地图POI周边检索;
(3)调启百度地图POI详情检索;
(4)调启百度地图步行线路规划;
(5)调启百度地图驾车线路规划;
(6)调启百度地图公交线路规划;
(7)调启百度地图步行导航;(需百度地图App 8.6.6以上版本支持)
(8)调启百度地图骑行导航(需百度地图App 8.6.6以上版本支持)
作为本章的最后一个例子,不再详细截取每个子功能的效果图了(由于模拟器没有安装百度地图客户端,无法看到效果图,只有在安装了百度地图客户端的真实手机上才能看到效果图),因此这里仅截取了在模拟器中运行时本章的主导航界面和本例子的导航界面。
1、添加demo25_open_baidumap.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="wrap_content" android:orientation="vertical" > <View android:layout_width="fill_parent" android:layout_height="1dip" android:background="?android:attr/listDivider" /> <ListView android:id="@+id/listView_openBaiduMap" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
2、添加Demo25OpenBaiduMap.cs文件
在SrcSdkDemos文件夹下添加该文件,然后将代码改为下面的内容:
using System; using System.Collections.Generic; using Android.App; using Android.OS; using Android.Widget; using Android.Views; using Com.Baidu.Mapapi.Navi; using Com.Baidu.Mapapi.Model; using Com.Baidu.Mapapi.Utils; using Com.Baidu.Mapapi.Utils.Poi; using Com.Baidu.Mapapi.Utils.Route; namespace BdMapV371Demos.SrcSdkDemos { /// <summary> /// 此demo用来展示如何在地图绘制的每帧中再额外绘制一些用户自己的内容 /// </summary> [Activity(Label = "@string/demo_name_open_baidumap")] public class Demo25OpenBaiduMap : Activity { // 天安门坐标 double mLat1 = 39.915291; double mLon1 = 116.403857; // 百度大厦坐标 double mLat2 = 40.056858; double mLon2 = 116.308194; protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.demo25_open_baidumap); ListView mListView = FindViewById<ListView>(Resource.Id.listView_openBaiduMap); mListView.Adapter = new OpenBaiduMapListAdapter(this, GetData()); mListView.ItemClick += (s, e) => { switch (e.Position) { case 0: startNavi(); break; case 1: startPoiNearbySearch(); break; case 2: startPoiDetails(); break; case 3: startRoutePlanWalking(); break; case 4: startRoutePlanDriving(); break; case 5: startRoutePlanTransit(); break; case 6: startWalkingNavi(); break; case 7: startBikingNavi(); break; default: break; } }; } private List<string> GetData() { List<string> data = new List<string>() { "启动百度地图导航(Native)", "启动百度地图Poi周边检索", "启动百度地图Poi详情页面", "启动百度地图步行路线规划", "启动百度地图驾车路线规划", "启动百度地图公交路线规划", "启动百度地图步行导航", "启动百度地图骑行导航" }; return data; } /// <summary>启动百度地图导航(Native)</summary> public void startNavi() { LatLng pt1 = new LatLng(mLat1, mLon1); LatLng pt2 = new LatLng(mLat2, mLon2); NaviParaOption para = new NaviParaOption() .InvokeStartPoint(pt1).InvokeEndPoint(pt2) .InvokeStartName("天安门").InvokeEndName("百度大厦"); try { BaiduMapNavigation.OpenBaiduMapNavi(para, this); } catch (BaiduMapAppNotSupportNaviException e) { ShowDialog(e.Message); } } /// <summary>启动百度地图步行导航(Native)</summary> public void startWalkingNavi() { LatLng pt1 = new LatLng(mLat1, mLon1); LatLng pt2 = new LatLng(mLat2, mLon2); NaviParaOption para = new NaviParaOption() .InvokeStartPoint(pt1).InvokeEndPoint(pt2) .InvokeStartName("天安门").InvokeEndName("百度大厦"); try { BaiduMapNavigation.OpenBaiduMapWalkNavi(para, this); } catch (BaiduMapAppNotSupportNaviException e) { ShowDialog(e.Message); } } /// <summary>启动百度地图骑行导航(Native)</summary> public void startBikingNavi() { LatLng pt1 = new LatLng(mLat1, mLon1); LatLng pt2 = new LatLng(mLat2, mLon2); NaviParaOption para = new NaviParaOption() .InvokeStartPoint(pt1).InvokeEndPoint(pt2) .InvokeStartName("天安门").InvokeEndName("百度大厦"); try { BaiduMapNavigation.OpenBaiduMapBikeNavi(para, this); } catch (BaiduMapAppNotSupportNaviException e) { ShowDialog(e.Message); } } /// <summary>启动百度地图Poi周边检索</summary> public void startPoiNearbySearch() { LatLng ptCenter = new LatLng(mLat1, mLon1); // 天安门 PoiParaOption para = new PoiParaOption() .InvokeKey("天安门") .InvokeCenter(ptCenter) .InvokeRadius(2000); try { BaiduMapPoiSearch.OpenBaiduMapPoiNearbySearch(para, this); } catch (Exception e) { ShowDialog(e.Message); } } /// <summary>启动百度地图Poi详情页面</summary> public void startPoiDetails() { PoiParaOption para = new PoiParaOption().InvokeUid("65e1ee886c885190f60e77ff"); // 天安门 try { BaiduMapPoiSearch.OpenBaiduMapPoiDetialsPage(para, this); } catch (Exception e) { ShowDialog(e.Message); } } /// <summary>启动百度地图步行路线规划</summary> public void startRoutePlanWalking() { LatLng ptStart = new LatLng(34.264642646862, 108.95108518068); LatLng ptEnd = new LatLng(mLat2, mLon2); RouteParaOption para = new RouteParaOption() .InvokeStartPoint(ptStart) .InvokeEndName("大雁塔") .InvokeCityName("西安"); try { BaiduMapRoutePlan.OpenBaiduMapWalkingRoute(para, this); } catch (Exception e) { ShowDialog(e.Message); } } /// <summary>启动百度地图驾车路线规划</summary> public void startRoutePlanDriving() { LatLng ptStart = new LatLng(34.264642646862, 108.95108518068); LatLng ptEnd = new LatLng(mLat2, mLon2); RouteParaOption para = new RouteParaOption() .InvokeStartPoint(ptStart) .InvokeEndName("大雁塔") .InvokeCityName("西安"); try { BaiduMapRoutePlan.OpenBaiduMapDrivingRoute(para, this); } catch (Exception e) { ShowDialog(e.Message); } } /// <summary>启动百度地图公交路线规划</summary> public void startRoutePlanTransit() { LatLng ptStart = new LatLng(mLat1, mLon1); LatLng ptEnd = new LatLng(mLat2, mLon2); RouteParaOption para = new RouteParaOption() .InvokeStartName("天安门").InvokeEndPoint(ptStart) .InvokeBusStrategyType(RouteParaOption.EBusStrategyType.BusRecommendWay); try { BaiduMapRoutePlan.OpenBaiduMapTransitRoute(para, this); } catch (Exception e) { ShowDialog(e.Message); } } protected override void OnDestroy() { base.OnDestroy(); BaiduMapNavigation.Finish(this); BaiduMapRoutePlan.Finish(this); BaiduMapPoiSearch.Finish(this); } /// <summary>提示未安装百度地图app或app版本过低</summary> public void ShowDialog(string message) { AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog dialog = null; builder.SetMessage("您尚未安装百度地图app或app版本过低,点击确认安装?"); builder.SetTitle("提示"); builder.SetPositiveButton("确认", delegate { dialog.Dismiss(); OpenClientUtil.GetLatestBaiduMapApp(this); }); builder.SetNegativeButton("取消", delegate { dialog.Dismiss(); }); dialog = builder.Create(); dialog.Show(); } private class OpenBaiduMapListAdapter : BaseAdapter { List<string> list; Demo25OpenBaiduMap a; public OpenBaiduMapListAdapter(Demo25OpenBaiduMap a, List<string> list) { this.a = a; this.list = list; } public override int Count { get { return list.Count; } } public override View GetView(int position, View convertView, ViewGroup parent) { convertView = View.Inflate(a, Resource.Layout.demo_info_item, null); TextView title = convertView.FindViewById<TextView>(Resource.Id.title); TextView desc = convertView.FindViewById<TextView>(Resource.Id.desc); desc.Visibility = ViewStates.Invisible; title.Text = list[position]; return convertView; } public override Java.Lang.Object GetItem(int position) { return list[position]; } public override long GetItemId(int position) { return position; } } } }
3、修改MainActivity.cs文件
在MainActivity.cs文件的demos字段定义中,去掉【示例25】下面的注释。
在手机上运行观察调启效果。