百度移动API获取定位信息
简单介绍一下使用百度移动API的主要几步:
1、创建完Android工程后,导入locSdk.jar 文件及附属的armeabi包下的liblocSDK.so 下载地址http://dev.baidu.com/wiki/geolocation/index.php?title=下载;
2、配置AndroidManifest.xml 文件; 添加使用权限。详见以下代码:
[html]
<permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></permission>
<uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<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" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
在AndroidManifest.xml 文件中声明service。详见代码:
[html]
<service android:name="com.baidu.location.f" android:enabled="true"
android:process=":remote" android:permission="android.permission.BAIDU_LOCATION_SERVICE">
<intent-filter>
<action android:name="com.baidu.location.service_v2.1"></action>
</intent-filter>
</service>
3、导入相关类:
[java]
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
4、初始化LocationClient类
[java]
private LocationClient mLocationClient = null; // 定位类
private MyReceiveListenner mListenner = new MyReceiveListenner();
@Override
public void onCreate() {
mLocationClient = new LocationClient(this);
mLocationClient.registerLocationListener(mListenner);
}
[java]
5、实现BDLocationListener接口:
[java]
private class MyReceiveListenner implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation location) {
logMsg(String);
[java]
}
[java]
设置参数:
[java]
option = new LocationClientOption();
option.setCoorType("gcj02"); // 设置返回的坐标类型
option.setScanSpan(myLocationTime); // 设置时间
option.setAddrType("detail"); // 返回地址类型
mLocationClient.setLocOption(option);
mLocationClient.start(); // 打开定位
文件描述简单,写了一个demo,可以参考。点击下载
/2012/0420/20120420101343470.rar
摘自 Android_Home
补充:移动开发 , Android ,