您好,登录后才能下订单哦!
先下载百度地图http://lbsyun.baidu.com/sdk/download?selected=mapsdk_basicmap,mapsdk_searchfunction,mapsdk_lbscloudsearch,mapsdk_calculationtool
百度地图绑定库文件
新建一个BindingsLibrary项目,并把刚下载的BaiduLBS_Android.jar添加到项目的Jars文件夹下,更改BaiduLBS_Android.jar的属性中的”生成操作”属性为EmbeddedJar
生成些项目,如果遇到这样的错误:
VersionInfo”: 成员名不能与它们的封闭类型相同
打开项目中的Transforms的Metadate.xml,在<metadata>节点添加如下代码
< attr path = " /api/package[@name='com.baidu.mapapi']/class[@name='VersionInfo']/field[@name='VERSION_INFO'] " name = " name " > VersionInfos </ attr >
因为在C#中不能定义和类名相同的类成员(构造函数除外)
创建andorid项目
在项目中创建一个libs文件夹,把下载的百度地图包中的.so文件添加进来,并在属性上设置成AndroidNativeLibrary
添加权限和设置百度地图访问应用Key
<application android:label="BaiduMapDemo" android:icon="@drawable/Icon"> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="这个需要注册百度开放平台申请Android的Key" /> </application> <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位--> <uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission> <!-- 用于读取手机当前的状态--> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 在布局文件中添加地图控件(注意大小写:) <com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" />
C#代码如下
using Android.App; using Android.Content; using Android.Runtime; using Android.Widget; using Android.OS; using Com.Baidu.Mapapi.Map; using Com.Baidu.Mapapi; using Android.Locations; using Com.Baidu.Mapapi.Model; namespace BaiduMapDemo { [Activity(Label = "BaiduMapDemo", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity, ILocationListener//为安卓定位接口 { LocationManager locMgr; MapView mMapView ; BaiduMap mBaiduMap ; protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); //初台化地图,注意位置,在SetContentView前 SDKInitializer.Initialize(Application.Context); SetContentView(Resource.Layout.Main); mMapView = FindViewById<MapView>(Resource.Id.bmapView); mBaiduMap = mMapView.Map; //普通地图 mBaiduMap.MapType = BaiduMap.MapTypeNormal; } protected override void OnResume() { base.OnResume(); locMgr = GetSystemService(Context.LocationService) as LocationManager; Criteria locationCriteria = new Criteria(); locationCriteria.Accuracy = Accuracy.Coarse; locationCriteria.PowerRequirement = Power.Medium; locationCriteria.AltitudeRequired = false;//海拔 locationCriteria.BearingRequired = false;//方位/方向 locationCriteria.SpeedRequired = false;//速度 locationCriteria.CostAllowed = true;//成本 var locationProvider = locMgr.GetBestProvider(locationCriteria, true); if (locationProvider != null) { locMgr.RequestLocationUpdates(locationProvider, 2000, 1, this); } else { Toast.MakeText(this, "The Network Provider does not exist or is not enabled!", ToastLength.Long).Show(); } } protected override void OnStart() { base.OnStart(); } protected override void OnStop() { base.OnStop(); } protected override void OnPause() { base.OnPause(); locMgr.RemoveUpdates(this); } /// <summary> /// 实现ILocationListener中方法,位置改变 /// </summary> /// <param name="location"></param> public void OnLocationChanged(Location location) { var point = new LatLng(location.Latitude, location.Longitude); //使地图移动到当前位置 MapStatus mMapStatus = new MapStatus.Builder() .Target(point) .Zoom(12) .Build(); //定义MapStatusUpdate对象,以便描述地图状态将要发生的变化 MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.NewMapStatus(mMapStatus); //改变地图状态 mBaiduMap.SetMapStatus(mMapStatusUpdate); //创建当前位置Marker图标 BitmapDescriptor bitmap = BitmapDescriptorFactory .FromResource(Resource.Drawable.icon_gcoding); //构建MarkerOption,用于在地图上添加Marker OverlayOptions option = new MarkerOptions() .InvokePosition(point) .InvokeIcon(bitmap); //在地图上添加Marker,并显示 mBaiduMap.AddOverlay(option); } /// <summary> /// 实现ILocationListener中方法 /// </summary> /// <param name="provider"></param> public void OnProviderDisabled(string provider) { } /// <summary> /// 实现ILocationListener中方法 /// </summary> /// <param name="provider"></param> public void OnProviderEnabled(string provider) { } /// <summary> /// 实现ILocationListener中方法 /// </summary> /// <param name="provider"></param> /// <param name="status"></param> /// <param name="extras"></param> public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras) { } } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。