分类:C#、Android、VS2015、百度地图应用; 创建日期:2016-02-04
简介:介绍如何使用LBS.云检索用户自有数据。
详述:
(1)LBS.云是百度地图针对LBS开发者推出的平台级服务;
(2)提供海量位置数据存储、检索、展示一体化解决方案;
(3)该服务对开发者免费开放;
(4)Demo中放入了测试用ak,开发者若使用自己的数据,请更换为自己的ak;
(5)LBS.云检索使用方法请参考开发指南“LBS云服务”部分;
(6)详细了解LBS.云,欢迎访问:http://developer.baidu.com/map/lbs-cloud.htm
(7)申请ak及管理数据等问题,欢迎访问LBS开放平台官网:http://lbsyun.baidu.com/
一、运行截图
本示例运行截图如下:
二、设计步骤
1、添加demo22_cloud_search.xml文件
在layout文件夹下添加该文件,代码不再列出。
2、添加Demo22CloudSearch.cs文件
在SrcSdkDemos文件夹下添加该文件,然后将代码改为下面的内容:
using Android.App;using Android.Content;using Android.OS;using Android.Widget;namespace BdMapV371Demos.SrcSdkDemos{ [Activity(Label = "@string/title_activity_cloud_search_demo")] public class Demo22CloudSearch : Activity { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.demo22_cloud_search); FindViewById
3、添加Demo22CloudSearchActivity.cs文件
在SrcSdkDemos文件夹下添加该文件,然后将代码改为下面的内容:
using Android.App;using Android.Content.PM;using Android.OS;using Android.Util;using Android.Widget;using Com.Baidu.Mapapi.Cloud;using Com.Baidu.Mapapi.Map;using Com.Baidu.Mapapi.Model;namespace BdMapV371Demos.SrcSdkDemos{ [Activity(Label = "@string/demo_name_cloud", ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.KeyboardHidden, ScreenOrientation = ScreenOrientation.Sensor)] public class Demo22CloudSearchActivity : Activity, ICloudListener { private static readonly string LTAG = nameof(Demo22CloudSearchActivity); private TextureMapView mMapView; private BaiduMap mBaiduMap; protected override void OnCreate(Bundle icicle) { base.OnCreate(icicle); SetContentView(Resource.Layout.demo22_lbssearch); CloudManager.Instance.Init(this); mMapView = FindViewById(Resource.Id.bmapView); mBaiduMap = mMapView.Map; FindViewById(Resource.Id.regionSearch).Click += delegate { LocalSearchInfo info = new LocalSearchInfo(); info.Ak = "B266f735e43ab207ec152deff44fec8b"; info.GeoTableId = 31869; info.Tags = ""; info.Q = "天安门"; info.Region = "北京市"; CloudManager.Instance.LocalSearch(info); }; FindViewById(Resource.Id.regionSearch).Click += delegate { LocalSearchInfo info = new LocalSearchInfo(); info.Ak = "B266f735e43ab207ec152deff44fec8b"; info.GeoTableId = 31869; info.Tags = ""; info.Q = "天安门"; info.Region = "北京市"; CloudManager.Instance.LocalSearch(info); }; FindViewById(Resource.Id.nearbySearch).Click += delegate { NearbySearchInfo info = new NearbySearchInfo(); info.Ak = "D9ace96891048231e8777291cda45ca0"; info.GeoTableId = 32038; info.Radius = 30000; info.Location = "116.403689,39.914957"; CloudManager.Instance.NearbySearch(info); }; FindViewById(Resource.Id.boundsSearch).Click += delegate { BoundSearchInfo info = new BoundSearchInfo(); info.Ak = "B266f735e43ab207ec152deff44fec8b"; info.GeoTableId = 31869; info.Q = "天安门"; info.Bound = "116.401663,39.913961;116.406529,39.917396"; CloudManager.Instance.BoundSearch(info); }; FindViewById(Resource.Id.detailsSearch).Click += delegate { DetailSearchInfo info = new DetailSearchInfo(); info.Ak = "B266f735e43ab207ec152deff44fec8b"; info.GeoTableId = 31869; info.Uid = 18622266; CloudManager.Instance.DetailSearch(info); }; } protected override void OnDestroy() { base.OnDestroy(); mMapView.OnDestroy(); CloudManager.Instance.Destroy(); } protected override void OnPause() { base.OnPause(); mMapView.OnPause(); } protected override void OnResume() { base.OnResume(); mMapView.OnResume(); } public void OnGetDetailSearchResult(DetailSearchResult result, int error) { if (result != null) { if (result.PoiInfo != null) { Toast.MakeText(this, result.PoiInfo.Title, ToastLength.Short).Show(); } else { Toast.MakeText(this, "status:" + result.Status, ToastLength.Short).Show(); } } } public void OnGetSearchResult(CloudSearchResult result, int error) { if (result != null && result.PoiList != null && result.PoiList.Count > 0) { Log.Debug(LTAG, "onGetSearchResult, result length: " + result.PoiList.Count); mBaiduMap.Clear(); BitmapDescriptor bd = BitmapDescriptorFactory.FromResource(Resource.Drawable.icon_gcoding); LatLng ll; LatLngBounds.Builder builder = new LatLngBounds.Builder(); foreach (CloudPoiInfo info in result.PoiList) { ll = new LatLng(info.Latitude, info.Longitude); OverlayOptions oo = new MarkerOptions().InvokeIcon(bd).InvokePosition(ll); mBaiduMap.AddOverlay(oo); builder.Include(ll); } LatLngBounds bounds = builder.Build(); MapStatusUpdate u = MapStatusUpdateFactory.NewLatLngBounds(bounds); mBaiduMap.AnimateMapStatus(u); } } }}
4、修改MainActivity.cs文件
在MainActivity.cs文件的demos字段定义中,去掉【示例22】下面的注释。
运行观察效果。