▍1. 安卓客户端和服务端交互(socket)
安卓客户端和服务端交互(socket)
一个android下开发百度地图的测试代码,与大家分享(An android development Baidu map under the test code, and share with you)
该项目提供了一种简便的方式,能够在运行期对任意给定的Android view添加徽章, 而不必配合它的布局。可添加的标签包括但不限于右上角的红色数字提醒角标
通过手机app遥控下位机进行温度的控制,手机可以读取温度值等的数据,并且可以自动发送指令,到达设定的温度后,手机可通过响铃或震动来提示用户。
这是一个用于自行车导航带地图和路线规划的android源码( 这是一个用于自行车导航带地图和路线规划的android源码 This is a navigation map for the bike and route planning Android source)
实现一个简单的电子菜谱,实现增删改查的功能(To implement a simple electronic recipes, add 、 delete、update、query functions)
主要功能有:七天天气折线图,城市列表,sharedpreference轻量级数据保存(按照个人需求,可以改成离线数据库),生活指数,语音播报,自动定位。。
这是GGLive的android版本,可以在手机上稳定使用,也是一个即时通(android gglive)
Android项目源码基于Material Desgin设计的校园交易APP,基于android studio开发的,仅供学习交流使用
CSipSimple是一款可以在Android手机上使用的支持sip的网络电话软件,可以在上面设置使用callda网络电话。连接使用方式最好是使用wifi,或者3g这样上网速度快,打起电话来效果才好。 注:本程序在Andriod studio下编译(Support SIP network phone software, you can set up the use of callda network phone. Connection to use the best way to use the WiFi, or 3G so fast Internet speed, to make a phone call to effect is good. Note: This program is compiled under Andriod studio)
package com.dyk.baidumapwork;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Toast;import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.location.Poi;import com.baidu.mapapi.SDKInitializer;import com.baidu.mapapi.map.BaiduMap;import com.baidu.mapapi.map.BitmapDescriptor;import com.baidu.mapapi.map.BitmapDescriptorFactory;import com.baidu.mapapi.map.GroundOverlayOptions;import com.baidu.mapapi.map.MapStatusUpdate;import com.baidu.mapapi.map.MapStatusUpdateFactory;import com.baidu.mapapi.map.MapView;import com.baidu.mapapi.map.Marker;import com.baidu.mapapi.map.MarkerOptions;import com.baidu.mapapi.map.MyLocationConfiguration;import com.baidu.mapapi.map.MyLocationData;import com.baidu.mapapi.map.OverlayOptions;import com.baidu.mapapi.map.PolygonOptions;import com.baidu.mapapi.map.Stroke;import com.baidu.mapapi.map.TextOptions;import com.baidu.mapapi.model.LatLng;import com.baidu.mapapi.model.LatLngBounds;import com.baidu.mapapi.radar.RadarNearbyResult;import com.baidu.mapapi.radar.RadarNearbySearchOption;import com.baidu.mapapi.radar.RadarSearchError;import com.baidu.mapapi.radar.RadarSearchListener;import com.baidu.mapapi.radar.RadarSearchManager;import com.baidu.mapapi.radar.RadarUploadInfo;import com.baidu.trace.LBSTraceClient;import com.baidu.trace.OnEntityListener;import com.baidu.trace.OnGeoFenceListener;import com.baidu.trace.OnStartTraceListener;import com.baidu.trace.OnStopTraceListener;import com.baidu.trace.OnTrackListener;import com.baidu.trace.Trace;import org.json.JSONException;import org.json.JSONObject;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends Activity implements View.OnClickListener, RadarSearchListener { private static final String TAG = "BaiduMap"; //周边雷达相关 private MapView mMapView; private BaiduMap mBaiduMap; private Marker markerA; private RadarSearchManager mManager; // 定位相关 private BitmapDescriptor mCurrentMarker; public LocationClient mLocationClient = null; public BDLocationListener myListener = new MyLocationListener(); private boolean isFirstGetLocation = true; // 方向感应器相关 private OrientationSensor mOrientationSensor; /** * 传感器方向 */ private float driection = 0; //鹰眼轨迹相关 private LBSTraceClient client; private Trace trace; /** * 鹰眼服务ID */ private long serviceId = 112700L; /** * 地理围栏监听器 */ private OnGeoFenceListener geoFenceListener; /** * 围栏ID */ private int fenceId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //在使用SDK各组件之前初始化context信息,传入ApplicationContext //注意该方法要再setContentView方法之前实现 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); // 初始化周边雷达// mManager = RadarSearchManager.getInstance(); // 实例化轨迹服务客户端 client = new LBSTraceClient(getApplicationContext()); createFence(); initView(); initClick();// testOverlay();// testGeometryOverlay();// testTextOverlay();// testGroundOverlay();// testLocation(); } private void initView() { mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); } private void initClick() { findViewById(R.id.map_common).setOnClickListener(this); findViewById(R.id.map_satelite).setOnClickListener(this); findViewById(R.id.map_blank).setOnClickListener(this); findViewById(R.id.map_traffic).setOnClickListener(this); findViewById(R.id.map_heat).setOnClickListener(this); findViewById(R.id.map_radar).setOnClickListener(this); findViewById(R.id.map_startLocation).setOnClickListener(this); findViewById(R.id.map_startTrace).setOnClickListener(this); findViewById(R.id.map_stopTrace).setOnClickListener(this); findViewById(R.id.map_findLocationAtTime).setOnClickListener(this); findViewById(R.id.map_findLocationOnHistory).setOnClickListener(this); findViewById(R.id.map_findFenceAtTime).setOnClickListener(this); findViewById(R.id.map_findFenceAtHistry).setOnClickListener(this); } /** * 覆盖物 */ private void testOverlay() { LatLng point = new LatLng(39.963175, 116.400244); // 通过marker的icons设置一组图片,再通过period设置多少帧刷新一次图片资源 ArrayList bitmapList = new ArrayList(); BitmapDescriptor bitmapA = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka); BitmapDescriptor bitmapB = BitmapDescriptorFactory.fromResource(R.drawable.icon_markb); BitmapDescriptor bitmapC = BitmapDescriptorFactory.fromResource(R.drawable.icon_markc); BitmapDescriptor bitmapD = BitmapDescriptorFactory.fromResource(R.drawable.icon_markd); BitmapDescriptor bitmapE = BitmapDescriptorFactory.fromResource(R.drawable.icon_marke); bitmapList.add(bitmapA); bitmapList.add(bitmapB); bitmapList.add(bitmapC); bitmapList.add(bitmapD); bitmapList.add(bitmapE); OverlayOptions options = new MarkerOptions() .position(point) //位置 .icons(bitmapList) // 图标 .period(10) // 刷新帧数 .zIndex(9)// 显示层级 .alpha(0.5f) //透明度 .draggable(true);// 支持拖拽 markerA = (Marker) (mBaiduMap.addOverlay(options));// 在地图上添加marker并显示 // 点击marker mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { marker.remove(); return true; } }); } /** * 几何覆盖物 */ private void testGeometryOverlay() { //定义多边形的五个顶点 LatLng pt1 = new LatLng(39.93923, 116.357428); LatLng pt2 = new LatLng(39.91923, 116.327428); LatLng pt3 = new LatLng(39.89923, 116.347428); LatLng pt4 = new LatLng(39.89923, 116.367428); LatLng pt5 = new LatLng(39.91923, 116.387428); List pts = new ArrayList(); pts.add(pt1); pts.add(pt2); pts.add(pt3); pts.add(pt4); pts.add(pt5); //构建用户绘制多边形的Option对象 OverlayOptions polygonOption = new PolygonOptions() .points(pts) .stroke(new Stroke(5, 0xAA00FF00)) .fillColor(0xAAFFFF00); //在地图上添加多边形Option,用于显示 mBaiduMap.addOverlay(polygonOption); } /** * 文字覆盖物 */ private void testTextOverlay() { LatLng text = new LatLng(39.86923, 116.397428); OverlayOptions options = new TextOptions() .bgColor(0xAAFFFF00) .fontSize(24) .fontColor(0xFFFF00FF) .text("文字覆盖物测试") .rotate(-45) // 旋转角度 .position(text); mBaiduMap.addOverlay(options); } /** * 地形图图层(GroundOverlay),又可叫做图片图层 */ private void testGroundOverlay() { //定义Ground的显示地理范围 LatLng southwest = new LatLng(39.92235, 116.380338); LatLng northeast = new LatLng(39.947246, 116.414977); LatLngBounds bounds = new LatLngBounds.Builder() .include(northeast) .include(southwest) .build(); //定义Ground显示的图片 BitmapDescriptor bdGround = BitmapDescriptorFactory .fromResource(R.drawable.ground_overlay); //定义Ground覆盖物选项 OverlayOptions ooGround = new GroundOverlayOptions() .positionFromBounds(bounds) .image(bdGround) .transparency(0.8f);// 透明度 //在地图中添加Ground覆盖物 mBaiduMap.addOverlay(ooGround); } /** * 周边雷达信息上传 */ private void testRadarInfoUpload() { // 周边雷达设置监听 mManager.addNearbyInfoListener(this); //周边雷达设置用户身份标识,id为空默认是设备标识 mManager.setUserID("123"); //上传位置 RadarUploadInfo info = new RadarUploadInfo(); LatLng pt = new LatLng(39.963175, 116.400244); info.comments = "备注"; info.pt = pt; mManager.uploadInfoRequest(info); } /** * 雷达周边位置检索 */ private void testRadarLocationSearch() { // 自己的位置 LatLng pt = new LatLng(39.963175, 116.400244); RadarNearbySearchOption option = new RadarNearbySearchOption() .centerPt(pt) .pageNum(0) .radius(2 * 1000); // 发起查询请求 mManager.nearbyInfoRequest(option); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 mMapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 mMapView.onDestroy(); if (mManager != null) { //移除监听 mManager.removeNearbyInfoListener(this); //清除用户信息// mManager.clearUserInfo(); //释放资源 mManager.destroy(); mManager = null; } if (mLocationClient != null && mLocationClient.isStarted()) { mLocationClient.stop(); } if (mBaiduMap.isMyLocationEnabled()) { // 当不需要定位图层时关闭定位图层 mBaiduMap.setMyLocationEnabled(false); } //反注册OrientationSensor if (mOrientationSensor != null) { mOrientationSensor.stop(); } } @Override public void onClick(View v) { switch (v.getId()) { // 普通地图 case R.id.map_common: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); break; // 卫星地图 case R.id.map_satelite: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); break; // 空白地图 case R.id.map_blank: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NONE); break; // 交通图 case R.id.map_traffic: if (mBaiduMap.isTrafficEnabled()) { mBaiduMap.setTrafficEnabled(false); } else { mBaiduMap.setTrafficEnabled(true); } break; // 热力图 case R.id.map_heat: if (mBaiduMap.isBaiduHeatMapEnabled()) { mBaiduMap.setBaiduHeatMapEnabled(false); } else { mBaiduMap.setBaiduHeatMapEnabled(true); } break; // 周边检索 case R.id.map_radar: testRadarInfoUpload(); testRadarLocationSearch(); break; // 开始定位 case R.id.map_startLocation: mOrientationSensor = new OrientationSensor(this); mOrientationSensor.start(); mLocationClient.start(); break; // 开始鹰眼轨迹追踪 case R.id.map_startTrace: startTrace(); break; // 关闭鹰眼轨迹追踪 case R.id.map_stopTrace: stopTrace(); break; // 鹰眼查询实时位置 case R.id.map_findLocationAtTime: findLocationAtTime(); break; // 鹰眼查询历史轨迹 case R.id.map_findLocationOnHistory: findLocationOnHistory(); break; // 围栏查询实时状态 case R.id.map_findFenceAtTime: // 异步创建Fence 所以应该正规应该使用handlder findFenceAtTime(); break; // 围栏历史报警信息 case R.id.map_findFenceAtHistry: findFenceOnHistory(); break; } } @Override public void onGetNearbyInfoList(RadarNearbyResult radarNearbyResult, RadarSearchError radarSearchError) { if (radarSearchError == RadarSearchError.RADAR_NO_ERROR) { Toast.makeText(MainActivity.this, "查询周边成功", Toast.LENGTH_LONG).show(); Log.i(TAG, radarNearbyResult.infoList.get(0).userID "" radarNearbyResult.infoList.get(0).distance "" radarNearbyResult.infoList.get(0).pt "" radarNearbyResult.infoList.get(0).timeStamp); } else { Toast.makeText(MainActivity.this, "查询周边失败", Toast.LENGTH_LONG).show(); Log.i(TAG, "查询错误:" radarSearchError.toString()); } } @Override public void onGetUploadState(RadarSearchError radarSearchError) { if (radarSearchError == RadarSearchError.RADAR_NO_ERROR) { //上传成功 Toast.makeText(MainActivity.this, "单次上传位置成功", Toast.LENGTH_LONG).show(); } else { //上传失败 Toast.makeText(MainActivity.this, "单次上传位置失败", Toast.LENGTH_LONG).show(); Log.i(TAG, "上传错误:" radarSearchError.toString()); } } @Override public void onGetClearInfoState(RadarSearchError radarSearchError) { if (radarSearchError == RadarSearchError.RADAR_NO_ERROR) { //清除成功 Toast.makeText(MainActivity.this, "清除位置成功", Toast.LENGTH_LONG).show(); } else { //清除失败 Toast.makeText(MainActivity.this, "清除位置失败", Toast.LENGTH_LONG).show(); } } /** * 定位结合方向传感器 */ private void testLocation() { mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 mLocationClient.registerLocationListener(myListener); //注册监听函数 /** * 高精度定位模式:这种定位模式下,会同时使用网络定位和GPS定位,优先返回最高精度的定位结果; * 低功耗定位模式:这种定位模式下,不会使用GPS,只会使用网络定位(Wi-Fi和基站定位); * 仅用设备定位模式:这种定位模式下,不需要连接网络,只使用GPS进行定位,这种模式下不支持室内环境的定位。 */ LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系 int span = 1000; option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true);//可选,默认false,设置是否使用gps option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果 option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死 option.SetIgnoreCacheException(true);//可选,默认false,设置是否收集CRASH信息,默认收集 option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要 mLocationClient.setLocOption(option); } /** * 开始鹰眼轨迹追踪 */ private void startTrace() { // entity标识(监听后台显示的名字) String entityName = "test"; // 轨迹服务类型(0 : 不上传位置数据,也不接收报警信息; 1 : 不上传位置数据,但接收报警信息; // 2 : 上传位置数据,且接收报警信息) int traceType = 2; //实例化轨迹服务 trace = new Trace(getApplicationContext(), serviceId, entityName, traceType); // 实例化开启轨迹服务回调接口 OnStartTraceListener startTraceListener = new OnStartTraceListener() { // 开启轨迹服务回调接口(arg0 : 消息编码,arg1 : 消息内容,详情查看类参考) @Override public void onTraceCallback(int arg0, String arg1) { Log.i(TAG, "onTraceCallback" "arg0 = " arg0 " arg1 = " arg1); } // 轨迹服务推送接口(用于接收服务端推送消息,arg0 : 消息类型,arg1 : 消息内容,详情查看类参考) @Override public void onTracePushCallback(byte arg0, String arg1) { Log.i(TAG, "onTracePushCallback" "arg0 = " arg0 " arg1 = " arg1); } }; // 位置采集周期 int gatherInterval = 10; // 打包周期 int packInterval = 60; // 设置协议类型,0为http,1为https int protocoType = 0; // 设置位置采集和打包周期 client.setInterval(gatherInterval, packInterval); // 设置协议 client.setProtocolType(protocoType); // 开启轨迹服务 client.startTrace(trace, startTraceListener); } /** * 测试数据 */ private Map getMap() { Map map = new HashMap(); map.put("key1", "value1"); map.put("key2", "value2"); return map; } /** * 结束鹰眼轨迹追踪 */ private void stopTrace() { // 实例化停止轨迹服务回调接口 OnStopTraceListener stopTraceListener = new OnStopTraceListener() { // 轨迹服务停止成功 @Override public void onStopTraceSuccess() { Log.i(TAG, "onStopTraceSuccess"); } // 轨迹服务停止失败(arg0 : 错误编码,arg1 : 消息内容,详情查看类参考) @Override public void onStopTraceFailed(int arg0, String arg1) { Log.i(TAG, "onStopTraceFailed" "arg0 = " arg0 " arg1 = " arg1); } }; //停止轨迹服务 client.stopTrace(trace, stopTraceListener); } /** * 鹰眼查询实时位置 */ private void findLocationAtTime() { //entity标识列表(多个entityName,以英文逗号"," 分割) String entityNames = "test"; //检索条件(格式为 : "key1=value1,key2=value2,.....") String columnKey = ""; //返回结果的类型(0 : 返回全部结果,1 : 只返回entityName的列表) int returnType = 0; //活跃时间,UNIX时间戳(指定该字段时,返回从该时间点之后仍有位置变动的entity的实时点集合) int activeTime = (int) (System.currentTimeMillis() / 1000 - 12 * 60 * 60); //分页大小 int pageSize = 1000; //分页索引 int pageIndex = 1; //Entity监听器 OnEntityListener entityListener = new OnEntityListener() { // 查询失败回调接口 @Override public void onRequestFailedCallback(String arg0) { Log.i(TAG, "onRequestFailedCallback" "arg0 = " arg0); } // 查询entity回调接口,返回查询结果列表 @Override public void onQueryEntityListCallback(String arg0) { Log.i(TAG, "onQueryEntityListCallback" " arg0 = " arg0); } }; //查询实时轨迹 client.queryEntityList(serviceId, entityNames, columnKey, returnType, activeTime, pageSize, pageIndex, entityListener); } /** * 鹰眼查询历史轨迹 */ private void findLocationOnHistory() { //entity标识 String entityName = "test"; //是否返回精简的结果(0 : 将只返回经纬度,1 : 将返回经纬度及其他属性信息) int simpleReturn = 1; //开始时间(Unix时间戳) int startTime = (int) (System.currentTimeMillis() / 1000 - 12 * 60 * 60); //结束时间(Unix时间戳) int endTime = (int) (System.currentTimeMillis() / 1000); //分页大小 int pageSize = 1000; //分页索引 int pageIndex = 1; //轨迹查询监听器 OnTrackListener trackListener = new OnTrackListener() { //请求失败回调接口 @Override public void onRequestFailedCallback(String arg0) { Log.i(TAG, "onRequestFailedCallback" "arg0 = " arg0); } // 查询历史轨迹回调接口 @Override public void onQueryHistoryTrackCallback(String arg0) { Log.i(TAG, "onQueryHistoryTrackCallback" "arg0 = " arg0); } }; //查询历史轨迹 client.queryHistoryTrack(serviceId, entityName, simpleReturn, startTime, endTime, pageSize, pageIndex, trackListener); } /** * 创建围栏 */ private void createFence() { geoFenceListener = new OnGeoFenceListener() { //请求失败回调接口 @Override public void onRequestFailedCallback(String arg0) { Log.i(TAG, "geoFence请求失败 :" "arg0 = " arg0); } //创建圆形围栏回调接口 @Override public void onCreateCircularFenceCallback(String arg0) { Log.i(TAG, "创建圆形围栏回调接口消息 : " "arg0 = " arg0); JSONObject dataJson = null; try { dataJson = new JSONObject(arg0); int status = dataJson.getInt("status"); if (0 == status) { fenceId = dataJson.getInt("fence_id"); } } catch (JSONException e) { e.printStackTrace(); } } //更新圆形围栏回调接口 @Override public void onUpdateCircularFenceCallback(String arg0) { Log.i(TAG, "更新圆形围栏回调接口消息 :" "arg0 = " arg0); } //延迟报警回调接口 @Override public void onDelayAlarmCallback(String arg0) { Log.i(TAG, "延迟报警回调接口消息 :" "arg0 = " arg0); } //删除围栏回调接口 @Override public void onDeleteFenceCallback(String arg0) { Log.i(TAG, "删除围栏回调接口消息 :" "arg0 = " arg0); } //查询围栏列表回调接口 @Override public void onQueryFenceListCallback(String arg0) { Log.i(TAG, "查询围栏列表回调接口消息 : " "arg0 = " arg0); } //查询历史报警回调接口 @Override public void onQueryHistoryAlarmCallback(String arg0) { Log.i(TAG, "查询历史报警回调接口消息 : " "arg0 = " arg0); } //查询监控对象状态回调接口 @Override public void onQueryMonitoredStatusCallback(String arg0) { Log.i(TAG, "查询监控对象状态回调接口消息 : " "arg0 = " arg0); } }; //创建者(entity标识) String creator = "test"; //围栏名称 String fenceName = "school"; //围栏描述 String fenceDesc = "学校"; //监控对象列表(多个entityName,以英文逗号"," 分割) String monitoredPersons = "test"; //观察者列表(多个entityName,以英文逗号"," 分割) String observers = "test"; //生效时间列表 String validTimes = ""; //生效周期 int validCycle = 4; //围栏生效日期 String validDate = ""; //生效日期列表 String validDays = ""; //坐标类型 (1:GPS经纬度,2:国测局经纬度,3:百度经纬度) int coordType = 3; //围栏圆心(圆心位置, 格式 : "经度,纬度") String center = "120.2126083541,30.245619808604"; //围栏半径(单位 : 米) double radius = 500; //报警条件(1:进入时触发提醒,2:离开时触发提醒,3:进入离开均触发提醒) int alarmCondition = 3; //创建圆形地理围栏 client.createCircularFence(serviceId, creator, fenceName, fenceDesc, monitoredPersons, observers,validTimes, validCycle, validDate, validDays, coordType, center, radius, alarmCondition, geoFenceListener); } /** 围栏查询实时状态 */ private void findFenceAtTime() { if (fenceId != 0) { //监控对象列表(多个entityName,以英文逗号"," 分割) String monitoredPersons = "test"; //查询实时状态 client.queryMonitoredStatus(serviceId, fenceId, monitoredPersons, geoFenceListener); } } /** 围栏历史报警信息 */ private void findFenceOnHistory() { if (fenceId != 0) { //监控对象列表(多个entityName,以英文逗号"," 分割) String monitoredPersons = "test"; //开始时间(unix时间戳) int beginTime = (int) (System.currentTimeMillis() / 1000 - 12 * 60 * 60); //结束时间(unix时间戳) int endTime = (int) (System.currentTimeMillis() / 1000); //查询历史报警信息 client.queryFenceHistoryAlarmInfo(serviceId, fenceId, monitoredPersons, beginTime, endTime, geoFenceListener); } } /** * 接收定位信息 */ private class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { //mapview 销毁后不在处理新接收的位置 if (location == null || mBaiduMap == null) { return; } getLocationInfo(location); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); // 构造定位数据 mOrientationSensor.setOrientationListener(new OrientationSensor.OrientationSensorListener() { @Override public void getOrientation(float x) { driection = x; } }); final MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(driection) .latitude(location.getLatitude()) .longitude(location.getLongitude()) .build(); Log.i(TAG, "driection=" driection); // 设置定位数据 mBaiduMap.setMyLocationData(locData); if (isFirstGetLocation) { isFirstGetLocation = false; // 获取经纬度 LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); // 设置地图中心点以及缩放级别 MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngZoom(latLng, 15); mBaiduMap.animateMapStatus(mapStatusUpdate); }// Log.i(TAG, "latitude:" mBaiduMap.getLocationData().latitude // "longitude:" mBaiduMap.getLocationData().longitude // "direction:" mBaiduMap.getLocationData().direction); // 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标) mCurrentMarker = BitmapDescriptorFactory.fromResource(R.mipmap.location_pic); MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, mCurrentMarker); mBaiduMap.setMyLocationConfigeration(config); } private void getLocationInfo(BDLocation location) { //Receive Location StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("error code : "); sb.append(location.getLocType()); sb.append("latitude : "); sb.append(location.getLatitude()); sb.append("lontitude : "); sb.append(location.getLongitude()); sb.append("radius : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果 sb.append("speed : "); sb.append(location.getSpeed());// 单位:公里每小时 sb.append("satellite : "); sb.append(location.getSatelliteNumber()); sb.append("height : "); sb.append(location.getAltitude());// 单位:米 sb.append("direction : "); sb.append(location.getDirection());// 单位度 sb.append("addr : "); sb.append(location.getAddrStr()); sb.append("describe : "); sb.append("gps定位成功"); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果 sb.append("addr : "); sb.append(location.getAddrStr()); //运营商信息 sb.append("operationers : "); sb.append(location.getOperators()); sb.append("describe : "); sb.append("网络定位成功"); } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果 sb.append("describe : "); sb.append("离线定位成功,离线定位结果也是有效的"); } else if (location.getLocType() == BDLocation.TypeServerError) { sb.append("describe : "); sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因"); } else if (location.getLocType() == BDLocation.TypeNetWorkException) { sb.append("describe : "); sb.append("网络不同导致定位失败,请检查网络是否通畅"); } else if (location.getLocType() == BDLocation.TypeCriteriaException) { sb.append("describe : "); sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机"); } sb.append("locationdescribe : "); sb.append(location.getLocationDescribe());// 位置语义化信息,结果类似于“在北京天安门附近” List list = location.getPoiList();// POI数据 if (list != null) { sb.append("poilist size = : "); sb.append(list.size()); for (Poi p : list) { sb.append("poi= : "); sb.append(p.getId() " " p.getName() " " p.getRank()); } } Log.i(TAG, sb.toString()); } }} package com.dyk.baidumapwork; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Toast; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.location.Poi; import com.baidu.mapapi.SDKInitializer; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.GroundOverlayOptions; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.Marker; import com.baidu.mapapi.map.MarkerOptions; import com.baidu.mapapi.map.MyLocationConfiguration; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.map.OverlayOptions; import com.baidu.mapapi.map.PolygonOptions; import com.baidu.mapapi.map.Stroke; import com.baidu.mapapi.map.TextOptions; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.model.LatLngBounds; import com.baidu.mapapi.radar.RadarNearbyResult; import com.baidu.mapapi.radar.RadarNearbySearchOption; import com.baidu.mapapi.radar.RadarSearchError; import com.baidu.mapapi.radar.RadarSearchListener; import com.baidu.mapapi.radar.RadarSearchManager; import com.baidu.mapapi.radar.RadarUploadInfo; import com.baidu.trace.LBSTraceClient; import com.baidu.trace.OnEntityListener; import com.baidu.trace.OnGeoFenceListener; import com.baidu.trace.OnStartTraceListener; import com.baidu.trace.OnStopTraceListener; import com.baidu.trace.OnTrackListener; import com.baidu.trace.Trace; import org.json.JSONException; import org.json.JSONObject; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends Activity implements View.OnClickListener, RadarSearchListener { private static final String TAG = "BaiduMap"; //周边雷达相关 private MapView mMapView; private BaiduMap mBaiduMap; private Marker markerA; private RadarSearchManager mManager; // 定位相关 private BitmapDescriptor mCurrentMarker; public LocationClient mLocationClient = null; public BDLocationListener myListener = new MyLocationListener(); private boolean isFirstGetLocation = true; // 方向感应器相关 private OrientationSensor mOrientationSensor; /** * 传感器方向 */ private float driection = 0; //鹰眼轨迹相关 private LBSTraceClient client; private Trace trace; /** * 鹰眼服务ID */ private long serviceId = 112700L; /** * 地理围栏监听器 */ private OnGeoFenceListener geoFenceListener; /** * 围栏ID */ private int fenceId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //在使用SDK各组件之前初始化context信息,传入ApplicationContext //注意该方法要再setContentView方法之前实现 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); // 初始化周边雷达 // mManager = RadarSearchManager.getInstance(); // 实例化轨迹服务客户端 client = new LBSTraceClient(getApplicationContext()); createFence(); initView(); initClick(); // testOverlay(); // testGeometryOverlay(); // testTextOverlay(); // testGroundOverlay(); // testLocation(); } private void initView() { mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); } private void initClick() { findViewById(R.id.map_common).setOnClickListener(this); findViewById(R.id.map_satelite).setOnClickListener(this); findViewById(R.id.map_blank).setOnClickListener(this); findViewById(R.id.map_traffic).setOnClickListener(this); findViewById(R.id.map_heat).setOnClickListener(this); findViewById(R.id.map_radar).setOnClickListener(this); findViewById(R.id.map_startLocation).setOnClickListener(this); findViewById(R.id.map_startTrace).setOnClickListener(this); findViewById(R.id.map_stopTrace).setOnClickListener(this); findViewById(R.id.map_findLocationAtTime).setOnClickListener(this); findViewById(R.id.map_findLocationOnHistory).setOnClickListener(this); findViewById(R.id.map_findFenceAtTime).setOnClickListener(this); findViewById(R.id.map_findFenceAtHistry).setOnClickListener(this); } /** * 覆盖物 */ private void testOverlay() { LatLng point = new LatLng(39.963175, 116.400244); // 通过marker的icons设置一组图片,再通过period设置多少帧刷新一次图片资源 ArrayList bitmapList = new ArrayList(); BitmapDescriptor bitmapA = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka); BitmapDescriptor bitmapB = BitmapDescriptorFactory.fromResource(R.drawable.icon_markb); BitmapDescriptor bitmapC = BitmapDescriptorFactory.fromResource(R.drawable.icon_markc); BitmapDescriptor bitmapD = BitmapDescriptorFactory.fromResource(R.drawable.icon_markd); BitmapDescriptor bitmapE = BitmapDescriptorFactory.fromResource(R.drawable.icon_marke); bitmapList.add(bitmapA); bitmapList.add(bitmapB); bitmapList.add(bitmapC); bitmapList.add(bitmapD); bitmapList.add(bitmapE); OverlayOptions options = new MarkerOptions() .position(point) //位置 .icons(bitmapList) // 图标 .period(10) // 刷新帧数 .zIndex(9)// 显示层级 .alpha(0.5f) //透明度 .draggable(true);// 支持拖拽 markerA = (Marker) (mBaiduMap.addOverlay(options));// 在地图上添加marker并显示 // 点击marker mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { marker.remove(); return true; } }); } /** * 几何覆盖物 */ private void testGeometryOverlay() { //定义多边形的五个顶点 LatLng pt1 = new LatLng(39.93923, 116.357428); LatLng pt2 = new LatLng(39.91923, 116.327428); LatLng pt3 = new LatLng(39.89923, 116.347428); LatLng pt4 = new LatLng(39.89923, 116.367428); LatLng pt5 = new LatLng(39.91923, 116.387428); List pts = new ArrayList(); pts.add(pt1); pts.add(pt2); pts.add(pt3); pts.add(pt4); pts.add(pt5); //构建用户绘制多边形的Option对象 OverlayOptions polygonOption = new PolygonOptions() .points(pts) .stroke(new Stroke(5, 0xAA00FF00)) .fillColor(0xAAFFFF00); //在地图上添加多边形Option,用于显示 mBaiduMap.addOverlay(polygonOption); } /** * 文字覆盖物 */ private void testTextOverlay() { LatLng text = new LatLng(39.86923, 116.397428); OverlayOptions options = new TextOptions() .bgColor(0xAAFFFF00) .fontSize(24) .fontColor(0xFFFF00FF) .text("文字覆盖物测试") .rotate(-45) // 旋转角度 .position(text); mBaiduMap.addOverlay(options); } /** * 地形图图层(GroundOverlay),又可叫做图片图层 */ private void testGroundOverlay() { //定义Ground的显示地理范围 LatLng southwest = new LatLng(39.92235, 116.380338); LatLng northeast = new LatLng(39.947246, 116.414977); LatLngBounds bounds = new LatLngBounds.Builder() .include(northeast) .include(southwest) .build(); //定义Ground显示的图片 BitmapDescriptor bdGround = BitmapDescriptorFactory .fromResource(R.drawable.ground_overlay); //定义Ground覆盖物选项 OverlayOptions ooGround = new GroundOverlayOptions() .positionFromBounds(bounds) .image(bdGround) .transparency(0.8f);// 透明度 //在地图中添加Ground覆盖物 mBaiduMap.addOverlay(ooGround); } /** * 周边雷达信息上传 */ private void testRadarInfoUpload() { // 周边雷达设置监听 mManager.addNearbyInfoListener(this); //周边雷达设置用户身份标识,id为空默认是设备标识 mManager.setUserID("123"); //上传位置 RadarUploadInfo info = new RadarUploadInfo(); LatLng pt = new LatLng(39.963175, 116.400244); info.comments = "备注"; info.pt = pt; mManager.uploadInfoRequest(info); } /** * 雷达周边位置检索 */ private void testRadarLocationSearch() { // 自己的位置 LatLng pt = new LatLng(39.963175, 116.400244); RadarNearbySearchOption option = new RadarNearbySearchOption() .centerPt(pt) .pageNum(0) .radius(2 * 1000); // 发起查询请求 mManager.nearbyInfoRequest(option); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 mMapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 mMapView.onDestroy(); if (mManager != null) { //移除监听 mManager.removeNearbyInfoListener(this); //清除用户信息 // mManager.clearUserInfo(); //释放资源 mManager.destroy(); mManager = null; } if (mLocationClient != null && mLocationClient.isStarted()) { mLocationClient.stop(); } if (mBaiduMap.isMyLocationEnabled()) { // 当不需要定位图层时关闭定位图层 mBaiduMap.setMyLocationEnabled(false); } //反注册OrientationSensor if (mOrientationSensor != null) { mOrientationSensor.stop(); } } @Override public void onClick(View v) { switch (v.getId()) { // 普通地图 case R.id.map_common: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); break; // 卫星地图 case R.id.map_satelite: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); break; // 空白地图 case R.id.map_blank: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NONE); break; // 交通图 case R.id.map_traffic: if (mBaiduMap.isTrafficEnabled()) { mBaiduMap.setTrafficEnabled(false); } else { mBaiduMap.setTrafficEnabled(true); } break; // 热力图 case R.id.map_heat: if (mBaiduMap.isBaiduHeatMapEnabled()) { mBaiduMap.setBaiduHeatMapEnabled(false); } else { mBaiduMap.setBaiduHeatMapEnabled(true); } break; // 周边检索 case R.id.map_radar: testRadarInfoUpload(); testRadarLocationSearch(); break; // 开始定位 case R.id.map_startLocation: mOrientationSensor = new OrientationSensor(this); mOrientationSensor.start(); mLocationClient.start(); break; // 开始鹰眼轨迹追踪 case R.id.map_startTrace: startTrace(); break; // 关闭鹰眼轨迹追踪 case R.id.map_stopTrace: stopTrace(); break; // 鹰眼查询实时位置 case R.id.map_findLocationAtTime: findLocationAtTime(); break; // 鹰眼查询历史轨迹 case R.id.map_findLocationOnHistory: findLocationOnHistory(); break; // 围栏查询实时状态 case R.id.map_findFenceAtTime: // 异步创建Fence 所以应该正规应该使用handlder findFenceAtTime(); break; // 围栏历史报警信息 case R.id.map_findFenceAtHistry: findFenceOnHistory(); break; } } @Override public void onGetNearbyInfoList(RadarNearbyResult radarNearbyResult, RadarSearchError radarSearchError) { if (radarSearchError == RadarSearchError.RADAR_NO_ERROR) { Toast.makeText(MainActivity.this, "查询周边成功", Toast.LENGTH_LONG).show(); Log.i(TAG, radarNearbyResult.infoList.get(0).userID "" radarNearbyResult.infoList.get(0).distance "" radarNearbyResult.infoList.get(0).pt "" radarNearbyResult.infoList.get(0).timeStamp); } else { Toast.makeText(MainActivity.this, "查询周边失败", Toast.LENGTH_LONG).show(); Log.i(TAG, "查询错误:" radarSearchError.toString()); } } @Override public void onGetUploadState(RadarSearchError radarSearchError) { if (radarSearchError == RadarSearchError.RADAR_NO_ERROR) { //上传成功 Toast.makeText(MainActivity.this, "单次上传位置成功", Toast.LENGTH_LONG).show(); } else { //上传失败 Toast.makeText(MainActivity.this, "单次上传位置失败", Toast.LENGTH_LONG).show(); Log.i(TAG, "上传错误:" radarSearchError.toString()); } } @Override public void onGetClearInfoState(RadarSearchError radarSearchError) { if (radarSearchError == RadarSearchError.RADAR_NO_ERROR) { //清除成功 Toast.makeText(MainActivity.this, "清除位置成功", Toast.LENGTH_LONG).show(); } else { //清除失败 Toast.makeText(MainActivity.this, "清除位置失败", Toast.LENGTH_LONG).show(); } } /** * 定位结合方向传感器 */ private void testLocation() { mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 mLocationClient.registerLocationListener(myListener); //注册监听函数 /** * 高精度定位模式:这种定位模式下,会同时使用网络定位和GPS定位,优先返回最高精度的定位结果; * 低功耗定位模式:这种定位模式下,不会使用GPS,只会使用网络定位(Wi-Fi和基站定位); * 仅用设备定位模式:这种定位模式下,不需要连接网络,只使用GPS进行定位,这种模式下不支持室内环境的定位。 */ LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系 int span = 1000; option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true);//可选,默认false,设置是否使用gps option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果 option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死 option.SetIgnoreCacheException(true);//可选,默认false,设置是否收集CRASH信息,默认收集 option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要 mLocationClient.setLocOption(option); } /** * 开始鹰眼轨迹追踪 */ private void startTrace() { // entity标识(监听后台显示的名字) String entityName = "test"; // 轨迹服务类型(0 : 不上传位置数据,也不接收报警信息; 1 : 不上传位置数据,但接收报警信息; // 2 : 上传位置数据,且接收报警信息) int traceType = 2; //实例化轨迹服务 trace = new Trace(getApplicationContext(), serviceId, entityName, traceType); // 实例化开启轨迹服务回调接口 OnStartTraceListener startTraceListener = new OnStartTraceListener() { // 开启轨迹服务回调接口(arg0 : 消息编码,arg1 : 消息内容,详情查看类参考) @Override public void onTraceCallback(int arg0, String arg1) { Log.i(TAG, "onTraceCallback" "arg0 = " arg0 " arg1 = " arg1); } // 轨迹服务推送接口(用于接收服务端推送消息,arg0 : 消息类型,arg1 : 消息内容,详情查看类参考) @Override public void onTracePushCallback(byte arg0, String arg1) { Log.i(TAG, "onTracePushCallback" "arg0 = " arg0 " arg1 = " arg1); } }; // 位置采集周期 int gatherInterval = 10; // 打包周期 int packInterval = 60; // 设置协议类型,0为http,1为https int protocoType = 0; // 设置位置采集和打包周期 client.setInterval(gatherInterval, packInterval); // 设置协议 client.setProtocolType(protocoType); // 开启轨迹服务 client.startTrace(trace, startTraceListener); } /** * 测试数据 */ private Map getMap() { Map map = new HashMap(); map.put("key1", "value1"); map.put("key2", "value2"); return map; } /** * 结束鹰眼轨迹追踪 */ private void stopTrace() { // 实例化停止轨迹服务回调接口 OnStopTraceListener stopTraceListener = new OnStopTraceListener() { // 轨迹服务停止成功 @Override public void onStopTraceSuccess() { Log.i(TAG, "onStopTraceSuccess"); } // 轨迹服务停止失败(arg0 : 错误编码,arg1 : 消息内容,详情查看类参考) @Override public void onStopTraceFailed(int arg0, String arg1) { Log.i(TAG, "onStopTraceFailed" "arg0 = " arg0 " arg1 = " arg1); } }; //停止轨迹服务 client.stopTrace(trace, stopTraceListener); } /** * 鹰眼查询实时位置 */ private void findLocationAtTime() { //entity标识列表(多个entityName,以英文逗号"," 分割) String entityNames = "test"; //检索条件(格式为 : "key1=value1,key2=value2,.....") String columnKey = ""; //返回结果的类型(0 : 返回全部结果,1 : 只返回entityName的列表) int returnType = 0; //活跃时间,UNIX时间戳(指定该字段时,返回从该时间点之后仍有位置变动的entity的实时点集合) int activeTime = (int) (System.currentTimeMillis() / 1000 - 12 * 60 * 60); //分页大小 int pageSize = 1000; //分页索引 int pageIndex = 1; //Entity监听器 OnEntityListener entityListener = new OnEntityListener() { // 查询失败回调接口 @Override public void onRequestFailedCallback(String arg0) { Log.i(TAG, "onRequestFailedCallback" "arg0 = " arg0); } // 查询entity回调接口,返回查询结果列表 @Override public void onQueryEntityListCallback(String arg0) { Log.i(TAG, "onQueryEntityListCallback" " arg0 = " arg0); } }; //查询实时轨迹 client.queryEntityList(serviceId, entityNames, columnKey, returnType, activeTime, pageSize, pageIndex, entityListener); } /** * 鹰眼查询历史轨迹 */ private void findLocationOnHistory() { //entity标识 String entityName = "test"; //是否返回精简的结果(0 : 将只返回经纬度,1 : 将返回经纬度及其他属性信息) int simpleReturn = 1; //开始时间(Unix时间戳) int startTime = (int) (System.currentTimeMillis() / 1000 - 12 * 60 * 60); //结束时间(Unix时间戳) int endTime = (int) (System.currentTimeMillis() / 1000); //分页大小 int pageSize = 1000; //分页索引 int pageIndex = 1; //轨迹查询监听器 OnTrackListener trackListener = new OnTrackListener() { //请求失败回调接口 @Override public void onRequestFailedCallback(String arg0) { Log.i(TAG, "onRequestFailedCallback" "arg0 = " arg0); } // 查询历史轨迹回调接口 @Override public void onQueryHistoryTrackCallback(String arg0) { Log.i(TAG, "onQueryHistoryTrackCallback" "arg0 = " arg0); } }; //查询历史轨迹 client.queryHistoryTrack(serviceId, entityName, simpleReturn, startTime, endTime, pageSize, pageIndex, trackListener); } /** * 创建围栏 */ private void createFence() { geoFenceListener = new OnGeoFenceListener() { //请求失败回调接口 @Override public void onRequestFailedCallback(String arg0) { Log.i(TAG, "geoFence请求失败 :" "arg0 = " arg0); } //创建圆形围栏回调接口 @Override public void onCreateCircularFenceCallback(String arg0) { Log.i(TAG, "创建圆形围栏回调接口消息 : " "arg0 = " arg0); JSONObject dataJson = null; try { dataJson = new JSONObject(arg0); int status = dataJson.getInt("status"); if (0 == status) { fenceId = dataJson.getInt("fence_id"); } } catch (JSONException e) { e.printStackTrace(); } } //更新圆形围栏回调接口 @Override public void onUpdateCircularFenceCallback(String arg0) { Log.i(TAG, "更新圆形围栏回调接口消息 :" "arg0 = " arg0); } //延迟报警回调接口 @Override public void onDelayAlarmCallback(String arg0) { Log.i(TAG, "延迟报警回调接口消息 :" "arg0 = " arg0); } //删除围栏回调接口 @Override public void onDeleteFenceCallback(String arg0) { Log.i(TAG, "删除围栏回调接口消息 :" "arg0 = " arg0); } //查询围栏列表回调接口 @Override public void onQueryFenceListCallback(String arg0) { Log.i(TAG, "查询围栏列表回调接口消息 : " "arg0 = " arg0); } //查询历史报警回调接口 @Override public void onQueryHistoryAlarmCallback(String arg0) { Log.i(TAG, "查询历史报警回调接口消息 : " "arg0 = " arg0); } //查询监控对象状态回调接口 @Override public void onQueryMonitoredStatusCallback(String arg0) { Log.i(TAG, "查询监控对象状态回调接口消息 : " "arg0 = " arg0); } }; //创建者(entity标识) String creator = "test"; //围栏名称 String fenceName = "school"; //围栏描述 String fenceDesc = "学校"; //监控对象列表(多个entityName,以英文逗号"," 分割) String monitoredPersons = "test"; //观察者列表(多个entityName,以英文逗号"," 分割) String observers = "test"; //生效时间列表 String validTimes = ""; //生效周期 int validCycle = 4; //围栏生效日期 String validDate = ""; //生效日期列表 String validDays = ""; //坐标类型 (1:GPS经纬度,2:国测局经纬度,3:百度经纬度) int coordType = 3; //围栏圆心(圆心位置, 格式 : "经度,纬度") String center = "120.2126083541,30.245619808604"; //围栏半径(单位 : 米) double radius = 500; //报警条件(1:进入时触发提醒,2:离开时触发提醒,3:进入离开均触发提醒) int alarmCondition = 3; //创建圆形地理围栏 client.createCircularFence(serviceId, creator, fenceName, fenceDesc, monitoredPersons, observers,validTimes, validCycle, validDate, validDays, coordType, center, radius, alarmCondition, geoFenceListener); } /** 围栏查询实时状态 */ private void findFenceAtTime() { if (fenceId != 0) { //监控对象列表(多个entityName,以英文逗号"," 分割) String monitoredPersons = "test"; //查询实时状态 client.queryMonitoredStatus(serviceId, fenceId, monitoredPersons, geoFenceListener); } } /** 围栏历史报警信息 */ private void findFenceOnHistory() { if (fenceId != 0) { //监控对象列表(多个entityName,以英文逗号"," 分割) String monitoredPersons = "test"; //开始时间(unix时间戳) int beginTime = (int) (System.currentTimeMillis() / 1000 - 12 * 60 * 60); //结束时间(unix时间戳) int endTime = (int) (System.currentTimeMillis() / 1000); //查询历史报警信息 client.queryFenceHistoryAlarmInfo(serviceId, fenceId, monitoredPersons, beginTime, endTime, geoFenceListener); } } /** * 接收定位信息 */ private class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { //mapview 销毁后不在处理新接收的位置 if (location == null || mBaiduMap == null) { return; } getLocationInfo(location); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); // 构造定位数据 mOrientationSensor.setOrientationListener(new OrientationSensor.OrientationSensorListener() { @Override public void getOrientation(float x) { driection = x; } }); final MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(driection) .latitude(location.getLatitude()) .longitude(location.getLongitude()) .build(); Log.i(TAG, "driection=" driection); // 设置定位数据 mBaiduMap.setMyLocationData(locData); if (isFirstGetLocation) { isFirstGetLocation = false; // 获取经纬度 LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); // 设置地图中心点以及缩放级别 MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngZoom(latLng, 15); mBaiduMap.animateMapStatus(mapStatusUpdate); } // Log.i(TAG, "latitude:" mBaiduMap.getLocationData().latitude // "longitude:" mBaiduMap.getLocationData().longitude // "direction:" mBaiduMap.getLocationData().direction); // 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标) mCurrentMarker = BitmapDescriptorFactory.fromResource(R.mipmap.location_pic); MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, mCurrentMarker); mBaiduMap.setMyLocationConfigeration(config); } private void getLocationInfo(BDLocation location) { //Receive Location StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("error code : "); sb.append(location.getLocType()); sb.append("latitude : "); sb.append(location.getLatitude()); sb.append("lontitude : "); sb.append(location.getLongitude()); sb.append("radius : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果 sb.append("speed : "); sb.append(location.getSpeed());// 单位:公里每小时 sb.append("satellite : "); sb.append(location.getSatelliteNumber()); sb.append("height : "); sb.append(location.getAltitude());// 单位:米 sb.append("direction : "); sb.append(location.getDirection());// 单位度 sb.append("addr : "); sb.append(location.getAddrStr()); sb.append("describe : "); sb.append("gps定位成功"); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果 sb.append("addr : "); sb.append(location.getAddrStr()); //运营商信息 sb.append("operationers : "); sb.append(location.getOperators()); sb.append("describe : "); sb.append("网络定位成功"); } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果 sb.append("describe : "); sb.append("离线定位成功,离线定位结果也是有效的"); } else if (location.getLocType() == BDLocation.TypeServerError) { sb.append("describe : "); sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因"); } else if (location.getLocType() == BDLocation.TypeNetWorkException) { sb.append("describe : "); sb.append("网络不同导致定位失败,请检查网络是否通畅"); } else if (location.getLocType() == BDLocation.TypeCriteriaException) { sb.append("describe : "); sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机"); } sb.append("locationdescribe : "); sb.append(location.getLocationDescribe());// 位置语义化信息,结果类似于“在北京天安门附近” List list = location.getPoiList();// POI数据 if (list != null) { sb.append("poilist size = : "); sb.append(list.size()); for (Poi p : list) { sb.append("poi= : "); sb.append(p.getId() " " p.getName() " " p.getRank()); } } Log.i(TAG, sb.toString()); } } } package com.dyk.baidumapwork;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Toast;import com.baidu.location.BDLocation;import com.baidu.location.BDLocationListener;import com.baidu.location.LocationClient;import com.baidu.location.LocationClientOption;import com.baidu.location.Poi;import com.baidu.mapapi.SDKInitializer;import com.baidu.mapapi.map.BaiduMap;import com.baidu.mapapi.map.BitmapDescriptor;import com.baidu.mapapi.map.BitmapDescriptorFactory;import com.baidu.mapapi.map.GroundOverlayOptions;import com.baidu.mapapi.map.MapStatusUpdate;import com.baidu.mapapi.map.MapStatusUpdateFactory;import com.baidu.mapapi.map.MapView;import com.baidu.mapapi.map.Marker;import com.baidu.mapapi.map.MarkerOptions;import com.baidu.mapapi.map.MyLocationConfiguration;import com.baidu.mapapi.map.MyLocationData;import com.baidu.mapapi.map.OverlayOptions;import com.baidu.mapapi.map.PolygonOptions;import com.baidu.mapapi.map.Stroke;import com.baidu.mapapi.map.TextOptions;import com.baidu.mapapi.model.LatLng;import com.baidu.mapapi.model.LatLngBounds;import com.baidu.mapapi.radar.RadarNearbyResult;import com.baidu.mapapi.radar.RadarNearbySearchOption;import com.baidu.mapapi.radar.RadarSearchError;import com.baidu.mapapi.radar.RadarSearchListener;import com.baidu.mapapi.radar.RadarSearchManager;import com.baidu.mapapi.radar.RadarUploadInfo;import com.baidu.trace.LBSTraceClient;import com.baidu.trace.OnEntityListener;import com.baidu.trace.OnGeoFenceListener;import com.baidu.trace.OnStartTraceListener;import com.baidu.trace.OnStopTraceListener;import com.baidu.trace.OnTrackListener;import com.baidu.trace.Trace;import org.json.JSONException;import org.json.JSONObject;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends Activity implements View.OnClickListener, RadarSearchListener { private static final String TAG = "BaiduMap"; //周边雷达相关 private MapView mMapView; private BaiduMap mBaiduMap; private Marker markerA; private RadarSearchManager mManager; // 定位相关 private BitmapDescriptor mCurrentMarker; public LocationClient mLocationClient = null; public BDLocationListener myListener = new MyLocationListener(); private boolean isFirstGetLocation = true; // 方向感应器相关 private OrientationSensor mOrientationSensor; /** * 传感器方向 */ private float driection = 0; //鹰眼轨迹相关 private LBSTraceClient client; private Trace trace; /** * 鹰眼服务ID */ private long serviceId = 112700L; /** * 地理围栏监听器 */ private OnGeoFenceListener geoFenceListener; /** * 围栏ID */ private int fenceId; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //在使用SDK各组件之前初始化context信息,传入ApplicationContext //注意该方法要再setContentView方法之前实现 SDKInitializer.initialize(getApplicationContext()); setContentView(R.layout.activity_main); // 初始化周边雷达// mManager = RadarSearchManager.getInstance(); // 实例化轨迹服务客户端 client = new LBSTraceClient(getApplicationContext()); createFence(); initView(); initClick();// testOverlay();// testGeometryOverlay();// testTextOverlay();// testGroundOverlay();// testLocation(); } private void initView() { mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); } private void initClick() { findViewById(R.id.map_common).setOnClickListener(this); findViewById(R.id.map_satelite).setOnClickListener(this); findViewById(R.id.map_blank).setOnClickListener(this); findViewById(R.id.map_traffic).setOnClickListener(this); findViewById(R.id.map_heat).setOnClickListener(this); findViewById(R.id.map_radar).setOnClickListener(this); findViewById(R.id.map_startLocation).setOnClickListener(this); findViewById(R.id.map_startTrace).setOnClickListener(this); findViewById(R.id.map_stopTrace).setOnClickListener(this); findViewById(R.id.map_findLocationAtTime).setOnClickListener(this); findViewById(R.id.map_findLocationOnHistory).setOnClickListener(this); findViewById(R.id.map_findFenceAtTime).setOnClickListener(this); findViewById(R.id.map_findFenceAtHistry).setOnClickListener(this); } /** * 覆盖物 */ private void testOverlay() { LatLng point = new LatLng(39.963175, 116.400244); // 通过marker的icons设置一组图片,再通过period设置多少帧刷新一次图片资源 ArrayList bitmapList = new ArrayList(); BitmapDescriptor bitmapA = BitmapDescriptorFactory.fromResource(R.drawable.icon_marka); BitmapDescriptor bitmapB = BitmapDescriptorFactory.fromResource(R.drawable.icon_markb); BitmapDescriptor bitmapC = BitmapDescriptorFactory.fromResource(R.drawable.icon_markc); BitmapDescriptor bitmapD = BitmapDescriptorFactory.fromResource(R.drawable.icon_markd); BitmapDescriptor bitmapE = BitmapDescriptorFactory.fromResource(R.drawable.icon_marke); bitmapList.add(bitmapA); bitmapList.add(bitmapB); bitmapList.add(bitmapC); bitmapList.add(bitmapD); bitmapList.add(bitmapE); OverlayOptions options = new MarkerOptions() .position(point) //位置 .icons(bitmapList) // 图标 .period(10) // 刷新帧数 .zIndex(9)// 显示层级 .alpha(0.5f) //透明度 .draggable(true);// 支持拖拽 markerA = (Marker) (mBaiduMap.addOverlay(options));// 在地图上添加marker并显示 // 点击marker mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() { @Override public boolean onMarkerClick(Marker marker) { marker.remove(); return true; } }); } /** * 几何覆盖物 */ private void testGeometryOverlay() { //定义多边形的五个顶点 LatLng pt1 = new LatLng(39.93923, 116.357428); LatLng pt2 = new LatLng(39.91923, 116.327428); LatLng pt3 = new LatLng(39.89923, 116.347428); LatLng pt4 = new LatLng(39.89923, 116.367428); LatLng pt5 = new LatLng(39.91923, 116.387428); List pts = new ArrayList(); pts.add(pt1); pts.add(pt2); pts.add(pt3); pts.add(pt4); pts.add(pt5); //构建用户绘制多边形的Option对象 OverlayOptions polygonOption = new PolygonOptions() .points(pts) .stroke(new Stroke(5, 0xAA00FF00)) .fillColor(0xAAFFFF00); //在地图上添加多边形Option,用于显示 mBaiduMap.addOverlay(polygonOption); } /** * 文字覆盖物 */ private void testTextOverlay() { LatLng text = new LatLng(39.86923, 116.397428); OverlayOptions options = new TextOptions() .bgColor(0xAAFFFF00) .fontSize(24) .fontColor(0xFFFF00FF) .text("文字覆盖物测试") .rotate(-45) // 旋转角度 .position(text); mBaiduMap.addOverlay(options); } /** * 地形图图层(GroundOverlay),又可叫做图片图层 */ private void testGroundOverlay() { //定义Ground的显示地理范围 LatLng southwest = new LatLng(39.92235, 116.380338); LatLng northeast = new LatLng(39.947246, 116.414977); LatLngBounds bounds = new LatLngBounds.Builder() .include(northeast) .include(southwest) .build(); //定义Ground显示的图片 BitmapDescriptor bdGround = BitmapDescriptorFactory .fromResource(R.drawable.ground_overlay); //定义Ground覆盖物选项 OverlayOptions ooGround = new GroundOverlayOptions() .positionFromBounds(bounds) .image(bdGround) .transparency(0.8f);// 透明度 //在地图中添加Ground覆盖物 mBaiduMap.addOverlay(ooGround); } /** * 周边雷达信息上传 */ private void testRadarInfoUpload() { // 周边雷达设置监听 mManager.addNearbyInfoListener(this); //周边雷达设置用户身份标识,id为空默认是设备标识 mManager.setUserID("123"); //上传位置 RadarUploadInfo info = new RadarUploadInfo(); LatLng pt = new LatLng(39.963175, 116.400244); info.comments = "备注"; info.pt = pt; mManager.uploadInfoRequest(info); } /** * 雷达周边位置检索 */ private void testRadarLocationSearch() { // 自己的位置 LatLng pt = new LatLng(39.963175, 116.400244); RadarNearbySearchOption option = new RadarNearbySearchOption() .centerPt(pt) .pageNum(0) .radius(2 * 1000); // 发起查询请求 mManager.nearbyInfoRequest(option); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 mMapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 mMapView.onDestroy(); if (mManager != null) { //移除监听 mManager.removeNearbyInfoListener(this); //清除用户信息// mManager.clearUserInfo(); //释放资源 mManager.destroy(); mManager = null; } if (mLocationClient != null && mLocationClient.isStarted()) { mLocationClient.stop(); } if (mBaiduMap.isMyLocationEnabled()) { // 当不需要定位图层时关闭定位图层 mBaiduMap.setMyLocationEnabled(false); } //反注册OrientationSensor if (mOrientationSensor != null) { mOrientationSensor.stop(); } } @Override public void onClick(View v) { switch (v.getId()) { // 普通地图 case R.id.map_common: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); break; // 卫星地图 case R.id.map_satelite: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); break; // 空白地图 case R.id.map_blank: mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NONE); break; // 交通图 case R.id.map_traffic: if (mBaiduMap.isTrafficEnabled()) { mBaiduMap.setTrafficEnabled(false); } else { mBaiduMap.setTrafficEnabled(true); } break; // 热力图 case R.id.map_heat: if (mBaiduMap.isBaiduHeatMapEnabled()) { mBaiduMap.setBaiduHeatMapEnabled(false); } else { mBaiduMap.setBaiduHeatMapEnabled(true); } break; // 周边检索 case R.id.map_radar: testRadarInfoUpload(); testRadarLocationSearch(); break; // 开始定位 case R.id.map_startLocation: mOrientationSensor = new OrientationSensor(this); mOrientationSensor.start(); mLocationClient.start(); break; // 开始鹰眼轨迹追踪 case R.id.map_startTrace: startTrace(); break; // 关闭鹰眼轨迹追踪 case R.id.map_stopTrace: stopTrace(); break; // 鹰眼查询实时位置 case R.id.map_findLocationAtTime: findLocationAtTime(); break; // 鹰眼查询历史轨迹 case R.id.map_findLocationOnHistory: findLocationOnHistory(); break; // 围栏查询实时状态 case R.id.map_findFenceAtTime: // 异步创建Fence 所以应该正规应该使用handlder findFenceAtTime(); break; // 围栏历史报警信息 case R.id.map_findFenceAtHistry: findFenceOnHistory(); break; } } @Override public void onGetNearbyInfoList(RadarNearbyResult radarNearbyResult, RadarSearchError radarSearchError) { if (radarSearchError == RadarSearchError.RADAR_NO_ERROR) { Toast.makeText(MainActivity.this, "查询周边成功", Toast.LENGTH_LONG).show(); Log.i(TAG, radarNearbyResult.infoList.get(0).userID "" radarNearbyResult.infoList.get(0).distance "" radarNearbyResult.infoList.get(0).pt "" radarNearbyResult.infoList.get(0).timeStamp); } else { Toast.makeText(MainActivity.this, "查询周边失败", Toast.LENGTH_LONG).show(); Log.i(TAG, "查询错误:" radarSearchError.toString()); } } @Override public void onGetUploadState(RadarSearchError radarSearchError) { if (radarSearchError == RadarSearchError.RADAR_NO_ERROR) { //上传成功 Toast.makeText(MainActivity.this, "单次上传位置成功", Toast.LENGTH_LONG).show(); } else { //上传失败 Toast.makeText(MainActivity.this, "单次上传位置失败", Toast.LENGTH_LONG).show(); Log.i(TAG, "上传错误:" radarSearchError.toString()); } } @Override public void onGetClearInfoState(RadarSearchError radarSearchError) { if (radarSearchError == RadarSearchError.RADAR_NO_ERROR) { //清除成功 Toast.makeText(MainActivity.this, "清除位置成功", Toast.LENGTH_LONG).show(); } else { //清除失败 Toast.makeText(MainActivity.this, "清除位置失败", Toast.LENGTH_LONG).show(); } } /** * 定位结合方向传感器 */ private void testLocation() { mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 mLocationClient.registerLocationListener(myListener); //注册监听函数 /** * 高精度定位模式:这种定位模式下,会同时使用网络定位和GPS定位,优先返回最高精度的定位结果; * 低功耗定位模式:这种定位模式下,不会使用GPS,只会使用网络定位(Wi-Fi和基站定位); * 仅用设备定位模式:这种定位模式下,不需要连接网络,只使用GPS进行定位,这种模式下不支持室内环境的定位。 */ LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系 int span = 1000; option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true);//可选,默认false,设置是否使用gps option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果 option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死 option.SetIgnoreCacheException(true);//可选,默认false,设置是否收集CRASH信息,默认收集 option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要 mLocationClient.setLocOption(option); } /** * 开始鹰眼轨迹追踪 */ private void startTrace() { // entity标识(监听后台显示的名字) String entityName = "test"; // 轨迹服务类型(0 : 不上传位置数据,也不接收报警信息; 1 : 不上传位置数据,但接收报警信息; // 2 : 上传位置数据,且接收报警信息) int traceType = 2; //实例化轨迹服务 trace = new Trace(getApplicationContext(), serviceId, entityName, traceType); // 实例化开启轨迹服务回调接口 OnStartTraceListener startTraceListener = new OnStartTraceListener() { // 开启轨迹服务回调接口(arg0 : 消息编码,arg1 : 消息内容,详情查看类参考) @Override public void onTraceCallback(int arg0, String arg1) { Log.i(TAG, "onTraceCallback" "arg0 = " arg0 " arg1 = " arg1); } // 轨迹服务推送接口(用于接收服务端推送消息,arg0 : 消息类型,arg1 : 消息内容,详情查看类参考) @Override public void onTracePushCallback(byte arg0, String arg1) { Log.i(TAG, "onTracePushCallback" "arg0 = " arg0 " arg1 = " arg1); } }; // 位置采集周期 int gatherInterval = 10; // 打包周期 int packInterval = 60; // 设置协议类型,0为http,1为https int protocoType = 0; // 设置位置采集和打包周期 client.setInterval(gatherInterval, packInterval); // 设置协议 client.setProtocolType(protocoType); // 开启轨迹服务 client.startTrace(trace, startTraceListener); } /** * 测试数据 */ private Map getMap() { Map map = new HashMap(); map.put("key1", "value1"); map.put("key2", "value2"); return map; } /** * 结束鹰眼轨迹追踪 */ private void stopTrace() { // 实例化停止轨迹服务回调接口 OnStopTraceListener stopTraceListener = new OnStopTraceListener() { // 轨迹服务停止成功 @Override public void onStopTraceSuccess() { Log.i(TAG, "onStopTraceSuccess"); } // 轨迹服务停止失败(arg0 : 错误编码,arg1 : 消息内容,详情查看类参考) @Override public void onStopTraceFailed(int arg0, String arg1) { Log.i(TAG, "onStopTraceFailed" "arg0 = " arg0 " arg1 = " arg1); } }; //停止轨迹服务 client.stopTrace(trace, stopTraceListener); } /** * 鹰眼查询实时位置 */ private void findLocationAtTime() { //entity标识列表(多个entityName,以英文逗号"," 分割) String entityNames = "test"; //检索条件(格式为 : "key1=value1,key2=value2,.....") String columnKey = ""; //返回结果的类型(0 : 返回全部结果,1 : 只返回entityName的列表) int returnType = 0; //活跃时间,UNIX时间戳(指定该字段时,返回从该时间点之后仍有位置变动的entity的实时点集合) int activeTime = (int) (System.currentTimeMillis() / 1000 - 12 * 60 * 60); //分页大小 int pageSize = 1000; //分页索引 int pageIndex = 1; //Entity监听器 OnEntityListener entityListener = new OnEntityListener() { // 查询失败回调接口 @Override public void onRequestFailedCallback(String arg0) { Log.i(TAG, "onRequestFailedCallback" "arg0 = " arg0); } // 查询entity回调接口,返回查询结果列表 @Override public void onQueryEntityListCallback(String arg0) { Log.i(TAG, "onQueryEntityListCallback" " arg0 = " arg0); } }; //查询实时轨迹 client.queryEntityList(serviceId, entityNames, columnKey, returnType, activeTime, pageSize, pageIndex, entityListener); } /** * 鹰眼查询历史轨迹 */ private void findLocationOnHistory() { //entity标识 String entityName = "test"; //是否返回精简的结果(0 : 将只返回经纬度,1 : 将返回经纬度及其他属性信息) int simpleReturn = 1; //开始时间(Unix时间戳) int startTime = (int) (System.currentTimeMillis() / 1000 - 12 * 60 * 60); //结束时间(Unix时间戳) int endTime = (int) (System.currentTimeMillis() / 1000); //分页大小 int pageSize = 1000; //分页索引 int pageIndex = 1; //轨迹查询监听器 OnTrackListener trackListener = new OnTrackListener() { //请求失败回调接口 @Override public void onRequestFailedCallback(String arg0) { Log.i(TAG, "onRequestFailedCallback" "arg0 = " arg0); } // 查询历史轨迹回调接口 @Override public void onQueryHistoryTrackCallback(String arg0) { Log.i(TAG, "onQueryHistoryTrackCallback" "arg0 = " arg0); } }; //查询历史轨迹 client.queryHistoryTrack(serviceId, entityName, simpleReturn, startTime, endTime, pageSize, pageIndex, trackListener); } /** * 创建围栏 */ private void createFence() { geoFenceListener = new OnGeoFenceListener() { //请求失败回调接口 @Override public void onRequestFailedCallback(String arg0) { Log.i(TAG, "geoFence请求失败 :" "arg0 = " arg0); } //创建圆形围栏回调接口 @Override public void onCreateCircularFenceCallback(String arg0) { Log.i(TAG, "创建圆形围栏回调接口消息 : " "arg0 = " arg0); JSONObject dataJson = null; try { dataJson = new JSONObject(arg0); int status = dataJson.getInt("status"); if (0 == status) { fenceId = dataJson.getInt("fence_id"); } } catch (JSONException e) { e.printStackTrace(); } } //更新圆形围栏回调接口 @Override public void onUpdateCircularFenceCallback(String arg0) { Log.i(TAG, "更新圆形围栏回调接口消息 :" "arg0 = " arg0); } //延迟报警回调接口 @Override public void onDelayAlarmCallback(String arg0) { Log.i(TAG, "延迟报警回调接口消息 :" "arg0 = " arg0); } //删除围栏回调接口 @Override public void onDeleteFenceCallback(String arg0) { Log.i(TAG, "删除围栏回调接口消息 :" "arg0 = " arg0); } //查询围栏列表回调接口 @Override public void onQueryFenceListCallback(String arg0) { Log.i(TAG, "查询围栏列表回调接口消息 : " "arg0 = " arg0); } //查询历史报警回调接口 @Override public void onQueryHistoryAlarmCallback(String arg0) { Log.i(TAG, "查询历史报警回调接口消息 : " "arg0 = " arg0); } //查询监控对象状态回调接口 @Override public void onQueryMonitoredStatusCallback(String arg0) { Log.i(TAG, "查询监控对象状态回调接口消息 : " "arg0 = " arg0); } }; //创建者(entity标识) String creator = "test"; //围栏名称 String fenceName = "school"; //围栏描述 String fenceDesc = "学校"; //监控对象列表(多个entityName,以英文逗号"," 分割) String monitoredPersons = "test"; //观察者列表(多个entityName,以英文逗号"," 分割) String observers = "test"; //生效时间列表 String validTimes = ""; //生效周期 int validCycle = 4; //围栏生效日期 String validDate = ""; //生效日期列表 String validDays = ""; //坐标类型 (1:GPS经纬度,2:国测局经纬度,3:百度经纬度) int coordType = 3; //围栏圆心(圆心位置, 格式 : "经度,纬度") String center = "120.2126083541,30.245619808604"; //围栏半径(单位 : 米) double radius = 500; //报警条件(1:进入时触发提醒,2:离开时触发提醒,3:进入离开均触发提醒) int alarmCondition = 3; //创建圆形地理围栏 client.createCircularFence(serviceId, creator, fenceName, fenceDesc, monitoredPersons, observers,validTimes, validCycle, validDate, validDays, coordType, center, radius, alarmCondition, geoFenceListener); } /** 围栏查询实时状态 */ private void findFenceAtTime() { if (fenceId != 0) { //监控对象列表(多个entityName,以英文逗号"," 分割) String monitoredPersons = "test"; //查询实时状态 client.queryMonitoredStatus(serviceId, fenceId, monitoredPersons, geoFenceListener); } } /** 围栏历史报警信息 */ private void findFenceOnHistory() { if (fenceId != 0) { //监控对象列表(多个entityName,以英文逗号"," 分割) String monitoredPersons = "test"; //开始时间(unix时间戳) int beginTime = (int) (System.currentTimeMillis() / 1000 - 12 * 60 * 60); //结束时间(unix时间戳) int endTime = (int) (System.currentTimeMillis() / 1000); //查询历史报警信息 client.queryFenceHistoryAlarmInfo(serviceId, fenceId, monitoredPersons, beginTime, endTime, geoFenceListener); } } /** * 接收定位信息 */ private class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { //mapview 销毁后不在处理新接收的位置 if (location == null || mBaiduMap == null) { return; } getLocationInfo(location); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); // 构造定位数据 mOrientationSensor.setOrientationListener(new OrientationSensor.OrientationSensorListener() { @Override public void getOrientation(float x) { driection = x; } }); final MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(driection) .latitude(location.getLatitude()) .longitude(location.getLongitude()) .build(); Log.i(TAG, "driection=" driection); // 设置定位数据 mBaiduMap.setMyLocationData(locData); if (isFirstGetLocation) { isFirstGetLocation = false; // 获取经纬度 LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); // 设置地图中心点以及缩放级别 MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngZoom(latLng, 15); mBaiduMap.animateMapStatus(mapStatusUpdate); }// Log.i(TAG, "latitude:" mBaiduMap.getLocationData().latitude // "longitude:" mBaiduMap.getLocationData().longitude // "direction:" mBaiduMap.getLocationData().direction); // 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标) mCurrentMarker = BitmapDescriptorFactory.fromResource(R.mipmap.location_pic); MyLocationConfiguration config = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, mCurrentMarker); mBaiduMap.setMyLocationConfigeration(config); } private void getLocationInfo(BDLocation location) { //Receive Location StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("error code : "); sb.append(location.getLocType()); sb.append("latitude : "); sb.append(location.getLatitude()); sb.append("lontitude : "); sb.append(location.getLongitude()); sb.append("radius : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果 sb.append("speed : "); sb.append(location.getSpeed());// 单位:公里每小时 sb.append("satellite : "); sb.append(location.getSatelliteNumber()); sb.append("height : "); sb.append(location.getAltitude());// 单位:米 sb.append("direction : "); sb.append(location.getDirection());// 单位度 sb.append("addr : "); sb.append(location.getAddrStr()); sb.append("describe : "); sb.append("gps定位成功"); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果 sb.append("addr : "); sb.append(location.getAddrStr()); //运营商信息 sb.append("operationers : "); sb.append(location.getOperators()); sb.append("describe : "); sb.append("网络定位成功"); } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果 sb.append("describe : "); sb.append("离线定位成功,离线定位结果也是有效的"); } else if (location.getLocType() == BDLocation.TypeServerError) { sb.append("describe : "); sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因"); } else if (location.getLocType() == BDLocation.TypeNetWorkException) { sb.append("describe : "); sb.append("网络不同导致定位失败,请检查网络是否通畅"); } else if (location.getLocType() == BDLocation.TypeCriteriaException) { sb.append("describe : "); sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机"); } sb.append("locationdescribe : "); sb.append(location.getLocationDescribe());// 位置语义化信息,结果类似于“在北京天安门附近” List list = location.getPoiList();// POI数据 if (list != null) { sb.append("poilist size = : "); sb.append(list.size()); for (Poi p : list) { sb.append("poi= : "); sb.append(p.getId() " " p.getName() " " p.getRank()); } } Log.i(TAG, sb.toString()); } }}
安卓开发,实现数据增加,下拉到底部后自动增加新页并且增加数据(Android development, implement the data increases, down to the bottom after adding new pages and increase data automatically)
android环境下,编写了一个播放类,可以播放播放wav、mp3等声音文件的源代码。(Android environment, the preparation of a broadcast category, you can play wav, mp3 and other sound file source code.)
编程实现在安卓应用界面的添加与查询联系人的功能。(Programming contacts add query functionality Android application interface.)
运行于Android studio的java代码,有完整数据库,需要连接navicat,只要修改DB.properties代码里的你的navicat密码即可,若没有密码,则是password= ,这是非常好,没有任何错误的图书管理系统代码
android语音识别,使用讯飞的sdk(在线使用)(android voice recognition, use iFly sdk)