使用百度sdk简单定位

时间:2022-09-04 10:23:06

参考 http://wiki.lbsyun.baidu.com/cms/locsdkrelease/20160427/locsdkDemo6.2.3.zip 最新demo


首先申请密匙 http://lbsyun.baidu.com/apiconsole/key

最简便获取SHA1的方法,到

http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=106461

下载MCodeCheck.zip

用这软件即可测试key是否正确,然后进项目在

AndroidManifest.xml中添加权限

<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>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

application
<!--不写
android:name=".LocationApplication"
会报NoClassDefFoundError-->

<application
android:name=".LocationApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme.Dark"
>
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
<intent-filter>
<action android:name="com.baidu.location.service_v2.2" >
</action>
</intent-filter>
</service>
<meta-data
android:name="com.baidu.lbsapi.API_KEY"
android:value="开发者申请的key" /> <!-- //key:开发者申请的key-->
然后创建类LocationApplication
import android.app.Application;import android.app.Service;import android.os.Vibrator;import com.baidu.mapapi.SDKInitializer;import com.hhxx.www.hhusercheck.LocationService.LocationService;/** * 主Application,所有百度定位SDK的接口说明请参考线上文档:http://developer.baidu.com/map/loc_refer/index.html * * 百度定位SDK官方网站:http://developer.baidu.com/map/index.php?title=android-locsdk * * 直接拷贝com.baidu.location.service包到自己的工程下,简单配置即可获取定位结果,也可以根据demo内容自行封装 */public class LocationApplication extends Application {    public LocationService locationService;    public Vibrator mVibrator;    @Override    public void onCreate() {        super.onCreate();        /*** * 初始化定位sdk,建议在Application中创建 */ locationService = new LocationService(getApplicationContext());        mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);        SDKInitializer.initialize(getApplicationContext());    }}



写服务类 
LocationService
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;
import android.content.Context;

/**
*
* @author baidu
*
*/
public class LocationService {
private LocationClient client = null;
private LocationClientOption mOption,DIYoption;
private Object objLock = new Object();

/***
*
* @param locationContext
*/
public LocationService(Context locationContext){
synchronized (objLock) {
if(client == null){
client = new LocationClient(locationContext);
client.setLocOption(getDefaultLocationClientOption());
}
}
}

/***
*
* @param listener
* @return
*/

public boolean registerListener(BDLocationListener listener){
boolean isSuccess = false;
if(listener != null){
client.registerLocationListener(listener);
isSuccess = true;
}
return isSuccess;
}

public void unregisterListener(BDLocationListener listener){
if(listener != null){
client.unRegisterLocationListener(listener);
}
}

/***
*
* @param option
* @return isSuccessSetOption
*/
public boolean setLocationOption(LocationClientOption option){
boolean isSuccess = false;
if(option != null){
if(client.isStarted())
client.stop();
DIYoption = option;
client.setLocOption(option);
isSuccess = true;
}
return isSuccess;
}

public LocationClientOption getOption(){
return DIYoption;
}
/***
*
* @return DefaultLocationClientOption
*/
public LocationClientOption getDefaultLocationClientOption(){
if(mOption == null){
mOption = new LocationClientOption();
mOption.setLocationMode(LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
mOption.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系,如果配合百度地图使用,建议设置为bd09ll;
mOption.setScanSpan(3000);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
mOption.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
mOption.setIsNeedLocationDescribe(true);//可选,设置是否需要地址描述
mOption.setNeedDeviceDirect(false);//可选,设置是否需要设备方向结果
mOption.setLocationNotify(false);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果
mOption.setIgnoreKillProcess(true);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
mOption.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京*附近”
mOption.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
mOption.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
}
return mOption;
}

public void start(){
synchronized (objLock) {
if(client != null && !client.isStarted()){
client.start();
}
}
}
public void stop(){
synchronized (objLock) {
if(client != null && client.isStarted()){
client.stop();
}
}
}

}



最后只需在需要调动定位的地方
初始化、注册服务、启动定位服务、以及结束时候解绑服务与停止服务就好了,代码在demo里,就能实现简单定位了