百度地图绑定库文件
新建一个BindingsLibrary项目,并把刚下载的BaiduLBS_Android.jar添加到项目的Jars文件夹下,更改BaiduLBS_Android.jar的属性中的”生成操作”属性为EmbeddedJar
生成些项目,如果遇到这样的错误:
VersionInfo”: 成员名不能与它们的封闭类型相同
打开项目中的Transforms的Metadate.xml,在<metadata>节点添加如下代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<
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#代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
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)
{
}
}
}
|
本文转自桂素伟51CTO博客,原文链接: http://blog.51cto.com/axzxs/1707541,如需转载请自行联系原作者