標籤:xamarin android 百度地圖
先下載百度地圖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#中不能定義和類名相同的類成員(建構函式除外)
在項目中建立一個libs檔案夾,把下載的百度地圖包中的.so檔案添加進來,並在屬性上設定成AndroidNativeLibrary
650) this.width=650;" title="XamainAndroidBiadu001.png" src="http://s3.51cto.com/wyfs02/M02/75/0B/wKiom1YxcmmhMxEhAANNWXpvrg4488.jpg" alt="wKiom1YxcmmhMxEhAANNWXpvrg4488.jpg" />
添加許可權和設定百度地圖訪問應用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) { } }}
本文出自 “桂素偉” 部落格,請務必保留此出處http://axzxs.blog.51cto.com/730810/1707541
Xamarin隻言片語4——Xamarin.Android百度地圖綁定