准备工作
事实上,我觉得百度的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"); option.setScanSpan(5000); option.setIsNeedAddress(true); option.setNeedDeviceDirect(true); option.setOpenGps(true); mLocationClient.setLocOption(option);
mLocationClient.registerLocationListener(myListener); mLocationClient.start();
new Thread() { public void run() { new Timer().schedule(new TimerTask() {
@Override public void run() { mLocationClient.requestLocation(); } }, 0, 300); }; }.start();
startNotify.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { mNotifyListener = new NotifyListener(); mNotifyListener.SetNotifyLocation(23.0206, 113.0960, 3000, "gcj02"); mLocationClient.registerNotify(mNotifyListener); } });
stopNotify.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) { mLocationClient.removeNotifyEvent(mNotifyListener); } }); }
@Override protected void onDestroy() { super.onDestroy(); mLocationClient.stop(); mLocationClient.unRegisterLocationListener(myListener); }
public class MyLocationListener implements BDLocationListener {
@Override public void onReceiveLocation(BDLocation location) { 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()); } showLocation.setText(sb.toString()); } }
public class NotifyListener extends BDNotifyListener { @Override public void onNotify(BDLocation location, float distance) { Toast.makeText( getApplicationContext(), "距离" + location.getAddrStr() + String.valueOf(distance) + "米", Toast.LENGTH_LONG).show(); } } }
|
效果图