准备工作

  • 获取密钥: 具体步骤看这里
  • 下载库文件: 链接

事实上,我觉得百度的Android定位SDK开发指南已经很详细了。本文中的例子就是根据开发指南写的。

具体实现

设置AndroidManifest.xml

在application标签中声明service组件,每个app拥有自己单独的定位service

1
2
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote">
</service>

声明使用权限

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
<!-- 这个权限用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" >
</uses-permission>
<!-- 这个权限用于访问GPS定位 -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" >
</uses-permission>
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" >
</uses-permission>
<!-- 这个权限用于获取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.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<!-- 访问网络,网络定位需要上网 -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- SD卡读取权限,用户写入离线定位数据 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>
<!-- 允许应用读取低级别的系统日志文件 -->
<uses-permission android:name="android.permission.READ_LOGS" >
</uses-permission>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
</uses-permission>
<!-- 允许应用读取低级别的系统日志文件 -->
<uses-permission android:name="android.permission.READ_LOGS" >
</uses-permission>

在application中设置AcessKey

1
2
3
4
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="key" />
<!-- key:开发者申请的key,即准备工作中的密钥ak -->

代码实现: MainActivity.java

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
public class MainActivity extends Activity {

public LocationClient mLocationClient;
public BDLocationListener myListener = new MyLocationListener();
public NotifyListener mNotifyListener;

private EditText showLocation;
private Button startNotify, stopNotify;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showLocation = (EditText) findViewById(R.id.showLocation);
startNotify = (Button) findViewById(R.id.startNotify);
stopNotify = (Button) findViewById(R.id.stopNotify);
mLocationClient = new LocationClient(getApplicationContext());

LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy); // 设置定位模式
option.setCoorType("bd0911"); // 返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5000); // 发起定位请求时间间隔为5000ms
option.setIsNeedAddress(true); // 返回的结果包含地址信息
option.setNeedDeviceDirect(true); // 返回定位结果包含手机朝向
option.setOpenGps(true); // 设置打开GPS,默认是不打开的
mLocationClient.setLocOption(option);

// 注册监听器
mLocationClient.registerLocationListener(myListener);
mLocationClient.start();

new Thread() {
public void run() {
new Timer().schedule(new TimerTask() {

@Override
public void run() {
// TODO Auto-generated method stub
// 每隔3秒请求一次位置信息
mLocationClient.requestLocation();
}
}, 0, 300);
};
}.start();

// 位置提醒
startNotify.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
mNotifyListener = new NotifyListener();
// 4个参数代表要位置提醒的点的坐标,具体含义依次为:纬度,经度,距离范围,坐标系类型(gcj02,gps,bd09,bd09ll)
mNotifyListener.SetNotifyLocation(23.0206, 113.0960, 3000,
"gcj02");
// 注册位置监听提醒
mLocationClient.registerNotify(mNotifyListener);
}
});

stopNotify.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mLocationClient.removeNotifyEvent(mNotifyListener);
}
});
}

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mLocationClient.stop();
mLocationClient.unRegisterLocationListener(myListener);
}

public class MyLocationListener implements BDLocationListener {

@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
if (location == null) {
return;
}
StringBuilder sb = new StringBuilder();
sb.append("time: ");
sb.append(location.getTime());
sb.append("\nerr code: ");
sb.append(location.getLocType());
sb.append("\nlatitude: ");
sb.append(location.getLatitude());
sb.append("\nlongitude: ");
sb.append(location.getLongitude());
sb.append("\nradius: ");
sb.append(location.getRadius());
if (location.getLocType() == BDLocation.TypeGpsLocation) {
sb.append("\nspeed: ");
sb.append(location.getSpeed());
sb.append("\nsatellite: ");
sb.append(location.getSatelliteNumber());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
sb.append("\naddr: ");
sb.append(location.getAddrStr()); // 获取反地理编码地址
sb.append("\nprovince: ");
sb.append(location.getProvince());
sb.append("\ncity: ");
sb.append(location.getCity());
sb.append("\ndistrict: ");
sb.append(location.getDistrict());
}
// logMsg(sb.toString());
showLocation.setText(sb.toString());
}
}

public class NotifyListener extends BDNotifyListener {
@Override
public void onNotify(BDLocation location, float distance) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
"距离" + location.getAddrStr() + String.valueOf(distance)
+ "米", Toast.LENGTH_LONG).show();
}
}
}

效果图

百度定位