//自定义导航图标private BitmapDescriptor mIconLocation;//自定义传感器implements SensorEventListenerprivate MyOrientationListener myOrientationListener;//把监听的x方向的值存储到这里private float mCurrentX;
//initialize icon miconlocation = Bit Mapdescriptorfactory.fromresource (R.drawable.ic_launcher); //Initialize callback interface Myorientationlistener = new myorient Ationlistener (Getapplicationcontext ()); //Set callback interface Myorientationlistener.setonorientationlistener (new Onorientationlistener () { @Override public void Onorientationchanged (float x) {//TODO auto-generated method stub //callback interface value stored in Mcurrentx mCu Rrentx=x; } });
Synchronization with the life cycle of direction recognition within the activity lifecycle
Start method inside
//方向识别开始 myOrientationListener.start();
Stop method inside
//停止方向传感器 myOrientationListener.stop();
Inside the local Data settings
MyLocationData data = new MyLocationData.Builder() //设置方向 .direction(mCurrentX)//设置自定义图标 MyLocationConfiguration config = new MyLocationConfiguration(com.baidu.mapapi.map.MyLocationConfiguration.LocationMode.NORMAL, true, mIconLocation); mBaiduMap.setMyLocationConfigeration(config);
Here is the implementation code for sensor monitoring
Public class Myorientationlistener implements Sensoreventlistener{ //used to get Seosor PrivateSensormanager Msensormanager;PrivateSensor Msensor;PrivateContext Mcontext;Private floatLASTX;PrivateOnorientationlistener Monorientationlistener; Public Myorientationlistener(Context context) {Super(); context= This. Mcontext; }@SuppressWarnings("Deprecation")//How to turn on monitoring Public void Start() {Msensormanager = (Sensormanager) mcontext.getsystemservice (Context.sensor_service);if(msensormanager!=NULL) {msensor = Msensormanager.getdefaultsensor (sensormanager.sensor_orientation); }if(msensor!=NULL){//Register for monitoringMsensormanager.registerlistener ( This, Msensor, sensormanager.sensor_delay_ui); } }//How to stop listening Public void Stop() {Msensormanager.unregisterlistener ( This); }@Override Public void onaccuracychanged(Sensor arg0,intARG1) {}@Override Public void onsensorchanged(Sensorevent event) {//Get direction sensor if (Event.sensor.getType () ==sensor.type_orientation) { floatx = event.values[sensormanager.data_x];//If moving more than one precision then turn if(Math.Abs (X-LASTX) >1.0){if(monorientationlistener!=NULL) {monorientationlistener.onorientationchanged (x); }} LASTX =x; } } Public void Setonorientationlistener(Onorientationlistener Monorientationlistener) { This. Monorientationlistener = Monorientationlistener; }//Define Callback interface Public interface onorientationlistener{ //Pass change in the X-direction value of the listener voidOnorientationchanged (floatx); }}
Android for mobile phone orientation recognition