学习内容来自“慕课网”
这里学习百度地图方向传感器功能
就是当转动手机的时候,地图上显示个人所在位置的图标的箭头变动
学习接自前两篇
思路:
传感器随着定位的开始而开启,随着定位的结束而结束
传感器获得位置的移动而更新方向
代码在前两篇的基础上修改
一、讲原本坐标的图标改成箭头图标
二、引入方向传感器
首先增添MainActivity.java中的自定义图标和方向传感器代码
看红色字体的部分
1 package com.example.map; 2 3 import com.baidu.location.BDLocation; 4 import com.baidu.location.BDLocationListener; 5 import com.baidu.location.LocationClient; 6 import com.baidu.location.LocationClientOption; 7 import com.baidu.mapapi.SDKInitializer; 8 import com.baidu.mapapi.map.BaiduMap; 9 import com.baidu.mapapi.map.BitmapDescriptor; 10 import com.baidu.mapapi.map.BitmapDescriptorFactory; 11 import com.baidu.mapapi.map.MapStatusUpdate; 12 import com.baidu.mapapi.map.MapStatusUpdateFactory; 13 import com.baidu.mapapi.map.MapView; 14 import com.baidu.mapapi.map.MyLocationConfiguration; 15 import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode; 16 import com.baidu.mapapi.map.MyLocationData; 17 import com.baidu.mapapi.model.LatLng; 18 import com.example.map.MyOrientationListener.OnOrientationListener; 19 import com.example.map.R; 20 21 import android.location.Location; 22 import android.os.Bundle; 23 import android.app.Activity; 24 import android.content.Context; 25 import android.graphics.Bitmap; 26 import android.view.Menu; 27 import android.view.MenuItem; 28 import android.view.Window; 29 import android.widget.Toast; 30 31 public class MainActivity extends Activity { 32 33 private MapView mMapView = null; 34 private BaiduMap mBaiduMap; 35 //简化代码 用于this.context=this; 36 private Context context; 37 //定位相关 38 private LocationClient mLocationClient; 39 private MyLocationListener mLocationListener; 40 //是否第一次定位的标志 41 private boolean isFirstIn = true; 42 //定位经纬度 43 private double mLatitude; 44 private double mLongtitude; 45 //自定义定位图标 46 private BitmapDescriptor mIconLocation; 47 private LocationMode mLocationMode;//控制模式 48 private MyOrientationListener myOrientationListener; 49 private float mCurrentX; 50 51 52 @Override 53 protected void onCreate(Bundle savedInstanceState) { 54 super.onCreate(savedInstanceState); 55 //在使用SDK各组件之前初始化context信息,传入ApplicationContext 56 //注意该方法要再setContentView方法之前实现 57 SDKInitializer.initialize(getApplicationContext()); 58 59 requestWindowFeature(Window.FEATURE_NO_TITLE); 60 setContentView(R.layout.activity_main); 61 this.context = this; 62 initView(); 63 //初始化定位 64 initLocation(); 65 66 } 67 68 private void initView() { 69 // TODO Auto-generated method stub 70 mMapView = (MapView) findViewById(R.id.bmapView); 71 mBaiduMap = mMapView.getMap(); 72 //设置打开时的显示比列 这里显示500m左右 73 MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f); 74 mBaiduMap.setMapStatus(msu); 75 } 76 private void initLocation() { 77 // TODO Auto-generated method stub 78 mLocationClient = new LocationClient(this); 79 mLocationListener = new MyLocationListener(); 80 mLocationClient.registerLocationListener(mLocationListener); 81 LocationClientOption option = new LocationClientOption(); 82 option.setCoorType("bd09ll"); 83 option.setIsNeedAddress(true); 84 option.setOpenGps(true); 85 option.setScanSpan(1000); 86 87 mLocationClient.setLocOption(option); 88 89 // 初始化图标 90 mIconLocation = BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps_locked); 91 myOrientationListener = new MyOrientationListener(context); 92 myOrientationListener.setOnOrientationListener(new OnOrientationListener() 93 { 94 @Override 95 public void onOrientationChanged(float x) 96 { 97 mCurrentX = x; 98 } 99 }); 100 } 101 //activity销毁时百度地图也销毁 102 @Override 103 protected void onDestroy() { 104 super.onDestroy(); 105 //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 106 mMapView.onDestroy(); 107 } 108 @Override 109 protected void onStart() { 110 // TODO Auto-generated method stub 111 super.onStart(); 112 // 开启定位 113 mBaiduMap.setMyLocationEnabled(true); 114 if (!mLocationClient.isStarted()) 115 mLocationClient.start(); 116 // 开启方向传感器 117 myOrientationListener.start(); 118 119 } 120 @Override 121 protected void onStop() { 122 // TODO Auto-generated method stub 123 super.onStop(); 124 // 停止定位 125 mBaiduMap.setMyLocationEnabled(false); 126 mLocationClient.stop(); 127 // 停止方向传感器 128 myOrientationListener.stop(); 129 } 130 @Override 131 protected void onResume() { 132 super.onResume(); 133 //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 134 mMapView.onResume(); 135 } 136 @Override 137 protected void onPause() { 138 super.onPause(); 139 //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 140 mMapView.onPause(); 141 } 142 @Override 143 public boolean onCreateOptionsMenu(Menu menu) { 144 // TODO Auto-generated method stub 145 146 getMenuInflater().inflate(R.menu.main, menu); 147 return true; 148 } 149 //菜单按钮的响应事件 150 @Override 151 public boolean onOptionsItemSelected(MenuItem item) { 152 // TODO Auto-generated method stub 153 switch (item.getItemId()) { 154 case R.id.id_map_common: 155 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); 156 break; 157 case R.id.id_map_site: 158 mBaiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); 159 break; 160 case R.id.id_map_traffic: 161 if(mBaiduMap.isTrafficEnabled()) 162 { 163 mBaiduMap.setTrafficEnabled(false); 164 item.setTitle("实时交通(off)"); 165 } 166 else 167 { 168 mBaiduMap.setTrafficEnabled(true); 169 item.setTitle("实时交通(on)"); 170 } 171 break; 172 case R.id.id_map_location: 173 //定位最新自己的位置 174 centerToMyLocation(); 175 break; 176 177 default: 178 break; 179 } 180 return super.onOptionsItemSelected(item); 181 } 182 //进到自己的位置 183 private void centerToMyLocation() { 184 LatLng latLng = new LatLng(mLatitude,mLongtitude); 185 186 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng); 187 mBaiduMap.animateMapStatus(msu); 188 } 189 private class MyLocationListener implements BDLocationListener{ 190 191 @Override 192 public void onReceiveLocation(BDLocation location) { 193 // TODO Auto-generated method stub 194 MyLocationData data = new MyLocationData.Builder()// 195 .direction(mCurrentX)// 196 .accuracy(location.getRadius())// 197 .latitude(location.getLatitude())// 198 .longitude(location.getLongitude())// 199 .build(); 200 mBaiduMap.setMyLocationData(data); 201 // 设置自定义图标 202 MyLocationConfiguration config = new MyLocationConfiguration(mLocationMode.NORMAL, true, mIconLocation); 203 mBaiduMap.setMyLocationConfigeration(config); 204 //获取最新经纬度 205 mLatitude = location.getLatitude(); 206 mLongtitude = location.getLongitude(); 207 //判断是否第一次定位 208 if (isFirstIn) 209 { 210 //设置经纬度 211 LatLng latLng = new LatLng(location.getLatitude(),location.getLongitude()); 212 213 MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng); 214 mBaiduMap.animateMapStatus(msu); 215 //将是否第一次定位的标志 变为不是第一次定位 216 isFirstIn = false; 217 //显示当前定位的位置 218 Toast.makeText(context, location.getAddrStr(), 219 Toast.LENGTH_SHORT).show(); 220 } 221 222 } 223 224 } 225 }
添加方向传感器部分的类
MyLocationListener.java
![](https://yqfile.alicdn.com/img_1c53668bcee393edac0d7b3b3daff1ae.gif?x-oss-process=image/resize,w_1400/format,webp)
![](https://yqfile.alicdn.com/img_405b18b4b6584ae338e0f6ecaf736533.gif?x-oss-process=image/resize,w_1400/format,webp)
1 package com.example.map; 2 3 import android.content.Context; 4 import android.hardware.Sensor; 5 import android.hardware.SensorEvent; 6 import android.hardware.SensorEventListener; 7 import android.hardware.SensorManager; 8 9 public class MyOrientationListener implements SensorEventListener 10 { 11 private SensorManager mSensorManager; 12 private Context mContext; 13 //传感器 14 private Sensor mSensor; 15 private float lastX; 16 17 //构造方法 18 public MyOrientationListener(Context context) 19 { 20 this.mContext = context; 21 } 22 23 @SuppressWarnings("deprecation") 24 //开始监听 25 public void start() 26 { 27 //拿到系统服务 28 mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); 29 if (mSensorManager != null) 30 { 31 // 获得方向传感器 32 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 33 } 34 35 if (mSensor != null) 36 { 37 mSensorManager.registerListener(this, mSensor, 38 SensorManager.SENSOR_DELAY_UI); 39 } 40 } 41 42 //结束监听 43 public void stop() 44 { 45 mSensorManager.unregisterListener(this); 46 } 47 48 @Override 49 public void onAccuracyChanged(Sensor arg0, int arg1) 50 { 51 // TODO Auto-generated method stub 52 53 } 54 55 @SuppressWarnings( 56 { "deprecation" }) 57 @Override 58 //方向发生变化 59 public void onSensorChanged(SensorEvent event) 60 { 61 if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) 62 { 63 float x = event.values[SensorManager.DATA_X]; 64 65 if (Math.abs(x - lastX) > 1.0) 66 { 67 if (mOnOrientationListener != null) 68 { 69 mOnOrientationListener.onOrientationChanged(x); 70 } 71 } 72 73 lastX = x; 74 75 } 76 } 77 78 private OnOrientationListener mOnOrientationListener; 79 80 public void setOnOrientationListener( 81 OnOrientationListener mOnOrientationListener) 82 { 83 this.mOnOrientationListener = mOnOrientationListener; 84 } 85 86 public interface OnOrientationListener 87 { 88 void onOrientationChanged(float x); 89 } 90 91 }
改动以上两部分后便可以看到效果,移动手机方向,坐标箭头方向随之变化
效果图
坐标箭头的素材