package com.example.baidulocdemo_2;
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());
}
}
}///LocationService
/***
*
* @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);
}
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();
}
}
}///stop
}
package com.example.baidulocdemo_2;
import com.b