利用方向传感判断机头方向传值给定位
效果图:
用到的小图,自行下载:
原理就是利用方向传感判断机头方向传值给定位:
方向传感代码:
import android.content.Context; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; /* * @Comment : * @Created :2018/10/25 19:01 * @Name : YJ */public class MyOrientationListener implements SensorEventListener { private SensorManager mSensorManager; private Context mContext; private Sensor mSensor; private float lastX; private OnOrientationListener mOnOrientationListener; public void setmOnOrientationListener(OnOrientationListener mOnOrientationListener) { this.mOnOrientationListener = mOnOrientationListener; } public MyOrientationListener(Context context) { this.mContext = context; } public void star() { mSensorManager = (SensorManager) mContext .getSystemService(Context.SENSOR_SERVICE); if (mSensorManager != null) { //获得方向传感器 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); } if (mSensor != null) { mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_UI); } } public void stop() { //停止定位 mSensorManager.unregisterListener(this); } @Override public void onSensorChanged(SensorEvent event) { if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) { float x = event.values[SensorManager.DATA_X]; if (Math.abs(x - lastX) > 1.0) { if (mOnOrientationListener != null) { mOnOrientationListener.onOrientationChanged(x); } } lastX = x; } } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } public interface OnOrientationListener { void onOrientationChanged(float x); } }
然后在需要用到的地方调用:
private MyOrientationListener myOrientationListener;
这个再onCreate里面调用
private void initMyOrien() { //初始化图标 bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.jiantou); //方向传感器 myOrientationListener = new MyOrientationListener(this); myOrientationListener.setmOnOrientationListener(new MyOrientationListener.OnOrientationListener() { @Override public void onOrientationChanged(float x) { mLastX = x; } }); }
最后在地图监听里面调用这个方法传递过来的方向值就行:
locData = new MyLocationData.Builder() .accuracy(location.getRadius()) .direction(mLastX)// 此处设置开发者获取到的方向信息,顺时针0-360 .latitude(mLatitude) .longitude(mLongitude).build(); mBaiduMap.setMyLocationData(locData);// 设置定位数据
下面是全部的代码:
布局文件的代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <com.baidu.mapapi.map.MapView android:id="@+id/mapview" android:layout_width="match_parent" android:layout_height="match_parent"> </com.baidu.mapapi.map.MapView> <ImageView android:layout_width="wrap_content" android:layout_height="40dp" android:layout_centerInParent="true" android:paddingBottom="20dp" android:src="@mipmap/dingwei2" /> <ImageView android:id="@+id/sa_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_margin="10dp" android:src="@mipmap/icon_location" /> </RelativeLayout> </LinearLayout>
MainActivity里面的代码:
package com.example.administrator.baidumap1; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; 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.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.MapStatus; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MyLocationConfiguration; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.model.LatLng; import java.io.PipedInputStream; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private MapView mMapView; private BaiduMap mBaiduMap; private ImageView mSaLocation; private LocationClient mLocationClient; private LatLng latLng; private boolean isFirstLoc = true;//是否是首次定位 public BDLocationListener myListener = new MyLocationListener(); private MyLocationData locData; private BitmapDescriptor bitmapDescriptor; private MyOrientationListener myOrientationListener; private float mLastX; private double mLatitude; private double mLongitude; private PipedInputStream client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setInit(); initMap(); initMyOrien(); } public void setInit() { mMapView = findViewById(R.id.mapview); mBaiduMap = mMapView.getMap(); mMapView.showZoomControls(false); mSaLocation = findViewById(R.id.sa_location); mSaLocation.setOnClickListener(this); } private void initMyOrien() { //初始化图标 bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.jiantou); //方向传感器 myOrientationListener = new MyOrientationListener(this); myOrientationListener.setmOnOrientationListener(new MyOrientationListener.OnOrientationListener() { @Override public void onOrientationChanged(float x) { mLastX = x; } }); } /** * 定位 */ private void initMap() { mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);//默认显示普通地图 mBaiduMap.setMyLocationEnabled(true);// 开启定位图层 mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 initLocation();//配置定位SDK参数 mLocationClient.registerLocationListener(myListener); //注册监听函数 mLocationClient.start();//开启定位 mLocationClient.requestLocation();//图片点击事件,回到定位点 } /** * 配置定位SDK参数 */ private void initLocation() { LocationClientOption option = new LocationClientOption(); option.setCoorType("bd09ll");//可选,coorType - 取值有3个: 返回国测局经纬度坐标系:gcj02 返回百度墨卡托坐标系 :bd09 返回百度经纬度坐标系 :bd09ll Log.e("获取地址信息设置", option.getAddrType());//获取地址信息设置 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true); // 是否打开gps进行定位 option.setLocationNotify(true);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果 option.setScanSpan(1000);//可选,设置的扫描间隔,单位是毫秒,设置发起定位请求的间隔需要大于等于1000ms才是有效的 Log.e("获取设置的Prod字段值", option.getProdName()); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setNeedDeviceDirect(true);//在网络定位时,是否需要设备方向- true:需要 ; false:不需要。默认为false option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集 option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死 option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation // .getLocationDescribe里得到,结果类似于“在北京天安门附近” option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要 mLocationClient.setLocOption(option); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.sa_location: //把定位点再次显现出来 MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLng(latLng); mBaiduMap.animateMapStatus(mapStatusUpdate); break; } } //实现BDLocationListener接口,BDLocationListener为结果监听接口,异步获取定位结果 public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { latLng = new LatLng(location.getLatitude(), location.getLongitude()); // 构造定位数据 mLatitude= location.getLatitude(); mLongitude=location.getLongitude(); locData = new MyLocationData.Builder() .accuracy(location.getRadius()) .direction(mLastX)// 此处设置开发者获取到的方向信息,顺时针0-360 .latitude(mLatitude) .longitude(mLongitude).build(); mBaiduMap.setMyLocationData(locData);// 设置定位数据 //更新经纬度 mLatitude = location.getLatitude(); mLongitude = location.getLongitude(); //配置定位图层显示方式,使用自己的定位图标 //LocationMode定位模式有三种:普通模式,跟随模式,罗盘模式,在这使用普通模式 MyLocationConfiguration configuration = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, bitmapDescriptor); mBaiduMap.setMyLocationConfigeration(configuration); if (isFirstLoc) { isFirstLoc = false; LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); MapStatusUpdate msu= MapStatusUpdateFactory.newLatLng(latLng); //改变地图状态 mBaiduMap.setMapStatus(msu); MapStatus.Builder builder = new MapStatus.Builder(); builder.target(latLng).zoom(18.0f); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果 Toast.makeText(MainActivity.this, location.getAddrStr(), Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果 Toast.makeText(MainActivity.this, location.getAddrStr(), Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果 Toast.makeText(MainActivity.this, location.getAddrStr(), Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeServerError) {//服务器错误 Toast.makeText(MainActivity.this, "服务器错误,请检查", Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeNetWorkException) {//网络错误 Toast.makeText(MainActivity.this, "网络错误,请检查", Toast.LENGTH_SHORT).show(); } else if (location.getLocType() == BDLocation.TypeCriteriaException) {//手机模式错误 Toast.makeText(MainActivity.this, "手机模式错误,请检查是否飞行", Toast.LENGTH_SHORT).show(); } } } } @Override protected void onStart() { super.onStart(); //开启定位的允许 mBaiduMap.setMyLocationEnabled(true); if (!mLocationClient.isStarted()) { mLocationClient.start(); //开启方向传感器 myOrientationListener.star(); } } @Override protected void onStop() { super.onStop(); //关闭定位 mBaiduMap.setMyLocationEnabled(false); mLocationClient.stop(); //停止方向传感器 myOrientationListener.stop(); } @Override protected void onResume() { super.onResume(); mMapView.onResume(); } @Override protected void onPause() { super.onPause(); mMapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); mMapView.onDestroy(); } }
至于方向传感的工具类上面有就不贴了!
下载地址:https://download.csdn.net/download/lanrenxiaowen/10745509