您好,登錄后才能下訂單哦!
先下載百度地圖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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。