最近看了下鹰眼轨迹sdk,写了个小demo,做个笔记,以便以后需要的时候可以参考下...
1.先将需要的sdk嵌入到工程中,直接copy到libs下,并且将so库放到对应的文件夹中
2.准备工作申请key,就和前面定位中说到的一样,因为我的demo中包含了地图,定位,鹰眼,所以直接用同一个key就可以了
3.去鹰眼轨迹管理台申请鹰眼的服务ID
4.因为鹰眼是个服务,所以一定要在manifest中配置
<!-- 鹰眼定位的服务 -->
<service
android:name="com.baidu.trace.LBSTraceService"
android:enabled="true"
android:exported="true" >
</service>
5.然后就是代码了...
/**
* 鹰眼服务ID,开发者创建的鹰眼服务对应的服务ID
*/
public int serviceId = 1322;
/**
* 轨迹服务类型(0 : 不建立socket长连接, 1 : 建立socket长连接但不上传位置数据,2 : 建立socket长连接并上传位置数据)
*/
private int traceType = 2;
/**
* entity标识
*/
private String entityName ;
public static final int TIME = 2;
onCreat()
// 初始化轨迹服务客户端
mContext = getApplicationContext();
mLBSTraceClient = new LBSTraceClient(mContext);
// 设置定位模式
mLBSTraceClient.setLocationMode(LocationMode.High_Accuracy);
/**设置采集和打包周期
* TIME 表示采集周期 即 定位周期
* 8表示打包周期 即 回传周期
*/
// mLBSTraceClient.setInterval(TIME,8);
mLBSTraceClient.setInterval(10,10);
entityName = GetIMei.getImei(mContext);
// 初始化轨迹服务
trace = new Trace(mContext, serviceId, entityName, traceType);
我的以上这一坨是放到自定义的application中的
开启实时定位
//开启轨迹服务
traceClient.startTrace(mApp.getTrance(), myOnStartTraceListener);
private void initStartTrace() {
myOnStartTraceListener = new OnStartTraceListener() {
// 轨迹服务推送接口(用于接收服务端推送消息,arg0 : 消息类型,arg1 : 消息内容,详情查看类参考)
@Override
public void onTracePushCallback(byte arg0, String arg1) {
Log.i(TAG, arg1+"---服务端推送"+arg0);
}
// 开启轨迹服务回调接口(arg0 : 消息编码,arg1 : 消息内容,详情查看类参考)
@Override
public void onTraceCallback(int arg0, String arg1) {
Log.i(TAG, arg1+"--推送-"+arg0);
//服务开启成功后, 就开启实时定位的线程
refreshHandler.sendEmptyMessage(111);
}
};
}
refreshThread = new HandlerThread("refreshTrace");
refreshThread.start();
refreshHandler = new Handler(refreshThread.getLooper()){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (null != msg) {
if (msg.what == 111) {
while(isLoca){
try {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (null != map) {
map.clear();
}
}
});
queryRealtimeLoc();
Thread.sleep(SELLPTIME);
} catch (InterruptedException e) {
e.printStackTrace();
Log.i(TAG, "线程失败");
}
}
}
}
}
};
/**
* 查询实时位置
*/
public void queryRealtimeLoc(){
traceClient.queryRealtimeLoc(mApp.getServiceId(), myOnEntityListener);
}
private void initEntryTrace() {
myOnEntityListener = new OnEntityListener() {
//请求失败的回调接口
@Override
public void onRequestFailedCallback(String arg0) {
Log.i(TAG, "---"+arg0);
}
//查询entry列表的回调接口 ,这个接口对应的是列表查询的接口
@Override
public void onQueryEntityListCallback(String arg0) {
Log.i(TAG, "查询----"+arg0);
}
//这个回调对应的是 实时定位查询的接口
@Override
public void onReceiveLocation(TraceLocation location) {
super.onReceiveLocation(location);
realTimeLatlng = new LatLng(location.getLatitude(),location.getLongitude());
mHandler.obtainMessage(DRAW_MYLOCATION, realTimeLatlng).sendToTarget();
}
};
}
发送完handler之后,就直接设置覆盖物
/**
* 设置覆盖物*/
public void setMarker(LatLng latLng,String type){
BitmapDescriptor mIconLocation;
if ("start".equals(type)) { //起始点的覆盖物
mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.icon_start);
option = new MarkerOptions().position(latLng).icon(mIconLocation).zIndex(6);
}else if ("end".equals(type)) { //终点覆盖物
mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.icon_end);
option = new MarkerOptions().position(latLng).icon(mIconLocation).zIndex(6);
}else if("routeline".equals(type)){ //路线的覆盖物
mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
option = new MarkerOptions().position(latLng).icon(mIconLocation).zIndex(6);
if (null != polyline) {
map.addOverlay(polyline);
}
}else{ //其他情况就是红色的覆盖物
mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding);
option = new MarkerOptions().position(latLng).icon(mIconLocation).zIndex(6);
if (null != poiLatLng) {
if (CustomPopWindow.flag) { //如果查看服务端状态的按钮开着,才可以把围栏添加到地图上
fenceOverlayOption = new CircleOptions()
.fillColor(Color.parseColor("#77D7D7D7"))
.center(poiLatLng)
.stroke(new Stroke(10, Color.BLUE))
.radius(500);
}else{
fenceOverlayOption = null;
}
}
}
//设置缩放显示比例
MapStatusUpdate msus = MapStatusUpdateFactory.zoomTo(17.0f);
map.setMapStatus(msus);
//移动到选中位置,将选中位置设置为屏幕中心
MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);
map.animateMapStatus(msu);
marker = (Marker) map.addOverlay(option);
//将服务端围栏的overlay添加到地图上
if (null != fenceOverlayOption) {
map.addOverlay(fenceOverlayOption);
}
if (null != msUpdate) {
map.setMapStatus(msUpdate);
}
}
以上就是实时定位(实时位置查询)的主要代码,思路就是先开启鹰眼轨迹的服务,然后在他轨迹的回调中查询实时位置,调用查询的接口方法,在查询的接口回调中获取到经纬度,然后根据这个经纬度,给地图设置覆盖物,添加到地图上