标签:android
<!-- 定位service --> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" > </service>
/** * 初始化定位 */ 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(); }
/** * 定位的监听器 * * @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); } }
@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(); }
/** * 初始化方向传感器 */ 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)); } }); }
Android 百度地图 SDK v3.3.0 (二)--- 地图定位和图层展示
标签:android
原文地址:http://blog.csdn.net/tanzuai/article/details/43763159