Android 百度map SDK v3.3.0 (二)- map定位和图层展示
Android 百度地图 SDK v3.3.0 (二)--- 地图定位和图层展示
在上一篇博客中,我们成功把地图导入了我们的项目。本篇我们准备为地图添加:第一,定位功能;第二,图层展示,第三,结合方向传感器,通过旋转手机进行道路的方向确认。有了这三个功能,地图已经可以为我服务了!
转载请注明出处:http://blog.csdn.net/tanzuai/article/details/43763159
在上一篇博客中,我们成功把地图导入了我们的项目。本篇我们准备为地图添加:第一,定位功能;第二,图层展示,第三,结合方向传感器,通过旋转手机进行道路的方向确认。有了这三个功能,地图已经可以为我服务了!
为了方便,我把所有的按钮都放到了menu菜单中。
1.在AndroidManifest.xml配一个service
<!-- 定位service --> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" > </service>
2、初次启动定位
/** * 初始化定位 */ private void initMyLocation() { // 地图初始化 mBaiduMap = mMapView.getMap(); MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f); mBaiduMap.setMapStatus(msu); // 开启定位图层 //mBaiduMap.setMyLocationEnabled(true); // 定位初始化 mLocClient = new LocationClient(this); mLocClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true);// 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(1000);//设置发起定位请求的间隔时间为1000ms option.setIsNeedAddress(true);//返回的定位结果包含地址信息 option.setNeedDeviceDirect(true);//返回的定位结果包含手机机头的方向 mLocClient.setLocOption(option);//设置定位参数 //mLocClient.start(); }
3.然后是实现实位回调监听MyLocationListener:
/** * 实现实位回调监听 */ public class MyLocationListenner implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { // map view 销毁后不在处理新接收的位置 if (location == null || mMapView == null) return; MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(100).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); mCurrentAccracy = location.getRadius(); // 设置定位数据 mBaiduMap.setMyLocationData(locData); mCurrentLantitude = location.getLatitude(); mCurrentLongitude = location.getLongitude(); // 设置自定义图标 mCurrentMarker = BitmapDescriptorFactory .fromResource(R.drawable.navi_map_gps_locked); mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration( mCurrentMode, true, mCurrentMarker)); if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); mBaiduMap.animateMapStatus(u); } } public void onReceivePoi(BDLocation poiLocation) { } }
4.兼容定位的监听器
/** * 定位的监听器 * * @author TanZuAi */ public class MyOrientationListener implements SensorEventListener { private Context context; private SensorManager sensorManager; private Sensor sensor; private float lastX; private OnOrientationListener onOrientationListener; public MyOrientationListener(Context context) { this.context = context; } // 开始 public void start() { // 获得传感器管理器 sensorManager = (SensorManager) context .getSystemService(Context.SENSOR_SERVICE); if (sensorManager != null) { // 获得方向传感器 sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); } // 注册 if (sensor != null) {// SensorManager.SENSOR_DELAY_UI sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_UI); } } // 停止检测 public void stop() { sensorManager.unregisterListener(this); } @Override public void onAccuracyChanged(Sensor sensor, int accuracy) { } @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) { onOrientationListener.onOrientationChanged(x); } lastX = x; } } public void setOnOrientationListener( OnOrientationListener onOrientationListener) { this.onOrientationListener = onOrientationListener; } public interface OnOrientationListener { void onOrientationChanged(float x); } }
5. 定位也是比较耗电的,所以我们在onStart中开启定位,在onStop中关闭定位~~这样应用最小化时就不会一直在哪GPS请求定位了,用户 要是看你app一直在那定位,估计马上就被卸载了~
@Override protected void onStart() { // 开启图层定位 mBaiduMap.setMyLocationEnabled(true); if (!mLocClient.isStarted()) { mLocClient.start(); } // 开启方向传感器 myOrientationListener.start(); super.onStart(); } @Override protected void onStop() { // 关闭图层定位 mBaiduMap.setMyLocationEnabled(false); mLocClient.stop(); // 关闭方向传感器 myOrientationListener.stop(); super.onStop(); }
6.在onCreate中初始化方向传感器
/** * 初始化方向传感器 */ private void initOritationListener() { myOrientationListener = new MyOrientationListener( getApplicationContext()); myOrientationListener .setOnOrientationListener(new OnOrientationListener() { @Override public void onOrientationChanged(float x) { mXDirection = (int) x; // 构造定位数据 MyLocationData locData = new MyLocationData.Builder() .accuracy(mCurrentAccracy) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(mXDirection) .latitude(mCurrentLantitude) .longitude(mCurrentLongitude).build(); // 设置定位数据 mBaiduMap.setMyLocationData(locData); // 设置自定义图标 mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.navi_map_gps_locked); mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration( mCurrentMode, true, mCurrentMarker)); } }); }
好了,介绍完毕了
代码请到下面的地址下载: