先下载百度地图
百度地图绑定库文件
新建一个BindingsLibrary项目,并把刚下载的BaiduLBS_Android.jar添加到项目的Jars文件夹下,更改BaiduLBS_Android.jar的属性中的”生成操作”属性为EmbeddedJar
生成些项目,如果遇到这样的错误:
VersionInfo”: 成员名不能与它们的封闭类型相同
打开项目中的Transforms的Metadate.xml,在<metadata>节点添加如下代码
VersionInfos
因为在C#中不能定义和类名相同的类成员(构造函数除外)
创建andorid项目
在项目中创建一个libs文件夹,把下载的百度地图包中的.so文件添加进来,并在属性上设置成AndroidNativeLibrary
添加权限和设置百度地图访问应用Key
在布局文件中添加地图控件(注意大小写:)
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(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); } /// /// 实现ILocationListener中方法,位置改变 /// /// 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); } ////// 实现ILocationListener中方法 /// /// public void OnProviderDisabled(string provider) { } ////// 实现ILocationListener中方法 /// /// public void OnProviderEnabled(string provider) { } ////// 实现ILocationListener中方法 /// /// /// /// public void OnStatusChanged(string provider, [GeneratedEnum] Availability status, Bundle extras) { } }}